Signal pattern classification - To plot multi-colored 2D plots

조회 수: 5 (최근 30일)
krs.rb1
krs.rb1 2011년 9월 24일
Hi,
I am new to matlab, and i was trying signal processing. And i was trying to classify a certain signal based on certain range of values which indicated different states. Now to represent this classification i wanted to plot this vector with different colors at different states. But somehow, the entire plot changes color for -
x=sinewave;
for i=1:length(sinewave)
if x(i)>=0
plot(x,'r');
drawnow;
hold on;
else
plot(x,'b');
drawnow;
hold on;
end
refreshdata;
end
or the x axis of the plot changes for -
x=sinewave;
for i=1:length(sinewave)
if x(i)>=0
plot(x(i),'r');
drawnow;
hold on;
else
plot(x(i),'b');
drawnow;
hold on;
end
refreshdata;
end
I'd be grateful if anyone can kindly let me know if there is nay better way of doing this or if there is something that i am missing?

채택된 답변

Walter Roberson
Walter Roberson 2011년 9월 24일
Unfortunately, lineseries objects (such as are produced by plot()) and line objects (such as are produced by line()) can only have one color per object. If you want to color a line differently in different areas, then you have to break up the plotting so that the different segments are done in different calls.
In your second code, you are breaking it up point by point, but you are missing the implicit time axis, and should modify it to
plot(i,x(i),'r')
However, this is plotting a point at a time rather than a continuous line -- the points will not be joined.
If having the points not joined is what you want, then you will probably find using scatter() to be more efficient.
cols = 'br';
pointsize = 8; %adjust as needed
scatter(1:length(x), x, pointsize, cols(1 + (x(:)>=0)) );
  댓글 수: 4
Walter Roberson
Walter Roberson 2011년 9월 24일
Sure, scatter could be used within a for loop, with the same kind of rules as other graphics. The scatter command I show would do all of the plotting for x in a single shot, though.
Your refreshdata() call is not going to be useful in what you show; you might as well remove it.
Instead of using multiple if/elseif statements, use logical indexing. For example:
cols = 'rbck';
colidx = zeros(length(x),1);
colidx(x > 0) = 1;
colidx(x == 0) = 2;
colidx(x < 0) = 3;
colidx(x < -0.5) = 4;
scatter(1:length(x), x, pointsize, cols(colidx));
I do not understand your question about "while one vector is kept constant in that area" ?
Walter Roberson
Walter Roberson 2011년 9월 24일
mesh() generates a surface. surfaces can have different colors in different places. The property that controls this is named 'CData', documented for surface plots at: http://www.mathworks.com/help/techdoc/ref/surfaceplotproperties.html

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by