MATLAB Answers

Change plot color for increasing or decreasing x values

16 views (last 30 days)
Matt
Matt on 12 Aug 2014
Commented: Matt on 13 Aug 2014
Hi all...I'm pretty inexperienced with MATLAB so bear with me. I am trying to import a data set from a .txt file which has many cycles along the x-axis. Since the x-axis is temperature, I would like the increasing segment of each cycle curve (heating) to be red and the decreasing segment (cooling) to be blue. I'm not experienced enough to write a FOR loop to implement this, and I was unable to find a solution on the forums. Any help is much appreciated. Thanks!

Accepted Answer

Joseph Cheng
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])

  3 Comments

Matt
Matt on 13 Aug 2014
Thank you for the response...I am indeed plotting hysteresis loops! The code works with your example x,y but when I insert my data it gives me an error: index exceeds matrix dimensions on the second plot command (labeled with * * *). My x and y are both 8046x1 double and the output heating and cooling vectors are 8045x1. I'm just not seeing how to resolve this in the code. Thanks again for taking the time to help...my MATLAB knowledge is rubbish and this was needed in a hurry.
%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];
y = MTS810TrueStrain;
x = SampleTemp;
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])
Joseph Cheng
Joseph Cheng on 13 Aug 2014
What i can see it is because of how i defined my x and y and your x and y. my xy values are 1xN and yours is Nx1. so when i did the plotsegmentX and Y we needed it to be 2x(N-1). So with the code i would change it to be:
plotSegmentX = [x(1:end-1) x(2:end)]'; %pair the segments up.
plotSegmentY = [y(1:end-1) y(2:end)]'; %pair the segments up.
that should work with your x and y. That way when the plot function is called it plots the first column of plotSegmentX and first column of plotSegmentY together (etc down the rest of the columns.).
Matt
Matt on 13 Aug 2014
that did the trick. everything looks great, except there is some noise in the data that causes small blue segments on the heating curve. is there a way to take the difference between two non-consecutive values? such as x(n+2)-x(n)? i think this would filter out the noise.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!