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

1 次查看(过去 30 天)
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

采纳的回答

Cedric
Cedric 2013-2-16
编辑:Cedric 2013-2-16
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 个评论
Cedric
Cedric 2013-2-16
编辑:Cedric 2013-2-16
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') ;

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by