2014-01-27 106 views
0

我MATLAB接口与一个Arduino的电路工程密谋改变向量。我想轮询Arduino在给定传感器上感应到的电压,将该电压添加到一个矢量,然后将它全部绘制在同一个循环内。我有前两个部分,但我似乎无法弄清楚如何反复绘制电压矢量,因为它变得更长。有没有办法做到这一点?MATLAB在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

尝试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 
+0

谢谢!这工作得很好。 – SciurusDoomus

+0

@SciurusDoomus很高兴帮助。另外,请参阅编辑答案与另一个可能有用的想法 –

+0

有趣。我并不需要削减运行时间,因为绘图比我想要输出的实际数据更像是一种测试机制。但是,我相信我将来需要它,或者让这个程序在真正旧的系统上可用。谢谢! – SciurusDoomus