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

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