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!