storing a cell array in table with correct delimiter

2 次查看(过去 30 天)
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'

回答(1 个)

Walter Roberson
Walter Roberson 2019-4-14
编辑:Walter Roberson 2019-4-14
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.
  1 个评论
S H
S H 2019-4-14
编辑:S H 2019-4-14
Ok thank you. Then I have to create 2 functions by myself to do the job correctly.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by