MATLAB Answers

Is it possible to assign new values which are integers to categorical array?

9 views (last 30 days)
Rohit Deshmukh
Rohit Deshmukh on 12 Mar 2020
Edited: dpb on 12 Mar 2020
I want to change the values of categorical array, so that i can create figure from plots.I want to convert the plot into figure. For which i want app.UIAxes.XLim to be numerical not categorical. For example: currently-
app.UIAxes.XLim = 1×2 categorical array -> urban freeway
I want
app.UIAxes.XLim = 1x2 Matrix 2 6

  5 Comments

Show 2 older comments
Rohit Deshmukh
Rohit Deshmukh on 12 Mar 2020
i can plot them, but i want to convert the plot into figure. For which i want app.UIAxes.XLim to be numerical not categorical. For example: currently-
app.UIAxes.XLim = 1×2 categorical array -> urban freeway
I want
app.UIAxes.XLim = 1x2 Matrix 2 6
Walter Roberson
Walter Roberson on 12 Mar 2020
You cannot convert an existing categorical ruler to numeric ruler. If you have a categorical ruler and need numeric ruler it is typically easiest to destroy the entire axes and recreate it.
In theory you could also just replace the ruler property in the axes. However there are No User Serviceable Parts for creating rulers, so you would have to create another axes with the kind of ruler that you wanted and copy that on top of the existing ruler property.
Walter Roberson
Walter Roberson on 12 Mar 2020
Traditional figures support categorical rulers by the way, no need to switch to numeric, at least not for the last few years.

Sign in to comment.

Answers (2)

Ameer Hamza
Ameer Hamza on 12 Mar 2020
findgroups is one way to convert categorical array to numeric
numeric = findgroups(categorical_array);

  6 Comments

Show 3 older comments
Ameer Hamza
Ameer Hamza on 12 Mar 2020
Sorry, I forgot to mention that you can do this with simple variable. However, app.UIAxes.XLim is a property of categorical ruler class, which cannot be simply converted to numeric ruler. Your best bet is to do conversion before plot() function.
Check Walter’s comment above for more detail. He also proposed a solution.
Personally, I would prefer converting to numeric array before plot(). But if I really had no other choice, I would hide the ruler of current axes and create a new axes on top of previous axes with a numeric ruler. I am sure this will work on simple axes create in a figure(), but I am not sure whether this will work in app designer. I will try this in a while and post here if it works.
Walter Roberson
Walter Roberson on 12 Mar 2020
app.UIAxes.XLim = double(app.UiAxes.XLim)
The double will retrieve the numeric encoding associated with the categorical values.
However rulers know the data type the ruler was constructed with and will either refuse to make the change or will convert the values back to the datatype associated with the ruler.
Categorical rulers are a different graphics object than numeric rulers. You cannot change to a numeric ruler just by changing the xlim: you would need to replace the entire ruler.
dpb
dpb on 12 Mar 2020
"but i want to convert the plot into figure"
See Walter Roberson's comment above.
If that's not the issue, then explain the reasons behind the request. Doesn't seem rational on the face of it to replace the meaningful categorical names with numbers.

Sign in to comment.


dpb
dpb on 12 Mar 2020
Edited: dpb on 12 Mar 2020
See the above comment, but if there is some reason you really, really need/want the numbers instead on the axes, then
hAx=gca;
hAx.XTickLabel=['{2}';'{6}'];
Alternatively, create the category names as numeric when create the categorical variable and those will be what shows up on the axes.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by