How to correct the grouping variables in splitapply ?
Show older comments
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
Stephen23
6 minutes ago
@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.
Answers (1)
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
Muazma Ali
2 minutes ago
Muazma Ali
less than a minute ago
Stephen23
2 minutes ago
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');
Categories
Find more on Cell Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!