Writetable() to csv in different cells

23 views (last 30 days)
Nagore
Nagore on 14 May 2019
Commented: Binu on 23 Oct 2020
I have created an application with guide matlab. I need to export a table with its headers to csv, txt... in different cells.
Automatic=[1 2 3 4]';
Semi_Automatic=[5 6 7 8]';
Results={'MA';'FA';'FFMA';'RATIO'};
aut=array2table(Automatic);
sem=array2table(Semi_Automatic);
header=cell2table(Results);
table=[header, aut, sem];
table=table2cell(table);
filename=uiputfile({'*.csv';'*.txt';'*.*'},'Save as');
writetable(table,filename);
This is my result, can anyone help me?

Answers (1)

Guillaume
Guillaume on 14 May 2019
table=table2cell(table);
writetable(table,filename);
will produce an error and not create any file, since the misleadingly named table is no longer a table but a cell array. writetable does not work with cell arrays. Had you not pointlessly converted the table to a cell array, no error would have occured and a csv file would have been created.
The screenshot you show looks like csv file that has been imported into Excel with the wrong delimiter selected in excel. The fix for this is to choose the correct delimiter in Excel. You can of course, trivially export the file from matlab with a different delimiter (with the 'Delimiter' option of writetable) if you don't like the default , delimiter (which is the standard csv delimiter).
Note that the way you construct your table is probably the most roundabout way possible.
clear table %in case you still have a table variable. DO NOT USE table AS A VARIABLE NAME
Automatic=[1 2 3 4]';
Semi_Automatic=[5 6 7 8]';
Results={'MA';'FA';'FFMA';'RATIO'};
mytable = table(Results, Automatic, Semi_Automatic); %that's all that is required to construct your table
Never use table as a variable name, as that's the main function used to create tables.
  1 Comment
Binu
Binu on 23 Oct 2020
@ Guillaume
May I ask you a similar issue with tables.
I have several excel files each has a header and the data is arranged in a same format. As shown in the snapshot below.
I need to append all those excel files one after the other to get a long timeseries, but I don’t need the header. Would you be able to help me with this to do on matlab please?
Thank you

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!