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

11 ビュー (過去 30 日間)
Miguel Aznar
Miguel Aznar 2017 年 11 月 13 日
コメント済み: Jan Kappen 2024 年 10 月 22 日
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.
  3 件のコメント
Voss
Voss 2024 年 10 月 22 日
移動済み: Voss 2024 年 10 月 22 日
t = table([1;2;3],[4;5;6]);
t.Properties.VariableNames = {'A','B'};
t.Properties.VariableUnits = {'m','ft'};
filename = 'file.txt';
writetable_with_units_line(t,filename)
type(filename)
A,B m,ft 1,4 2,5 3,6
writetable_with_units_in_brackets(t,filename)
type(filename)
A[m],B[ft] 1,4 2,5 3,6
function writetable_with_units_line(t,filename)
fid = fopen(filename,'w');
str1 = strjoin(t.Properties.VariableNames,',');
str2 = strjoin(t.Properties.VariableUnits,',');
fprintf(fid,'%s\n%s',str1,str2);
fclose(fid);
writetable(t,filename,'WriteMode','append','WriteVariableNames',false,'Delimiter',',');
end
function writetable_with_units_in_brackets(t,filename)
fid = fopen(filename,'w');
str = strjoin(t.Properties.VariableNames+"["+t.Properties.VariableUnits+"]",',');
fprintf(fid,'%s',str);
fclose(fid);
writetable(t,filename,'WriteMode','append','WriteVariableNames',false,'Delimiter',',');
end
Jan Kappen
Jan Kappen 2024 年 10 月 22 日
Thanks for that proposal. Yeah it can be done this way, but I'd prefer not to write my own function for that ;)
BTW, I think you can make it shorter without using fopen and friends...
function writetable_with_units_in_brackets(T, filename)
arguments
T table
filename (1,1) string
end
namesWithUnits = T.Properties.VariableNames + " (" + T.Properties.VariableUnits + ")";
T.Properties.VariableNames = namesWithUnits;
writetable(T, filename, Delimiter=",");
end
But again - no function would be better.
A solution could be an extended version of the table class, adding a property WriteVariableUnits which gets parsed during writetable. But since table can't be subclassed that will never happen :(

サインインしてコメントする。

採用された回答

Miguel Aznar
Miguel Aznar 2017 年 11 月 13 日
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') ;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by