How do you split a table into sub-tables based on entries in a specific column?

288 views (last 30 days)
How do you split a table into sub-tables based on entries in a specific column? Essentially, I want to divide my table data into groups, determined by one of its values.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 13 Oct 2022
Edited: MathWorks Support Team on 13 Oct 2022
I have created a simple example which demonstrates how to split a table based on the first column as the category variable.
The script is also attached as "test_script.m". Please read the comments to get a better understanding of how to use the script for your use case.
% Patient pateint data
load patients
% Create test table
T = table(Gender(1:5), Height(1:5), 'VariableNames', {'Gender', 'Height'});
% Group based on first column - gender column
% Modify 1 to appropriate column number
G = findgroups(T{:, 1});
% Split table based on first column - gender column
T_split = splitapply( @(varargin) varargin, T , G);
% Allocate empty cell array fo sizxe equal to number of rows in T_Split
subTables = cell(size(T_split, 1));
% Create sub tables
for i = 1:size(T_split, 1)
subTables{i} = table(T_split{i, :}, 'VariableNames', ...
T.Properties.VariableNames);
end
% Display Results
disp('Full Table:');
disp(T);
disp('Sub Table 1:');
disp(subTables{1});
disp('Sub Table 2:');
disp(subTables{2});
See this documentation for another example of using 'splitapply':
Here is a list of the functions I am using:

More Answers (1)

yogan Sganzerla
yogan Sganzerla on 13 Jul 2021
Edited: yogan Sganzerla on 13 Jul 2021
It didn't helped me, so I will try to write down one example:
I have the following table:
[ 14-Jul-2021 20:36:59 1
14-Jul-2021 20:37:59 1
14-Jul-2021 20:38:59 1
14-Jul-2021 20:38:59 2
14-Jul-2021 20:40:59 2
14-Jul-2021 20:41:59 2
14-Jul-2021 20:42:59 2
14-Jul-2021 20:43:59 2
14-Jul-2021 20:44:59 1
14-Jul-2021 20:45:59 1
14-Jul-2021 20:46:59 2]
baisc, the values in the first column is datatime increasing 1 minut per line and the second colum can be just 1 or 2.
My goal is to breake this table into 2 table:
Table1= [ 14-Jul-2021 20:36:59 14-Jul-2021 20:38:59 1
14-Jul-2021 20:44:59 14-Jul-2021 20:46:59 1]
Table1= [ 14-Jul-2021 20:38:59 14-Jul-2021 20:44:59 2
14-Jul-2021 20:46:59 14-Jul-2021 20:47:59 2]
The first group of "1" start at 14-Jul-2021 20:36:59 and just finishe when the "2" starts so 14-Jul-2021 20:38:59
The same with the group 2 that starts at 14-Jul-2021 20:38:59 and finishes when the other group "1" starts (14-Jul-2021 20:44:59 )
and so on, until the lsat one, that we just add 1 minut because this is the duration on the signal

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!