storing a cell array in table with correct delimiter
2 views (last 30 days)
Show older comments
Could you help me fix the following code by applying a code to myvar in order to get the correct data shown at the end of this question? The issue is that writetable applies the 'bar' delimiter to the content of myvar while it shouldn't.
myvar={'a1','b1','1';'a2','b2','2';'a3','b3','3'};
TT=cell2table({1 'a' '1 1 1' '1,1,1', '1;1;1', myvar;...
2 'b' '2 2 2' '2,2,2', '2;2;2', myvar;...
3 'c' '3 3 3' '3,3,3', '3;3;3', myvar},...
'VariableNames',{'var1' 'var2' 'var3' 'var4' 'var5' 'var6'});
writetable(TT,'test.dat','Delimiter','bar')
TT %wrong stored table
var1|var2|var3|var4|var5|var6
1|a|1 1 1|1,1,1|1;1;1|a1|a2|a3|b1|b2|b3|1|2|3
2|b|2 2 2|2,2,2|2;2;2|a1|a2|a3|b1|b2|b3|1|2|3
3|c|3 3 3|3,3,3|3;3;3|a1|a2|a3|b1|b2|b3|1|2|3
%correct stored table
1|a|1 1 1|1,1,1|1;1;1|'a1','b1','1';'a2','b2','2';'a3','b3','3'
2|b|2 2 2|2,2,2|2;2;2|'a1','b1','1';'a2','b2','2';'a3','b3','3'
3|c|3 3 3|3,3,3|3;3;3|'a1','b1','1';'a2','b2','2';'a3','b3','3'
0 Comments
Answers (1)
Walter Roberson
on 14 Apr 2019
Edited: Walter Roberson
on 14 Apr 2019
You have a cell array (myvar) stored as elements of the table. There is no way for writetable to know that it should use semi-colons between the elements when it writes those entries out. You have defined the delimiter as bar.
"Each column of each variable in T becomes a column in the output file"
and under Algorithm,
"For variables with a celldata type, writetableoutputs the contents of each cell as a single row, in multiple fields. If the contents are other than numeric, logical, character, or categorical, then writetableoutputs a single empty field."
If that is not what you want, then you will have to pre-format the cell array as a character vector with whatever format you find appropriate.
See Also
Categories
Find more on Operators and Elementary Operations 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!