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'
0 件のコメント
回答 (1 件)
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.
参考
カテゴリ
Help Center および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!