Table VariableNames Property Won't Print Directly
이전 댓글 표시
I need to take an existing table I have, HTable and write all but the first column to a file, including the VariableNames (Column Headers) as the first line of the file.
Note: MLID is a file handle from an earlier fopen command.
The following code generates an error:
fstr = [repmat('%s,',1,length(HTable.Properties.VariableNames)-2),'%s\n'];
fprintf(MLID,fstr,HTable.Properties.VariableNames(2:end));
Error using fprintf
Function is not defined for 'cell' inputs.
OK, I need to dereference. But, the following code ONLY prints the second VariableName to the file:
fstr = [repmat('%s,',1,length(HTable.Properties.VariableNames)-2),'%s\n'];
fprintf(MLID,fstr,HTable.Properties.VariableNames{2:end});
But the following code works:
fstr = [repmat('%s,',1,length(HTable.Properties.VariableNames)-2),'%s\n'];
hdrs = HTable.Properties.VariableNames(2:end);
fprintf(MLID,fstr,hdrs{:});
I guess I've found the workaround, but a deeper question for me is why? Why doesn't the second set of code produce the exact same thing as the third? Why should assigning one to a new variable before calling fprintf matter? Or is there some other method of dereferencing I would need to get all the values from the cell array?
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!