matlab - 我们如何在 MATLAB 中分离传入的实时数据?
问题描述
我在matlab中连续获取实时数据。但是数据是交替出现的两个参数(电压和温度)的组合。那么我该如何分离和绘制它。
解决方案
这将是一种方法。除了分离电压和温度读数之外,我还冒昧地重新组织了您的代码,以便它只创建一次图,然后更新它(而不是在每个新样本上生成一个全新的图)。
我还将示例读取打包到一个匿名函数中,这样就不必经常重复代码。
时间戳可以从第一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
推荐阅读
- fortran - 错误: (1) 处的 READ 语句中的预期变量但已声明变量
- javascript - How to create an image with gradient where top has opacity 100% and bottom with opacity 0%?
- scala - 当文档的用法只是说 `> fooBar` 是什么意思?
- c++ - 将函数声明为指针的目的是什么?
- php - 是否可以将 PHP 数据库连接逻辑放在 HTML 中
- litedb - 在我删除了大约 200 条记录后,如何减小 LiteDb 创建的 db 文件的大小
- reactjs - 在 React 组件中滚动时状态不更新?
- reactjs - 从 Firebase 服务器响应更新状态
- python - 将 pandas_datareader 存储到新的 csv 文件中
- ios - 如何将 UIProgressView 的进度图像大小调整为整个进度视图轨道的大小