我正在matlab中串行获取实时数据,但是数据是交替出现的两个参数(电压和温度)的组合。那么我该如何分离和绘制它。
答案 0 :(得分:1)
这将是一种方法。除了分离电压和温度读数外,我还自由地组织了代码,因此它只创建了一个绘图,然后又对其进行了更新(而不是在每个新样本中生成全新的绘图)。
我还将示例阅读包打包到一个匿名函数中,而不必太重复代码。
可以从第一条tic
指令获得时间戳。您不必一个个地添加每个间隔。
% Prepare the plot so you do not have to recreate it at each new sample
Time = zeros(1,1) ;
Voltage = zeros(1,1) ;
Temperature = zeros(1,1) ;
hvolt = plot(Time,Voltage) ;
% only if you want to display temperature, uncomment below line
% hold on ; htemp = plot(Time,Temperature) ; hold off
ylim([3,5]);
ylabel('Voltage');
xlabel('Time');
% Define an anonymous function for reading ONE sample
readSample = @(s) str2double(char(fread(s, 5).')) ;
SampleCounter = 1 ;
StartTime = tic ;
while 1
% Read 2 samples (one Voltage and one Temperature)
Voltage(SampleCounter) = readSample(s) ;
Temperature(SampleCounter) = readSample(s) ;
% Readt the time
Time(SampleCounter,1) = toc(StartTime) ;
% Now we acquired 2 samples (one voltage and one temperature), we can
% refresh the display:
set(hvolt,'XData',Time,'YData',Voltage) ;
% only if you want to display temperature, uncomment below line
% set(htemp,'XData',Time,'YData',Temperature) ;
drawnow
SampleCounter=SampleCounter+1;
end