获取Matlab绘图滚动

时间:2016-09-19 13:15:11

标签: matlab matlab-figure

我正在尝试从微控制器发送的串行端口绘制数据,解释此数据并绘制图形。数据将以非常快的速度进入(每隔50微秒),所以一旦我达到一定数量的数据点,我想滚动图表。我已经能够成功地绘制单个数据值和多个数据值而无需滚动,但是当我尝试实现滚动时,值会失真,当我达到开始滚动的值时,我的代码通常会中断。

delete(instrfind);
clear;
close all;

s = serial('COM3'); %assigns the object s to serial port

set(s, 'InputBufferSize', 1); %number of bytes in inout buffer
set(s, 'FlowControl', 'hardware');
set(s, 'BaudRate', 9600);
set(s, 'Parity', 'none');
set(s, 'DataBits', 8);
set(s, 'StopBit', 1);
set(s, 'Timeout',10);
%s.Terminator = '"';
clc;

disp(get(s,'Name'));
prop(1)=(get(s,'BaudRate'));
prop(2)=(get(s,'DataBits'));
prop(3)=(get(s, 'StopBit'));
prop(4)=(get(s, 'InputBufferSize'));

disp(['Port Setup Done!!',num2str(prop)]);

fopen(s);           %opens the serial port
t=1;
a = zeros(100,'int8');
dataToDisplay = zeros(100,'int8');
disp('Running');


dataToDisplay = [];
while(t < 501)  %Runs for 500 cycles

       for x = 1:4
       a(x) = fread(s); %reads 3 values of the data from the serial port and stores it to the matrix a
       end

       if (t>101)
           for i = 1:98
           dataToDisplay(100) = ((a(1)-96)*10)+(a(2)-80)+((a(3)-32)/10); % combines the values in a and changes them into the value to display
           dataToDisplay(i) = dataToDisplay(i+1);
           end
       else
           dataToDisplay(t) = ((a(1)-96)*10)+(a(2)-80)+((a(3)-32)/10);
   end




   %if(data(t) == 10)
   %dataToDisplay(t) = a;   

   plot(dataToDisplay,'-*r');
   axis auto;
   grid on;
   hold on;

   t=t+1;
   x = 0;
   a=0;  %Clear the buffer
   drawnow;
end

fclose(s); %close the serial port

我还应该补充一点,我正在阅读的值将同时显示在4个七段显示器上,因此需要解码才能将数字输入到我希望显示的表格中。前3个显示器保存数字,而第4个显示器保存单元,此时matlab代码中不需要。

2 个答案:

答案 0 :(得分:0)

您的代码因为假定的错字而中断:

       dataToDisplay(i) = dataToDisplay(i+1);

一旦t = 102,它就会请求dataToDisplay(103)的值,该值尚不存在。

您想通过以下几行完成什么?我认为你的逻辑和实现之间存在差异

if (t>101)
       for i = 1:98
       dataToDisplay(100) = ((a(1)-96)*10)+(a(2)-80)+((a(3)-32)/10); % combines the values in a and changes them into the value to display
       dataToDisplay(i) = dataToDisplay(i+1);
       end
   else
       dataToDisplay(t) = ((a(1)-96)*10)+(a(2)-80)+((a(3)-32)/10);
end

编辑:假设下面的评论是您正在尝试做的正确猜测,我会将您的代码更改为以下内容(请参阅我对%%%的说明):

data = []; %%%Renamed variable
numSamples = 500; %%%Set numSamples as a variable, so that if you need this number later you can just change it once
figure; %%%Start figure before loop
axis auto;
grid on;

for t=1:numSamples %Runs for 500 cycles %%% More standard to use for loops than rolling your own with while and incrementing

     for x = 1:3 %%% Unsure why you had this set as 1:4, since you didn't read the fourth value
        a(x) = fread(s); %reads 3 values of the data from the serial port and stores it to the matrix a
    end

    data(t) = ((a(1)-96)*10)+(a(2)-80)+((a(3)-32)/10); %Update current value of data

    plot(data(max(1,t-100):t),'-*r'); %%%Plot the most recent 100 values

    %%% Unnecessary, variable clears when for x=1:3 loop ends   x = 0;
    %%% Unnecessary, a is reassigned when assigned to fre   a=0;  %Clear the buffer
    drawnow;
end

通过这种方式,您可以使用普通for循环而不是自己创建循环,减少不必要的命令,这会使您的代码不易清晰且性能更差,并在最后保留整个“数据”以防您需要重新使用 - 引用它。

答案 1 :(得分:0)

图(数据(最大(1,T-100):T),&#39; - * R&#39); %%%绘制最近的100个值

这将绘制100个值然后滚动,以便数据移动到左边一个点,每个新数据添加到图表中,这完全是想要的欢呼声