MATLAB: How to use “gscatter” with incomplete group

categoricalcategories;gscatterMATLAB

I use the "gscatter" function as follow:
>> h1 = gscatter(X,Y,GroupVariable,Colors([5 5 7 7],:),'.x.x');
Ideally, "GroupVariable" can contain either 1, 2, 3 or 4, and I want to the data point to be plotted with "Color(5,:)" if it is from 1 or 2 and "Color(7,:)" if it is from group 3 or 4.
However, with the current data set I am working on, "GroupVariable" is a 10×1 vector that shows only two values (1 and 3 in mixed order)
This causes all points in the plot get the same color, instead of what I desired.

Best Answer

Defining "GroupVariable" as categorical variable with an explicit 'valueset' vector would resolve the issue:
>> valueset = [1 2 3 4];
>> GroupVariable = categorical(GroupVariable, valueset);
>> h1 = gscatter(X,Y,GroupVariable,Colors([5 5 7 7],:),'.x.x');
This would help MATLAB understand that even though you only have '1' and '3' in "GroupVariable", there are actually 4 different possible categories:
>> categories(GroupVariable)
You can refer to the following documentation page for more information about the "categorical" function that I used above: