필터 지우기
필터 지우기

storing a cell array in table with correct delimiter

조회 수: 2 (최근 30일)
S H
S H 2019년 4월 14일
편집: S H 2019년 4월 14일
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