How to use splitapply function with a grouping variable which is a cell array in my table?

9 views (last 30 days)
Hi ! :)
I have searched a little on the internet but I didnt find any information about how I can use splitapply function with one grouping variable which is supposed to be a cell array containing two strings on each row. I have to implement the code in app designer.
Thanks
  1 Comment
the cyclist
the cyclist on 12 Oct 2024
It would be incredibly easier to help you with this if you upload your table (or a small representative subset). You can use the paper clip icon in the INSERT section of the toolbar.

Sign in to comment.

Answers (3)

Venkat Siddarth Reddy
Venkat Siddarth Reddy on 12 Oct 2024
Edited: Venkat Siddarth Reddy on 12 Oct 2024
Hi Muazma,
To use the "splitapply" function with grouping variable as a cell array, you can convert the grouping variable into the format supported by the "splitapply" function (such as numeric or categorical array) and execute it.
Here is an example implementation on how to execute "splitapply" function when the grouping variable is a cell array containing two strings in each row:
% Sample data
data = rand(10, 1);
groupingVar = {'A', 'X'; 'A', 'Y'; 'B', 'X'; 'B', 'Y'; 'A', 'X'; 'A', 'Y'; 'B', 'X'; 'B', 'Y'; 'A', 'X'; 'A', 'Y'};
% Converting the cell array to a categorical array
groupingVarCat = categorical(cellfun(@(x,y) strcat(x, '_', y), groupingVar(:,1), groupingVar(:,2), 'UniformOutput', false));
% Function to be applied on each group
myFunction = @(x) mean(x);
% splitapply
result = splitapply(myFunction, data, findgroups(groupingVarCat));
% Results
disp(result);
0.3320 0.4287 0.6687 0.4477
To learn more about "splitapply" function, please refer to the following documentation:
I hope it helps!

Hitesh
Hitesh on 12 Oct 2024
Hi Muazma Ali,
You can follow below to steps to achieve grouping of variables which is a cell array containing two strings on each row using "splitapply function":
  • Concatenate Strings: Convert each row of the cell array into a single string using a delimiter such as "underscore" to uniquely represent each pair of strings.
  • Categorical Conversion: Transform the concatenated strings into a categorical array, which can be used as a grouping variable.
  • Apply Function: Use "splitapply" with the categorical grouping variable to perform the desired operation (e.g., sum) on the data column.
  • Integrate in App Designer: Implement this logic in a "callback" function within App Designer to process user-loaded data interactively.
Refer to the below code:
% Sample data setup
T = table({{'A', 'B'}; {'A', 'C'}; {'B', 'C'}; {'A', 'B'}}, [10; 20; 30; 40], 'VariableNames', {'GroupVar', 'Data'});
% Convert the cell array to a string representation
groupStrings = cellfun(@(x) strjoin(x, '_'), T.GroupVar, 'UniformOutput', false);
% Convert the strings to a categorical array
groupingVar = categorical(groupStrings);
% Define the function to apply
sumFunction = @(x) sum(x);
% Use splitapply to sum the data based on the grouping variable
result = splitapply(sumFunction, T.Data, findgroups(groupingVar));
% Display the result
disp(result);
For more information on "splitapply" function,refer to the below MATLAB documentation:

Matt J
Matt J on 13 Oct 2024
Edited: Matt J on 13 Oct 2024
groupingVar = {'A', 'X'; 'A', 'Y'; 'B', 'X'; 'B', 'Y'; 'A', 'X'; 'A', 'Y'; 'B', 'X'; 'B', 'Y'; 'A', 'X'; 'A', 'Y'};
G=findgroups(groupingVar(:,1), groupingVar(:,2));
result = splitapply(myFunction, data, G);

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!