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

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?

 Risposta accettata

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 Commenti

oh! Okay, Thank you. So i have to create separate vectors for each area while one vector is kept constant in that area? that would add an extra line in the plot. But then again it would be better than the twinkling plot i have right now. Thanks again.
Oh, by the way, i was trying this after i saw examples of 3D plots in which there are different colors for different regions of the same plot. (mesh??) How is that achieved?
Oh! I Will try Scatter. However, can scatter be used inside a for loop? also, for multiple if/elseif statements?
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" ?
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

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by