Save a table of results to an Excel file

4 vues (au cours des 30 derniers jours)
Rebecca Montemurro
Rebecca Montemurro le 30 Avr 2022
Modifié(e) : dpb le 30 Avr 2022
I have a table composed of 33 cell arrays, each one with dimensions (Rx3)
R = different rows for each array.
How can I save all the results to an Excel file ?

Réponses (2)

Voss
Voss le 30 Avr 2022
Here's one way you could save that 1x33 cell array into a single sheet of an xlsx file:
% construct a cell array like yours, with random numbers and sizes:
C = cell(1,33);
for ii = 1:numel(C)
R = randi(50);
C{ii} = rand(R,3);
end
% now put the contents of all cells of C into a matrix.
% first, get the max number of rows of an element of C:
maxR = max(cellfun(@(x)size(x,1),C))
% initialize the matrix with the right size:
M = NaN(maxR,3*numel(C));
% put the contents of C in place in M:
for ii = 1:numel(C)
R = size(C{ii},1);
M(1:R,3*(ii-1)+(1:3)) = C{ii};
end
writematrix(M,'result.xlsx');
Or, if you prefer, here's how you could save each element of that cell array, i.e., each R-by-3 matrix, to a separate sheet in an xlsx file:
for ii = 1:numel(C)
writematrix(C{ii},'result.xlsx','Sheet',ii);
end

dpb
dpb le 30 Avr 2022
Modifié(e) : dpb le 30 Avr 2022
Excel "knows nuthink!" about cell arrays; every cell can contain only one value.
You'll have to expand to an array of sum of column sizes by max(row size) to write as a single array...
Probably the best thing could do here would be to simply loop over the columns of the table, writing each in turn.
You'd have to set the 'Range' named parameter to set each start column in turn to add to the same sheet in same orientation as the original table; the 'WriteMode','Append' parameter will only append vertically, not horizontally. (The latter would make for a nice enhancement request, it would seem, I've actually been working on some files for local nonprofit for which I do pro bono work in which that would also be a nice feature to have).

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by