Clear Filters
Clear Filters

How to writetable to file with 2nd row containing VariableUnits?

27 views (last 30 days)
Hi Everyone,
I would like to know how to write a table to file including the metadata (specifically VariableUnits). the function " writetable " does not seem to allow for this function. I have tried to writ table to a comma delimited file and use dlmwrite (....'-append') but this adds the unit row at the bottom of the file and one character at the time instead of the entire string. I would like to have it in the 2nd row, right underneath the variable names.

Accepted Answer

Miguel Aznar
Miguel Aznar on 13 Nov 2017
I found a work around it. It is pretty simple. I hope it helps whoever is stuck trying to do the same. Write first the header with fprinf indicating %s as the formatting of your cell array. Do not forgwt the break in the last cell "\n", and indicate the metadata of your table (i.e. Table.Properties.VariableNames {1,1:end})
fid = fopen('test.csv','w')
fprintf('test.csv', '%s, %s, %s, %s, %s, %s\n', Table.Properties.VariableNames{1,1:end});
fprintf('test.csv', '%s, %s, %s, %s, %s, %s\n', Table.Properties.VariableUnits{1,1:end});
M = table2array(Table);
dlmwrite('test.csv', M, '-append') ;

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!