Export rows as combined separate CSV files
2 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Peter Perkins
on 17 Jun 2022
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 Comments
See Also
Categories
Find more on Tables 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!