How to correct the grouping variables in splitapply ?

Hi
Kindly see my attached function. It is a callback function for a switch button
I have already converted both my grouping variables to categorical arrrays, still I am getting the message saying: "A grouping variable must be a vector."
I dont understand what more can be and should be done to the grouping variables.
value = app.Switch.Value;
if strcmpi(value, 'on')
weightpercentages = [app.Callingapp.Osmotisk_data.Weight_percent_best_salt_1 app.Callingapp.Osmotisk_data.Weight_percent_best_salt_2];
weightpercentages=categorical(weightpercentages);
Combo=categorical(app.Callingapp.Osmotisk_data.Combinations_of_salts);
[g,id1,id2]=findgroups(Combo, weightpercentages);
hold(app.UIAxes, 'on')
splitapply(@(x,y)scatter(app.UIAxes,x,y,'filled'),app.temperature,app.Callingapp.Total_water_activity,g)
legend(app.UIAxes,string(id1) + ", "+string(id2) ,'location','northwest')
xlabel(app.UIAxes,'Temperature'), ylabel(app.UIAxes,'Water activity')
end

1 Comment

@Muazma Ali: I notice that you have asked 134 questions on this forum, but have accepted very few of the answers that the volunteers have given you over the last few years. It is considered polite to accept the answer that best resolves your question, and is an easy way for you to show your appreciation to the volunteers who have helped you.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 about 2 hours ago
Edited: Stephen23 about 1 hour ago
The error is coming from this line:
weightpercentages = [app.Callingapp.Osmotisk_data.Weight_percent_best_salt_1 app.Callingapp.Osmotisk_data.Weight_percent_best_salt_2];
Concatenating with a space [A B] between two arrays concatenates them horizontally, thus you have created a 2-column matrix, not a vector. findgroups requires its grouping variables to be vectors (either row or column), so even after converting to categorical, the shape is wrong.
If they are column vectors use vertical concatenation with a semi-colon:
weightpercentages = [app.Callingapp.Osmotisk_data.Weight_percent_best_salt_1;... <- semi-colon!
app.Callingapp.Osmotisk_data.Weight_percent_best_salt_2];
If you do not know their orientation then use a single colon index to convert them into column vectors first:
weightpercentages = [app.Callingapp.Osmotisk_data.Weight_percent_best_salt_1(:);...
app.Callingapp.Osmotisk_data.Weight_percent_best_salt_2(:)];
You can always verify the shape before findgroups by checking:
disp(size(weightpercentages)) % Should be [N x 1] or [1 x N], NOT [N x 2]
How to create matrices and arrays using concatenation is explained in the documentation:

3 Comments

Thanks for the answer @Stephen23 but I dont think this approach will solve my problem; I thought I could have arrays as grouping variables, and my program requires that (the thing is that weightpercent_best_salt_1 is an array with weight percentages of one salt then I have the other arrray corresponding to the weight percentages of the other salt and I joined these two horizontally so I could display the weight percentages together with their respective salts in the string array called combinations of salts, as grouping variables in my plot.
is there any other way this can be done by keeping the arrays as they are?
Just supply them as separate arguments to findgroups, and MATLAB will group by their combination automatically:
Combo = categorical(app.Callingapp.Osmotisk_data.Combinations_of_salts);
W1 = app.Callingapp.Osmotisk_data.Weight_percent_best_salt_1(:);
W2 = app.Callingapp.Osmotisk_data.Weight_percent_best_salt_2(:);
[g, idCombo, idW1, idW2] = findgroups(Combo, W1, W2);
And then for the legend:
legendEntries = string(idCombo) + ": " + string(idW1) + "/" + string(idW2);
legend(app.UIAxes, legendEntries, 'location','northwest');

Sign in to comment.

Categories

Products

Asked:

on 9 Apr 2026 at 17:28

Edited:

on 9 Apr 2026 at 20:25

Community Treasure Hunt

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

Start Hunting!