MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by S Tajik on 10 Feb 2011

I've got a live stream of data coming to MATLAB. MATLAB does all the calculations and plotting just fine. The only problem is the colour of the plot lines.

I would like to have the plot line change colour automatically when is above or below a set number.

For example if my data is above 0 the plot line would be green and if below 0 the plot line would be red. So after a while when many data points were generated and plotted on the MATLAB figure, I'd like to see all lines above 0 to be green and all lines below 0 in red.

Answer by Matt Fig on 10 Feb 2011

Perhaps you mean something like this:

x = -10:.01:10; y = sin(x); idx = y<=0; plot(x(idx),y(idx),'r*',x(~idx),y(~idx),'b*')

**EDIT**

Simulate data coming in one piece at a time. Use a FOR loop as the simulated data retrieval mechanism.

x = -10:.01:10; for ii = 1:length(x) y(ii) = sin(x(ii)); % Data point ii has come in.

if y(ii)<0 c = 'r*'; else c = 'b*'; end

plot(x(ii),y(ii),c) hold on end

Show 1 older comment

Matt Fig on 10 Feb 2011

(Thanks Doug!)

That would depend on how the data is coming in. If it is coming in one piece at a time, do like in my edit above.

S Tajik on 10 Feb 2011

data coming in live (numerical) and then used to do some calculations on it (find the high and low of a certain period).

once the calculations are done all the NEW data are stored in vectors. and feeded into the plot diagram. Ive been trying to use your method to do so but so far nothing. in your example you used y value as a condition, what I need is as follow:

if price>myIndicator

plot price green

if price<myIndicator

plot price red

hope that explains a bit more

thanks for the help.

Answer by Seth DeLand on 10 Feb 2011

Here's a way to do it that splits the data up into 2 lines and then plots them both. Overlapping points are set to NaN so that they are not plotted.

% Your Data

x = 0:0.01:10; y = sin(x);

% Level for Color Change

lev = 0.2;

% Find points above the level

aboveLine = (y>=lev);

% Create 2 copies of y

bottomLine = y; topLine = y;

% Set the values you don't want to get drawn to nan

bottomLine(aboveLine) = NaN;

topLine(~aboveLine) = NaN;

plot(x,bottomLine,'r',x,topline,'g');

Show 1 older comment

S Tajik on 10 Feb 2011

thanks for all your help.

myx values are not stored in any vector as they can get very large and damp up the memory. they are just normal numeric data 1, 2 ,3 ,4 and so on. used as index.

my y values are however stored in a vector. when I use the solutions provided on this page I get the graph i want but it only plots dots and do not connect them together. I assume that is because my x value is not a vector.

any inputs will be much appreciated.

cheers

Answer by Vieniava on 10 Feb 2011

To control colour of any segment of plot you should use `line()`

>> doc line

Walter Roberson on 10 Feb 2011

That will not help any more than using plot(). line() has the same limitation, that any one line segment must be a single color.

Answer by Matt Tearle on 11 Feb 2011

Maybe this is going overboard, but given your comment "but it only plots dots and do not connect them together", I'm guessing you're getting a lot of crossings of the cutoff value. So... here's a brute-force approach that actually interpolates between the crossings.

Because of what you said about size and memory, I tried to make it so that memory wouldn't be an issue. (So the code isn't as pretty as it probably could be.)

x = 0:0.1:10; y = sin(4*x); clr = {'b','r'}; lev = -0.35;

n = length(y); idx = [0,find(sign(y(2:end)-lev)~=sign(y(1:end-1)-lev)),n]; nidx = length(idx);

for k=2:nidx x1 = idx(k-1)+1; x2 = idx(k); line(x1:x2,y(x1:x2),'color',clr{mod(k,2)+1},'marker','o') if k<nidx x1 = x2; x2 = x1+1; y1 = y(x1); y2 = y(x2); x0 = x1 + (lev-y1)/(y2-y1); y0 = y1 + (x0-x1)*(y2-y1); line([x1,x0],[y1,y0],'color',clr{mod(k,2)+1}) line([x0,x2],[y0,y2],'color',clr{mod(k+1,2)+1}) end end

Answer by Matt Tearle on 22 Feb 2011

Because I'm a dork, I made a function to do this. It's now available on File Exchange. You can contact me to find out where to send generous tips!

## 1 Comment

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/1156#comment_199940

I like to mark in a stream time series data when data value equals to a threshold. Whenever such value is reached in a streaming time series plot the marker will appear and then streaming will go on with the marker. I have modified code from http://www.mathworks.in/matlabcentral/answers/87466-real-time-plot-from-streaming-data as:

t = 0 ; x = 0 ; startSpot = 0; interv = 1000 ; % considering 1000 samples step = 0.1 ; % lowering step has a number of cycles and then acquire more data while ( t <interv ) b = sin(t)+5; x = [ x, b ]; plot(x) ; if ((t/step)-500 < 0) startSpot = 0; else startSpot = (t/step)-500; end axis([ startSpot, (t/step+50), 0 , 10 ]); grid t = t + step; drawnow; pause(0.01) end

But this is not working as intended????