# Change plot color for increasing or decreasing x values

13 views (last 30 days)

Show older comments

### Accepted Answer

Joseph Cheng
on 12 Aug 2014

from what you described you don't really need a for loop to implement it. here is a solution to get away from a for loop

%my attempt to make something i think you're plotting.

%something like a Hysteresis curve.

y = [0 8 12 8 0 -8 -12 -8 0];

x = [2 10 10 8 -2 -10 -10 -8 2];

figure,plot(x,y)

%find which segments are heating and cooling or staying the same.

dx = diff(x); %takes x(n+1)-x(n)

heating = dx>0; %positive dx

steady = dx ==0; %0 dx (temp values didn't change)

cooling = dx<0; %negative dx.

plotSegmentX = [x(1:end-1);x(2:end)]; %pair the segments up.

plotSegmentY = [y(1:end-1);y(2:end)]; %pair the segments up.

%plot only the heating segments and color them red. then plot only the cooling etc.

figure,plot(plotSegmentX(:,heating),plotSegmentY(:,heating),'r',...

plotSegmentX(:,cooling),plotSegmentY(:,cooling),'b',...

plotSegmentX(:,steady),plotSegmentY(:,steady),'g')

xlim([-14 14]);ylim([-12 12])

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!