conditional colouring graph problem

2 views (last 30 days)
Hey, I have a problem with changing the colours on my graph conditionally; my function is as follows
function [t,vx,vy] = Q3_1400000(rocket,brake)
[t,ax,ay] = getAcceleration(rocket,brake);
vx = 0; vy = 0;
for n = 1:999;
vx(n+1) = vx(n) + ax(n+1);
vy(n+1) = vy(n) + ay(n+1);
k = 1:length(vx);
if vy(k) > vx(k)
c = 'r';
else
c = 'b';
end
end
plot(t,vx(k),c);
hold on
grid on
plot(t,vy(k),c);
xlabel('Time (s)');
ylabel('Velocity (m/s)');
title('\Velocity data for the rocket sled');
legend({'v_x','v_y'})
end
For some reason when the graph plots, the colour will always be the ' else ' colour and won't change at all. Does anybody know the solution to this please ?
Thanks

Accepted Answer

Chad Greene
Chad Greene on 27 Jan 2015
It looks like you're only testing the condition of whether the very last value in vy is bigger than the very last value in vx because each time it goes through the loop it's overwriting c.
  3 Comments
Chad Greene
Chad Greene on 27 Jan 2015
Edited: Chad Greene on 27 Jan 2015
It's hard to understand exactly what you're trying to do. Perhaps the example below will help. It puts a red circle around all the black y1 points that are bigger than their corresponding blue y2 points:
x = 1:10;
y1 = rand(size(x));
y2 = rand(size(x));
plot(x,y1,'kx',x,y2,'b+')
hold on
% indices of y1 values that are bigger than y2:
ind = y1>y2;
plot(x(ind),y1(ind),'ro')
box off
Thomas Plank
Thomas Plank on 27 Jan 2015
Sorry for not being clear; what I intended to do was plot 2 lines on the same graph, vy and vx. When vy is greater than vx I wanted to change the colour of the lines to indicate that vy > vx.

Sign in to comment.

More Answers (2)

Chad Greene
Chad Greene on 27 Jan 2015
First create a logical array the size of vx and vy. It will have a 1 everywhere vy is bigger than vx, and a zero everywhere else:
vybigger = vy>vx;
Plot vx and vy, both in red where vy is bigger than vx:
plot(t(vybigger),vy(vybigger),'r');
hold on
plot(t(vybigger),vx(vybigger),'r');
Plot the rest of the data in blue:
plot(t(~vybigger),vy(~vybigger),'b');
plot(t(~vybigger),vx(~vybigger),'b');
  1 Comment
Thomas Plank
Thomas Plank on 28 Jan 2015
Edited: Thomas Plank on 28 Jan 2015
Thanks ! This is almost exactly the effect I wanted; however for some reason it some some odd lines on it ( at the start from 0 - 10 on the x axis and the line inbetween where vy>vx shown as a thick purple colour ) and I'm not entirely sure why.
I have inserted the image below and the coding for the colour change is this if it makes a difference.
plot(t(vybigger),vy(vybigger),'g');
hold on
plot(t(vybigger),vx(vybigger),'m');
plot(t(~vybigger),vy(~vybigger),'r');
plot(t(~vybigger),vx(~vybigger),'b');
Thanks
EDIT: I have got rid of the lines from 1-10 however I still have the problem with the line inbetween vy>vx and vy<vx

Sign in to comment.


Kelly Kearney
Kelly Kearney on 28 Jan 2015
An alternative method to Chad's (which works nicely if there's only one color switch, but may need some modification if there are multiple switches and you want solid lines) is to use patches with interpolated edge color:
t = linspace(0,2*pi,100);
vx = cos(t);
vy = sin(t);
vybigger = vy > vx;
hold on;
h(1) = patch([t NaN], [vx NaN], [vybigger NaN]);
h(2) = patch([t NaN], [vy NaN], [vybigger NaN]);
set(h, 'edgecolor', 'interp');
colormap([0 0 1; 1 0 0]);

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!