Naming x-values in bar(x,y)

Julian on 23 Oct 2023
Latest activity Reply by Karol Ondrejkovic on 5 Feb 2024

I know the latest version of MATLAB R2023b has this feature already, put it should be added to R2023a as well because of its simplicity and convenience.
Basically, I want to make a bar graph that lets me name each column in a basic bar graph:
y=[100 99 100 200 200 300 500 800 1000];
x=["0-4" "5-17" "18-29" "30-39" "40-49" "50-64" "65-74" "75-84" "85+"];
However, in R2023a, this isn't a feature. I think it should be added because it helps to present data and ideas more clearly and professionally, which is the purpose of a graph to begin with.
Adam Danz
Adam Danz on 23 Oct 2023 (Edited on 23 Oct 2023)
@Julian, just convert your string to categoricals.
y=[100 99 100 200 200 300 500 800 1000];
x=["0-4" "5-17" "18-29" "30-39" "40-49" "50-64" "65-74" "75-84" "85+"];
Karol Ondrejkovic
Karol Ondrejkovic on 5 Feb 2024
@Adam Danz Thank you for the post. I just woder how do you flip the position of 5-17 category on the x-axis using:
bar(categorical(x,x), y)
instead of
bar(categorical(x), y)
Could you please provide a short comment on this?
Julian on 24 Oct 2023
Thanks, I didn't know you could do that, that's exactly what I was trying to do.
Kilsu Kim
Kilsu Kim on 23 Oct 2023
As you said, the feature specifyng bar labels as string vectors is introduced in R2023b. Please refer to this release note.
Bar Charts: Specify bar labels as string vectors
When you create bar charts using the bar and barh functions, you can specify the bar labels as string vectors. The bar tick labels appear in the order you specify them.
Unfortunately, newly introduced features that are not bug fixes will not be backported to previous releases.

See Also


No tags entered yet.