How to write data from a Cell Array as multiple columns in a text or CSV file?

35 vues (au cours des 30 derniers jours)
Kylen
Kylen le 17 Juin 2024
Commenté : Kylen le 20 Juin 2024
Greeting,
I have a 1x2 cell array with data in each cell. I'm trying to extract the data from each cell and write it to a CSV or text file. I've tried various combinations of writecell, cell2table, writetable, etc. The closest I can get us using writecell, but I end up with a single row containing all the data from both cells in the cell array.
Does anybody have a suggestion how to end up with multiple columns when I write my output to a file?
For context, right now my cell array is 1x2, and is only this small since I'm building and troubleshooting a Simulink model and data output script. Once I finish troubleshooting, the real deal data in the cell array will probably be around 1x20, and I'll be doing about 10 runs in Simulink (so 10 total sets of 1x20 output in my cell arrays).
Thanks in advance!

Réponse acceptée

Stephen23
Stephen23 le 18 Juin 2024
Modifié(e) : Stephen23 le 18 Juin 2024
This actually works with vectors of different lengths (as your question shows):
C = {[0;1;2;3], [4;5;6;7;8;9]};
D = {};
for k = 1:numel(C)
V = num2cell(C{k});
D(1:numel(V),k) = V;
end
writecell(D,'myfile.csv')
Checking:
type myfile.csv
0,4 1,5 2,6 3,7 ,8 ,9
  1 commentaire
dpb
dpb le 18 Juin 2024
Modifié(e) : dpb le 18 Juin 2024
Good catch @Stephen23, I noticed the vector sizes were four digits beginning w/ 6, but didn't observe they weren't the same length so the conversion directly to a 2D array doesn't work..,converting each cell in turn and combining into the 2D cell array does...
Just a minor rewriting of your solution...precomputing the sizes vector for the reference and eliminating the intermediary...
C = {[0;1;2;3], [4;5;6;7;8;9]};
D = {};
S=cellfun(@numel,C);
for k = 1:numel(C)
D(1:S(k),k)=num2cell(C{k});
end
writecell(D,'myfile.csv')
type('myfile.csv')
0,4 1,5 2,6 3,7 ,8 ,9
On reflection, nothing is gained by precomputing the lengths...
C = {[0;1;2;3], [4;5;6;7;8;9]};
D = {};
for k = 1:numel(C)
D(1:numel(C{k}),k)=num2cell(C{k});
end
writecell(D,'myfile.csv')
type('myfile.csv')
0,4 1,5 2,6 3,7 ,8 ,9
I wonder if this behavior could be made an extended behavior of num2cell itself?

Connectez-vous pour commenter.

Plus de réponses (2)

Hassaan
Hassaan le 17 Juin 2024
% Sample 1x2 cell array
cellArray = {[1 2 3 4]; [5 6 7 8]};
% Initialize a matrix to store the data from the cell array
numCells = numel(cellArray);
maxLength = max(cellfun(@length, cellArray));
dataMatrix = NaN(maxLength, numCells); % Use NaN for padding shorter arrays
% Fill the matrix with data from the cell array
for i = 1:numCells
dataMatrix(1:length(cellArray{i}), i) = cellArray{i};
end
% Convert the matrix to a table if you want column headers
dataTable = array2table(dataMatrix, 'VariableNames', {'Column1', 'Column2'});
disp(dataTable)
Column1 Column2 _______ _______ 1 5 2 6 3 7 4 8
% Write the table to a CSV file
writetable(dataTable, 'output.csv');

dpb
dpb le 17 Juin 2024
% Sample 1x2 cell array
c = {rand(5,1) rand(5,1)};
writematrix(cell2mat(c),'test.csv','delimiter',',')
type test.csv
0.751289722855528,0.625407599675041 0.320210583319942,0.35105797453853 0.229944968794913,0.892547779281931 0.751958522185629,0.267327006907302 0.369014419390138,0.303300508737344
The "trick" is to not try to write the cell array as a compound cell array but convert it to the underlying 2D array.
It would be "more simpler" to not create the cell array in the beginning if you can avoid it, but from Simulink it may be less convenient; I don't have it so don't really know much about your choices from that side.
To do it as a cell, though, you'll need to convert the 1x2 array to the Nx2 cell array --
cc=num2cell(cell2mat(c))
cc = 5x2 cell array
{[0.7513]} {[0.6254]} {[0.3202]} {[0.3511]} {[0.2299]} {[0.8925]} {[0.7520]} {[0.2673]} {[0.3690]} {[0.3033]}
Then you can add text for column headers to the cell array if desired or use the 'Append' named parameter to make separate writes of the header followed by the data.

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by