Designate order of categorical array?

8 views (last 30 days)
Alexis Gray
Alexis Gray on 7 Jun 2021
Commented: goc3 on 19 Aug 2023
I have a categorical array that I'm plotting on the y-axis against time. I need to designate that E_1 is greater than E_2, but it keeps plotting E_1 as the lower y-value. I tried the reverse function: ay.YAxis.Direction = 'reverse' but it doesn't seem to apply to categorical arrays.
y= ["E_1","E_1","E_2","E_2","E_2","E_2","E_2"] %y-values
y1 = categorical(y) %designate as categorical array
x= [-0.2, 0, 0.002, 0.01, 0.05, 0.25, 1] %x-values
plot(x,y1,'k',"LineWidth",1.5) %plot

Accepted Answer

dpb
dpb on 7 Jun 2021
You have to create an ordinal categorical array of the desired ordering of magnitudes to force other than the alphabetic ordering MATLAB uses by default.
valueset=fliplr(unique(y)); % generate order of values reverse alpha order
yOrd=categorical(y,valueset,'Ordinal',true); % create ordinal categorical with those values
Now the plot of yOrd versus x will have E1 above E2 on the y-axis.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!