MATLAB Answers

0

How to plot 3 different values in single plot in real time.

Here i need to read data from serial port, for plotting ECG waveform in real time. From that serial data, i need to create 2 more values. like serial data=x
1st value=x; 2nd value=x-50; 3rd value=x-20;
so for these 3 values i need to add different colors ('r','g','b') in single plot that to in real time.
In below code i can able to receive one value from serial port and plotted in real time.
obj = serial('COM1','BaudRate',9600); %creating the object
fopen(obj); %open port
set(obj,'terminator','cr') %providing the terminator
a1=0; % a variable for y axis
time1=now; % time is in x axis
liH=line(NaN,NaN);
while 1 % infinite loop
time2=now;
x=[time1 time2];
ip_data = fscanf(obj);
a2= str2num(ip_data) ;
a=[a1 a2];
line(x,a);
set(liH,'XData',[get(liH,'XData') x]);
set(liH,'YData',[get(liH,'YData') a]);
datetick('x','HH:MM') %change the axis
% pause(0.5);
a1=a2;
time1=time2;
end
delete(instrfindall)
Here i am plotting correctly for single value, then how to change colors of each value and how to plot 3 different values in single plot.
Any suggestions please..?
Thank You

  0 Comments

Sign in to comment.

1 Answer

Answer by Cedric Wannaz
on 16 Feb 2013
Edited by Cedric Wannaz
on 16 Feb 2013
 Accepted Answer

Using hold on allows you to plot consecutively on the same figure without erasing the previous content. E.g.
figure(1) ;
clf ;
hold on ;
plot(sin(0:.1:10), 'r') ;
plot(cos(0:.1:10), 'g') ;
I don't fully understand why you want to change the color for each line, but you can do this using the ColorSpec parameter, generating the RGB triple with some mathematical function of your own:
line([0, 100], [-1, 1], 'Color', [1,0.4,0]) ;

  3 Comments

Thank you Cedric..
here i am receiving serial data continuously like x=[23 24 56...], first it receive 23 then 24 so on..
ones it receives first value of x, then i need to make two more values like this y=x-10 and z=x+50 for every received value.
then i need to plot all these 3 values simultaneously in single plot. For each value i need to assign different colors to differentiate between these values.
Please can you tell me what to modify in above code for these 3 values..
Keeping your approach and notation, you would have to have your code start with
figure(1) ;
clf ;
hold on ;
and then in the loop, replace the line(x,a) call with e.g.
line(x, a, 'b') ;
line(x, a+50, 'r') ;
line(x, a-10, 'g') ;
Thank You Cedric, now i got it.

Sign in to comment.