【问题标题】:MATLAB plotting a changing vector in a while loopMATLAB 在 while 循环中绘制一个变化的向量
【发布时间】:2014-01-27 22:10:11
【问题描述】:

我正在为一个电路工程项目将 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

【问题讨论】:

    标签: matlab


    【解决方案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
    

    【讨论】:

    • @SciurusDoomus 很高兴它有帮助。另外,请参阅编辑后的答案以及可能有用的另一个想法
    • 有趣。我真的不需要减少运行时间,因为绘图更像是一种测试机制,而不是我想要输出的实际数据。但是,我确信我将来会需要它,或者让这个程序在非常旧的系统上可用。谢谢!
    猜你喜欢
    • 2013-04-23
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 2013-02-18
    相关资源
    最近更新 更多