conditional colouring graph problem
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Chad Greene
2015-1-27
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 个评论
Chad Greene
2015-1-27
编辑:Chad Greene
2015-1-27
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
更多回答(2 个)
Chad Greene
2015-1-27
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');
Kelly Kearney
2015-1-28
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]);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
