2014-01-27 107 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