How to write data from a Cell Array as multiple columns in a text or CSV file?
26 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Stephen23
on 18 Jun 2024
Edited: Stephen23
on 18 Jun 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
1 Comment
dpb
on 18 Jun 2024
Edited: dpb
on 18 Jun 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')
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')
I wonder if this behavior could be made an extended behavior of num2cell itself?
More Answers (2)
Hassaan
on 17 Jun 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)
% Write the table to a CSV file
writetable(dataTable, 'output.csv');
dpb
on 17 Jun 2024
% Sample 1x2 cell array
c = {rand(5,1) rand(5,1)};
writematrix(cell2mat(c),'test.csv','delimiter',',')
type test.csv
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))
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.
See Also
Categories
Find more on Cell Arrays 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!