Split a Table at every nth row
38 views (last 30 days)
Show older comments
I have a table that is approximately 96,000 lines long, i need to split it into tables that are approximately 28,000 lines long, how would i go about doing this?
1 Comment
Stephen23
on 17 Oct 2023
Note that in general splitting up data makes it harder to work with.
MATLAB has plenty of functions and tools that identify and process groups of data within tables:
Answers (2)
the cyclist
on 17 Oct 2023
Here is one way:
% Make up a table
var1 = rand(96000,1);
var2 = rand(96000,1);
tbl = table(var1,var2);
% Define the smaller table size
smallSize = 28000;
% Break it up, storing each smaller table in a cell array
numberTables = ceil(height(tbl)/smallSize);
smallTables = cell(numberTables,1);
for nt = 1:numberTables-1
indexToThisChunk = ((nt-1)*smallSize+1):(nt*smallSize);
smallTables{nt} = tbl(indexToThisChunk,:);
end
smallTables{numberTables} = tbl((numberTables-1)*smallSize+1:end,:);
0 Comments
Voss
on 17 Oct 2023
T = array2table(rand(96014,10)) % approximately 96000 rows in the table
One approach: Split T into tables that are exactly 28000 rows long, plus another smaller table for the leftover rows at the end:
n_rows = 28000;
n_tables = ceil(size(T,1)/n_rows);
C = cell(n_tables,1);
for ii = 1:n_tables
C{ii} = T((ii-1)*n_rows+1:min(ii*n_rows,end),:);
end
disp(C);
Another approach: Split T into (almost) equally-sized tables, each with as close to 28000 rows as you can get:
n_rows_target = 28000;
n_rows_T = size(T,1);
n_tables_min = floor(n_rows_T/n_rows_target);
n_tables_max = ceil(n_rows_T/n_rows_target);
n_rows_max = n_rows_T/n_tables_min;
n_rows_min = n_rows_T/n_tables_max;
if n_rows_max - n_rows_target > n_rows_target - n_rows_min
n_rows = floor(n_rows_min);
n_tables = n_tables_max;
else
n_rows = floor(n_rows_max);
n_tables = n_tables_min;
end
n_rows_extra = n_rows_T - n_rows*n_tables;
n_rows = n_rows * ones(1,n_tables);
n_rows(1:n_rows_extra) = n_rows(1:n_rows_extra) + 1;
start_idx = 1 + cumsum([0 n_rows]);
C = cell(n_tables,1);
for ii = 1:n_tables
C{ii} = T(start_idx(ii):start_idx(ii+1)-1,:);
end
disp(C);
2 Comments
Voss
on 19 Oct 2023
In both approaches, the tables are stored in the cell array C.
You can write each table to a different sheet of a xls file like this:
output_file = 'tables.xls';
for ii = 1:numel(C)
writetable(C{ii},output_file,'Sheet',ii);
end
See Also
Categories
Find more on Tables 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!