Reorder x-axis bar graph categorical

23 vues (au cours des 30 derniers jours)
Mark Crook-Rumsey
Mark Crook-Rumsey le 27 Nov 2019
Modifié(e) : Adam Danz le 20 Sep 2023
Hi all,
I have viewed some of the other suggestions on here, but none have worked for me and I'm unsure where I am going wrong. I am trying to reorder a bar graph to be in the order I have specified in my variable cats.
errlow_ERP = [young_mid_parietal_PMconcept_stderr_ERP old_mid_parietal_PMconcept_stderr_ERP MCI_mid_parietal_PMconcept_stderr_ERP];
ERP_mean = [young_mid_parietal_PMconcept_mean_ERP old_mid_parietal_PMconcept_mean_ERP MCI_mid_parietal_PMconcept_mean_ERP];
errhigh_ERP = [young_mid_parietal_PMconcept_stderr_ERP old_mid_parietal_PMconcept_stderr_ERP MCI_mid_parietal_PMconcept_stderr_ERP];
cats = categorical({'Young','Older adult','MCI'});
subplot(3,4,12)
hold on;
p = bar(cats,ERP_mean,...
'FaceColor',[0.75,0.75,0.75],...
'EdgeColor','k',...
'LineWidth',1.5,...
'BaseValue',0);
baseline = p.BaseLine;
er = errorbar(cats,ERP_mean,errlow_ERP,errhigh_ERP,'.','vertical');
set(gca,'LineWidth',1,'FontSize',14)
ylim([0 8.5])
er.LineWidth = 1.5;
er.Color = 'k';
er.MarkerSize = 1;
xlabel('Group','FontSize',16)
ylabel('Amplitude in \muV','FontSize',16)
Any advice is greatly appreciated!

Réponse acceptée

Adam Danz
Adam Danz le 27 Nov 2019
Modifié(e) : Adam Danz le 20 Sep 2023
Starting in MATLAB R2023b, bar accepts string arrays in addition to categoricals in the x-argument. The bar order will maintain the string order.
str = ["Young","Older adult","MCI"];
y = 1:3;
bar(str, y)
For releases prior to R2023b, you can reorder the categories using reordercats().
I know it doesn't look intuitive because the order specified by reordercats is the same as the original order but this sets that order in stone.
figure
cats = categorical({'Young','Older adult','MCI'});
cats = reordercats(cats,{'Young','Older adult','MCI'});
p = bar(cats, y);

Plus de réponses (0)

Catégories

En savoir plus sur Labels and Annotations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by