필터 지우기
필터 지우기

How can I write table variable units to a file?

조회 수: 25 (최근 30일)
David
David 2014년 6월 23일
댓글: Peter Krammer 2022년 11월 10일
Hi!
I'm working with some tables and I've assigned values to the VariableUnits Property field of the table (I also have variable names). I'd like to use writetable to write the table to a file and I'd like to have the VariableNames be the first row and their units appear below, is there any way to do this? I've set the 'WriteVariableNames' part to true and that works, but getting the units in there seems a bit more difficult.
Thanks, David
  댓글 수: 2
Alexandre Aono
Alexandre Aono 2016년 7월 20일
Anyone?
Alberto
Alberto 2017년 5월 9일
Somebody know how to do it ?

댓글을 달려면 로그인하십시오.

답변 (2개)

Zoltan Danilo
Zoltan Danilo 2017년 10월 13일
편집: Walter Roberson 2017년 10월 13일
Hi,
Not an ultimate solution, just a quick fix of the problem.
function [] = WriteTableWithUnits( TableToWtrite, FileName )
%WriteTableWithUnits
% To write tables with names and units of columns and row names
[RowNum, ColNum]=size(TableToWtrite);
HeaderRow=cell(1,ColNum+1);
HeaderRow{1,1}='Rows';
TableToSave=cell2table(cell(RowNum+1, ColNum+1));
for iCol=1:ColNum
HeaderRow{1,iCol+1}=[TableToWtrite.Properties.VariableNames{iCol}, ' [', TableToWtrite.Properties.VariableUnits{iCol},']'];
end
TableToSave{1,:}=HeaderRow;
TableToSave{2:end,1}=TableToWtrite.Properties.RowNames(:);
TableToSave{2:end,2:end}=table2cell(TableToWtrite(:,:));
writetable(TableToSave, FileName, 'WriteVariableNames', false);
end
  댓글 수: 1
Peter Krammer
Peter Krammer 2022년 11월 10일
Thanx Zoltan for your Idea, but it does not works correctly. Your code gives me error in line 12
(To assign to or create a variable in a table, the number of rows must match the height of the table.)
I have a similar problem - to write Table into csv or xls file with Units and Descriptions.
In function writetable, I did not see an option for writing a Units / Descriptions into csv / xls.
And if I write it non-standart, then I have a problem with reading back (with readtable() function ).
Currently, I see the only solution - save to mat file using a save(...) function , but it is not portable/presentable for non-matlab users (they cant read mat file).

댓글을 달려면 로그인하십시오.


Rodney
Rodney 2019년 5월 9일
If you save it as a .mat file it will retain all of the table properties (metadata) including units.
  댓글 수: 1
Peter Krammer
Peter Krammer 2022년 11월 10일
Sure, but mat files are not the best cross-platform files, for easy reading and presenting for non-matlab users.
Csv or xls file representation could be open by many programs; also csv could be reading directly by every text editor. So it looks much more suitable for presenting and cross-platform uses.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by