首页 > 解决方案 > 我们如何在 MATLAB 中分离传入的实时数据?

问题描述

我在matlab中连续获取实时数据。但是数据是交替出现的两个参数(电压和温度)的组合。那么我该如何分离和绘制它。

标签: matlabstm32

解决方案


这将是一种方法。除了分离电压和温度读数之外,我还冒昧地重新组织了您的代码,以便它只创建一次图,然后更新它(而不是在每个新样本上生成一个全新的图)。

我还将示例读取打包到一个匿名函数中,这样就不必经常重复代码。

时间戳可以从第一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

推荐阅读