How to write data from a Cell Array as multiple columns in a text or CSV file?
34 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Stephen23
2024-6-18
编辑:Stephen23
2024-6-18
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 个评论
dpb
2024-6-18
编辑:dpb
2024-6-18
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?
更多回答(2 个)
Hassaan
2024-6-17
% 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
2024-6-17
% 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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!