MATLAB在while循环中绘制变化向量

时间:2014-01-27 22:10:11

标签: matlab

我正在将MATLAB与Arduino接口用于电路工程项目。我想在Arduino上查看它在给定传感器上感应到的电压,将该电压添加到矢量,然后将其全部绘制在同一个while循环中。我有前两个部分,但我似乎无法弄清楚如何随着时间的推移一遍又一遍地绘制电压矢量。有没有办法做到这一点?

%{
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

1 个答案:

答案 0 :(得分:1)

尝试在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
相关问题