Attached is an example Excel file.
I'm using this command to read the info out of the file:
T1 = readtable('test.xlsx', 'PreserveVariableNames',true);
Here is my question. How do I read the units out of the 2nd row?
Thanks!

 採用された回答

Walter Roberson
Walter Roberson 2020 年 6 月 17 日

1 投票

filename = 'test.xlxs';
opt = detectImportOptions(filename);
opt.VariableUnitsRange = '2:2'; %row number
T1 = readtable(filename, opt);
T1.Properties.VariableUnits will be set.

4 件のコメント

Leon
Leon 2020 年 6 月 17 日
Many thanks! This is brilliant!
One typo: xlxs -> xlsx
Leon
Leon 2020 年 6 月 17 日
BTW, why doesn't this work?
T1 = readtable(filename, opt, 'PreserveVariableNames', true);
Walter Roberson
Walter Roberson 2020 年 6 月 17 日
you cannot mix named options with the option structure. There is a field you can set in opt to preserve variable names but I would need to to look up the proper name.
Leon
Leon 2020 年 6 月 17 日
Thanks!
It seems this works:
opt.PreserveVariableNames = 1;

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

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 6 月 17 日

0 投票

Try this
units = readtable('test.xlsx', 'Range', 'B2:D2', 'ReadVariableNames', false);

1 件のコメント

Leon
Leon 2020 年 6 月 17 日
Many thanks!
I'm building a set of tools and this function needs to work for all files without knowing the column number. The units will always be on the 2nd row. Is there a way I could do without specifying the 'B2:D2' thing?
Another question. Once the units are read out. How do I call the text string? I tried the below:
a=units.(1)
But, what I got is a cell array: {'DEG/C'}, instead of a string.

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

製品

リリース

R2020a

タグ

質問済み:

2020 年 6 月 16 日

コメント済み:

2020 年 6 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by