MATLAB: How to sort a categorical array containing merged categories

categoricalcategories;dataMATLABmergesorttype

If I create a categorical array that requires merging categories (as shown below – notice categories with values 2 and 3 both have the same label 'b'), the resulting categorical array cannot be sorted without error.
>>z = categorical([1 2 3 2 3 1]', [1 2 3]', {'a','b','b'}') 
>>sort(z) 
Error using categorical/sort (line 48) 
First argument of CATEGORICALSORT must be a full, real array of type uint8, uint16 or uint32. 
 

Best Answer

One quick workaround for this issue is to first create a normal categorical array then use the ‘mergecats’ function to merge the categories.
>>z = categorical([1 2 3 2 3 1]', [1 2 3]', {'a','b', 'c'})
>>z = mergecats(z,{'b','c'})
>>sort(z)