How to rearrange categoricals to a specified sequence
1 view (last 30 days)
I am a Earth and Environmental Science student trying to do his group project and failing miserably.
I am trying to plot the frequency of the number of storms per month for a year.
I used tabulate to get the total number of storms oer month and then plot them using bar.
However, the months that are tabulated are in a disorganised arrangement and i would like to plot them following the correct monthly order.
I have tried listing them out as per the matlab instructions however, it keeps returning me that it is not a permutation of the categorical.
the categorical gives me a 12x1 categorical and the names of the months are spelled correctly
I have also tried using the function perms to calculate all possible permutations of the 12 months, however , matlab does not let me as it is too big of an output (42.8GB)
Pls send help :)
Campion Loong on 15 Nov 2019
The categories of your categorical array is sorted in alphabetical order.
% 'c' is your categorical array
12×1 cell array
Note that order of the categories is independent of the order of elements in the array itself:
12×1 categorical array
In this state, when you call bar with the categorical array and a numeric vector (i.e. your monthly rainfall), the visualization looks out of order:
% A naive example of 1:12
>> bar(c, 1:12)
To get the bars in desired order, reorder the categorical array's categories before calling bar:
>> months_in_order = ["January" "February" "March" "April" "May" "June" "July" "August" "September" "October" "November" "December"];
>> c = reordercats(c, months_in_order);
>> bar(c, 1:12);