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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1034925/image.png)
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);
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!