【发布时间】:2014-07-23 12:36:07
【问题描述】:
我的应用程序通过 ARDUINO UNO 平台从传感器读取数据,然后通过串行端口,我设法在 MATLAB 中读取了我需要的所有数据。所以现在我有 3 个数据要实时绘制(数据、数据 2、数据 3)在同一张图上。
我还设法使用我在 mathworks 上找到的一些代码一次绘制一个数据并对其进行了一些修改,这不适合我的项目。
这是我用来绘制其中一个数据的 matlab 代码:
clear
clc
%User Defined Properties
serialPort = 'COM7'; % define COM port #
baudeRate = 115200;
plotTitle = 'Serial Data Log'; % plot title
xLabel = 'Elapsed Time (s)'; % x-axis label
yLabel = 'Data'; % y-axis label
plotGrid = 'on'; % 'off' to turn off grid
min = -200; % set y-min
max = 200; % set y-max
scrollWidth = 10; % display period in plot, plot entire data log if <= 0
delay = .01; % make sure sample faster than resolution
%Define Function Variables
time = 0;
data = 0;
data2 = 0;
data3 = 0;
count = 0;
%Set up Plot
plotGraph = plot(time,data,time,data2,time,data3);
title(plotTitle,'FontSize',25);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
axis([0 10 min max]);
grid(plotGrid);
%Open Serial COM Port
s = serial(serialPort, 'BaudRate',baudeRate)
disp('Close Plot to End Session');
fopen(s);
tic
while ishandle(plotGraph) %Loop when Plot is Active
dat = fscanf(s,'%f'); %Read Data from Serial as Float
if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct
count = count + 1;
time(count) = toc; %Extract Elapsed Time
data(count) = dat(1); %Extract 1st Data Element
data2(count) = dat(2);
data3(count) = dat(3);
data(count);
data2(count);
data3(count);
%Set Axis according to Scroll Width
if(scrollWidth > 0)
set(plotGraph,'XData',time(time > time(count)-scrollWidth),'YData',data3(time > time(count)-scrollWidth));
%plot(time(time > time(count)-scrollWidth),data3(time > time(count)-scrollWidth));
axis([time(count)-scrollWidth time(count) min max]);
%set(plotGraph,'XData',time(time > time(count)-scrollWidth),'YData',data3(time > time(count)-scrollWidth));
%axis([time(count)-scrollWidth time(count) min max]);
else
set(plotGraph,'XData',time,'YData',data);
axis([0 time(count) min max]);
end
%Allow MATLAB to Update Plot
pause(delay);
end
end
%Close Serial COM Port and Delete useless Variables
fclose(s);
clear count dat delay max min baudRate plotGraph plotGrid plotTitle s ...
scrollWidth serialPort xLabel yLabel;
disp('Session Terminated...');
我需要用不同的颜色在图形上绘制所有 3 个数据(data、data2、data3)。请帮帮我。
【问题讨论】:
-
先试试
hold on。另外,看看mathworks.com/help/matlab/ref/lineseriesproperties.html 并关注X/Y/Z/Data。 -
坚持不起作用
标签: matlab plot serial-port arduino