让 Matlab 绘图滚动
Getting Matlab plot to scroll
我正在尝试绘制从微控制器发送的串口数据,解释该数据并绘制图表。数据将以非常快的速度出现(每 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 个显示的是数字,而第四个显示的是目前 matlab 代码中不需要的单位。
您的代码因假定的拼写错误而中断:
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 循环而不是自己制作循环,减少不必要的命令,这些命令会使您的代码更不清晰且性能更差,并在最后保留整个 "data" 以防万一你需要重新引用它。
plot(数据(max(1,t-100):t),'-*r'); %%%绘制最近的 100 个值
这将绘制 100 个值,然后滚动,以便数据在图表中添加每个新数据时向左移动一个位置,这正是我们想要的欢呼声
我正在尝试绘制从微控制器发送的串口数据,解释该数据并绘制图表。数据将以非常快的速度出现(每 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 个显示的是数字,而第四个显示的是目前 matlab 代码中不需要的单位。
您的代码因假定的拼写错误而中断:
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 循环而不是自己制作循环,减少不必要的命令,这些命令会使您的代码更不清晰且性能更差,并在最后保留整个 "data" 以防万一你需要重新引用它。
plot(数据(max(1,t-100):t),'-*r'); %%%绘制最近的 100 个值
这将绘制 100 个值,然后滚动,以便数据在图表中添加每个新数据时向左移动一个位置,这正是我们想要的欢呼声