Matlab changes XAXIS order

18 views (last 30 days)
I have two simple matrixes: X is time of day in hours:
X[ 6 7 8 9 10 11 12 1 2 3 4 5 ]
Y[ various values...... ]
When I plot, MATLAB rearranges order of axis so x axis is:
[ 1 2 3 4 5 6 etc...}
I need MATLAB to plot in order as shown originally.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 11 Feb 2016
x=[ 6 7 8 9 10 11 12 1 2 3 4 5 ]
y=sin(x)
x1=1:numel(x)
plot(x1,y)
set(gca,'xtick',x1,'xticklabel',x)

More Answers (1)

Walter Roberson
Walter Roberson on 11 Feb 2016
cX = unwrap(X*pi/6)*6/pi;
plot(cX, Y);
The unwrap() is a trick to convert the 12-hour based clock times into continuous hour based clock times (so if you had several 12 hour periods the count would just keep increasing.)
You can use tick labels to change the labeling. For example,
X = repmat(1:24,1,3)/2;
cX = unwrap(X*pi/6)*6/pi;
Y = X.^3;
plot(cX, Y)
set(gca, 'XTick', cX(2:2:end), 'XTickLabel', X(2:2:end));
The 2:2:end is to select out only the exact hours out of the particular sample X values that here are by the half hour.

Categories

Find more on Dates and Time 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!