필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Chandra Shekhar
Chandra Shekhar 2013년 2월 15일
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') ;
Chandra Shekhar
Chandra Shekhar 2013년 2월 19일
Thank You Cedric, now i got it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by