reordercats() confusion.
Show older comments
Hello,
Example data attached.
When selecting and assigning a portion of this table to another variable, MATLAB seems to remember that there were additional categories not included in the new table.
load("exampletbl.mat")
categories(a.name) % there are 15 categories
selection = a(a.type == "i",:) % select a portion of original table
categories(selection.name) % shows there to still be 15 cateogires
unique(selection.name) % when there should be 9....
Why are there still 15 categories?
I then wish to reorder the new categoires (the order of which I determine from data in other columns)
selection.name = reordercats(selection.name, {'K','I','H','G','F','E','D','C','B'})
Returns the following:
% ERROR using categorical/reorder cats (line 38)
% NEWORDER MUST BE A PERMUTATION OF THE EXISITNG CATEGORIES
It is a permutation of the exisitng categories.
However,
% if i include ALL 15 categories in "a"
selection.name = reordercats(selection.name,{'K','I','H','G','F','E','D','C','B','A','J','L','M','N','O'})
% this works
categories(selection.name) % but there are still 15
unique(selection.name) % 9 - in the correct order.
In the dataset that I am using (more columns, more categories and more rows than example given here) I am often creating a "selection" table based on multiple criteria, I then wish to reorder the categories based on another column, or two, so that when plotting on a categorical axis they are in the desired order. Should I in fact be sorting the complete table (here, "a") based on my criteria first, then reordering the categories, and then creating a "selection" table?
If anyone can shed light
I hope the above makes sense.
Thank you,
Accepted Answer
More Answers (0)
Categories
Find more on Categorical Arrays 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!