实时绘制文本文件

时间:2014-02-16 05:39:08

标签: iphone c matlab plot

我正在尝试进行温度与时间的实时绘图(或接近实时,只要它每1到2秒更新一次)。温度和时间数据将存储在文本文件中。该文本文件将由一个单独的程序不断更新。

是否可以使用Matlab或C执行此操作?

我在网上找到了这些代码,但我不知道如何使用它们;我只是创建一个新的Matlab脚本并将它们放入脚本中吗?

function [t] = livePlot(period, filename)
//%   inputs : period : update rate in seconds
//%            filename : name of the file to get data from
//%
//%   outputs: t      : the timer object
//%                     >> stop(t) 
//%                     ends streaming
//%%

close all;        
t = timer('StartDelay', 1, 'Period', period, ...
          'ExecutionMode', 'fixedRate');
//%% timer object callback functions
t.StopFcn  = {@stopFigure};
t.TimerFcn = {@updateFigure};
//%% initialize timer object user data
d = get(t, 'UserData');
d.data = []; % array for the data to plot
axes('Position', [0 0 1 1], 'Visible', 'off');
    d.axes_handle = axes('Position', [.2 .1 .7 .8]);
    d.line_handle = plot(NaN,NaN);
    d.fid = fopen(filename, 'r');    
    set(t, 'UserData', d);    
    start(t);
end

function stopFigure(obj, event)
    //% close function handle
    d = get(obj, 'UserData');
    fclose(d.fid);
end

function updateFigure(obj, event)
    d = get(obj, 'UserData');        
    //% read new data from file
    tmp = readFile(obj);
    //% append to array in user data
    d.data = [d.data transpose(tmp)];
    //% update the plot 
    set(gcf, 'CurrentAxes', d.axes_handle);
    set(d.line_handle, 'XData', 1:length(d.data), 'YData', d.data);
    //% store the timer object user-data
    set(obj, 'UserData', d);
end
function [tmp] = readFile(obj)
    //% read binary data. file-ID is in the timer user-data
    d = get(obj, 'UserData');
    tmp = fread(d.fid);
    fprintf('Current file location : %d \n', ftell(d.fid));
    //% fprintf('End of file indicator : %d \n', feof(d.fid));
    //% reset the end-of-file indicator
    fseek(d.fid, -1, 0);
    fseek(d.fid, 1, 0);
    //% fprintf('End of file indicator : %d \n', feof(d.fid));
    set(obj, 'UserData', d); 
end

1 个答案:

答案 0 :(得分:0)

如果您的文本文件包含以下温度值和date.sorry,因为格式不正确,请考虑第1列为白天,2为时间,3为临时。

Day      Time      Temp
1        09:00     16
2        09:00     16.2

然后您可以使用下面的示例c代码进行绘图

f = fopen('yourdata.txt');
c = textscan(f,'%f %s %f','HeaderLines',1);
fclose(f);
d = [c{1}-1 + rem(datenum(c{2},'HH:MM'),1), c{3}];
相关问题