Adding a unit row to a table

89 次查看(过去 30 天)
Leon
Leon 2020-8-17
编辑: Leon 2020-8-19
Below is how I normall create my table in Excel:
TT = table(Year, Month, Day, Longitude, Latitude, Oxygen, Chlorophyll_A);
writetable(TT, 'test.xlsx');
The issue is that my community wants the units to appear in a separate row, so that the final Excel file will look like this:
Row #1: Year Month Day Longitude Latitude Oxygen Chlorophyll_A
Row #2: N/A N/A N/A decimal_degrees decimal_degrees umol/kg ug/L
Row #3: 2005 3 12, -120 25 206 3.7
...
and so on
My question is how do I modify my above program so that I could add an extra unit row (2nd Row in the Excel file) when using writetable?
Many thanks!
  1 个评论
Michael Soskind
Michael Soskind 2020-8-19
Hi Leon,
Looks like this question has been discussed previously, and although there is no solution with how to truly display the variables as another row, or next to the table header row, there is a way to set the property of the values within that row. That does not help you, but that is discussed here.
Best,
Michael

请先登录,再进行评论。

采纳的回答

Sindar
Sindar 2020-8-19
% create example table
T = array2table(magic(3),"VariableNames",["a";"b";"c"]);
% define units
T.Properties.VariableUnits = ["m";"kg";"N"];
% print the variable names in the first row
writecell(T.Properties.VariableNames,'units_please.xlsx','Range',"A1")
% print the units in the second row
writecell(T.Properties.VariableUnits,'units_please.xlsx','Range',"A2")
% print the data starting in the third row
writetable(T,'units_please.xlsx',"Range","A3","WriteVariableNames",false)
  2 个评论
Sindar
Sindar 2020-8-19
A limited function. You could try adding varargin and parsing the normal writetable arguments to figure out where to start the printing
function write_table_with_units(T,filename)
% print the variable names in the first row
writecell(T.Properties.VariableNames,filename,'Range',"A1")
% print the units in the second row
writecell(T.Properties.VariableUnits,filename,'Range',"A2")
% print the data starting in the third row
writetable(T,filename,"Range","A3","WriteVariableNames",false)
end
Leon
Leon 2020-8-19
编辑:Leon 2020-8-19
Awesome! Thank you so much.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by