How to sort cell array of tables by size and concatenate vertically
8 views (last 30 days)
Show older comments

A cell array consists of individual tables of different sizes. It is to be sorted by size (along row) and then tables (along row) are to be concatenated vertically.
(Note: Sort command with cellfun is not working as reported in earlier versions).
0 Comments
Answers (1)
BhaTTa
on 5 Jun 2025
Hey @Mukund, Please refer to the below code that meets your requirement, please take it as a reference and modify it accordingly:
function concatenatedTable = sortAndConcatenateTables(cellArrayOfTables)
% sortAndConcatenateTables Sorts a cell array of tables by row count
% and concatenates them vertically.
%
% Input:
% cellArrayOfTables - A cell array where each cell contains a table.
%
% Output:
% concatenatedTable - A single table resulting from vertical
% concatenation of the sorted tables.
% --- Input Validation (Optional but good practice) ---
if ~iscell(cellArrayOfTables) || isempty(cellArrayOfTables)
error('Input must be a non-empty cell array.');
end
if ~all(cellfun(@istable, cellArrayOfTables))
error('All elements in the cell array must be tables.');
end
% --- Get the number of rows for each table ---
% We'll use a loop since cellfun with 'size' directly on tables can be tricky
% or less readable for specific dimension extraction.
numRows = zeros(length(cellArrayOfTables), 1);
for i = 1:length(cellArrayOfTables)
if ~isempty(cellArrayOfTables{i})
numRows(i) = height(cellArrayOfTables{i}); % 'height' is preferred for tables
else
numRows(i) = 0; % Handle empty tables if necessary
end
end
% --- Sort the tables based on the number of rows ---
[~, sortOrder] = sort(numRows);
sortedCellArray = cellArrayOfTables(sortOrder);
% --- Vertically concatenate the sorted tables ---
% Check if there's anything to concatenate
if isempty(sortedCellArray)
concatenatedTable = table; % Return an empty table
return;
end
% Ensure all tables have the same variable names for concatenation
% This is crucial for vertcat. If variable names differ, vertcat will error.
% We'll assume the first table's variable names are the standard.
% More robust error handling or name harmonization could be added here.
if ~isempty(sortedCellArray) && ~isempty(sortedCellArray{1})
referenceVarNames = sortedCellArray{1}.Properties.VariableNames;
for i = 2:length(sortedCellArray)
if ~isempty(sortedCellArray{i}) && ...
~isequal(sortedCellArray{i}.Properties.VariableNames, referenceVarNames)
warning('Table %d has different variable names. Concatenation might fail or produce unexpected results.', i);
% Optional: Add code here to rename variables or skip the table
end
end
end
% Perform the concatenation
concatenatedTable = vertcat(sortedCellArray{:});
% --- Display the result (Optional) ---
disp('Concatenated Table:');
disp(concatenatedTable);
end
% --- Example Usage: ---
% Create some sample tables of different sizes
table1 = table(rand(3,1), rand(3,1), 'VariableNames', {'A', 'B'});
table2 = table(rand(1,1), rand(1,1), 'VariableNames', {'A', 'B'});
table3 = table(rand(5,1), rand(5,1), 'VariableNames', {'A', 'B'});
table4 = table(); % Empty table
table5 = table(rand(2,1), rand(2,1), 'VariableNames', {'A', 'B'});
% Create a cell array of these tables
myTables = {table1, table2, table3, table4, table5};
% Call the function
finalTable = sortAndConcatenateTables(myTables);
0 Comments
See Also
Categories
Find more on Shifting and Sorting Matrices 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!