Table VariableNames Property Won't Print Directly
Show older comments
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?
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!