My application reads data from sensors trough an ARDUINO UNO platform and then trough serial port I managed to read all the data that I need in MATLAB. so now I have 3 datas that I want to plot (data, data2, data3) in real time ON THE SAME GRAPHIC.
I also managed to plot one data at a time with some code I found on mathworks and modified it a bit, which does not suit my project.
Here is the matlab code that I'm using to plot one of the data:
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...');
I need to plot all the 3 datas (data, data2, data3) on the graphic with different colors. Please help me out here.
hold onfirst. Also, take a look at mathworks.com/help/matlab/ref/lineseriesproperties.html and focus onX/Y/Z/Data.