MATLAB在一段时间内绘制变化的向量



我正在将MATLAB与Arduino接口到电路工程项目。我想将其在给定传感器处感应的电压进行轮询,并将电压添加到向量上,然后在循环时将其全部绘制在同一中。我将前两个部分倒下,但我似乎无法弄清楚如何一遍又一遍地绘制电压向量。有办法这样做吗?

%{
Ventilation Rate Sensor v0.1
This program uses a thermistor connected to pin A0 and analyzes the 
difference in voltage drop in order to assess the user's ventilation rate. 
Designed for use with a voltage divider using a 2.2kOhm resistor and a
10kOhm (at 25C) thermistor in series. Note that this REQUIRES the Arduino
to have the code for MATLAB interface already installed. This is included
in the MATLAB Arduino software page at
<<http://www.mathworks.com/matlabcentral/fileexchange/
32374-matlab-support-package-for-arduino-aka-arduinoio-package>>
%}
clc
clear
close all
ard = arduino('COM3');
voltage = [];
timer = datenum(clock+[0,0,0,0,0,30]);

while datenum(clock) < timer
    sensorValue = ard.analogRead(0);
    voltage = [voltage (sensorValue * (5/1023))];
    hold on;
    t = [1:1:length(voltage)];
    plot(t,voltage)
end

尝试在plot行之后添加drawnow。这会冲洗事件队列并迫使Matlab执行情节。

另外,您可以更新图的X和Y数据,而不是每次进行新的图。也许可以节省一些运行时间:

h = plot(NaN,NaN); %// dummy plot (for now). Get a handle to it
while [...]
    [...]
    set(h,'xdata',t,'ydata',voltage); %// update plot's x and y data
end

最新更新