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!