Export rows as combined separate CSV files
1 回表示 (過去 30 日間)
古いコメントを表示
Hi I have a 255 x 3 cell array that looks like shown below. I want to export all the column values from each row as tables in CSV files. In this case this will result in 255 CSV files with 1024 x 3 tables.

Thanks in advance!
0 件のコメント
回答 (1 件)
Peter Perkins
2022 年 6 月 17 日
There are a bunch of ways to do this. I thought I'd see how well rowfun works:
> C = {rand(10,1) rand(10,1); rand(10,1) rand(10,1); rand(10,1) rand(10,1)}
C =
3×2 cell array
{10×1 double} {10×1 double}
{10×1 double} {10×1 double}
{10×1 double} {10×1 double}
Turn that cell array into a table with two cell variables:
>> T = cell2table(C)
T =
3×2 table
C1 C2
_____________ _____________
{10×1 double} {10×1 double}
{10×1 double} {10×1 double}
{10×1 double} {10×1 double}
Combine the two col vectors in each row of T to make a table containing one cell variable, each cell containing a table, Then add a file name to each row:
>> T = rowfun(@(c1,c2) {table(c1,c2)},T,"ExtractCellContents",true)
T =
3×1 table
Var1
____________
{10×2 table}
{10×2 table}
{10×2 table}
>> T.Name = "data" + (1:3)' + ".csv"
T =
3×2 table
Var1 Name
____________ ___________
{10×2 table} "data1.csv"
{10×2 table} "data2.csv"
{10×2 table} "data3.csv"
Now use rowfun to write out each table:
>> rowfun(@(c,name) writetable(c{:},name),T,"NumOutputs",0);