Is there such a table units identification approach?

Imagine I have a table T1. We know its units will be
Units = T1.Properties.VariableUnits;
Similarly, its header will be
Headers = T1.Properties.VariableNames;
If I know the location of the VariableName, I can find its units easily. For example, if I know Header #35 is "dissolved_oxygen", I can find its unit easily as Units{35}. The thing is that sometimes I may not know the location of the VariableName. The only thing I know is its variableName Is there a similar approach like the Table values?
Column = T1.dissolved_oxygen;
I tried the below and it did not work:
Unit = Units.dissolved_oxygen;
Many thanks!

 Akzeptierte Antwort

Steven Lord
Steven Lord am 21 Okt. 2020
Let's make a sample table and give it some units:
>> load patients
>> patients = table(LastName,Gender,Age);
>> patients.Properties.VariableUnits{1} = 'person';
>> patients.Properties.VariableUnits{2} = 'N/A';
>> patients.Properties.VariableUnits{3} = 'years';
Extract the variable names and units from the table:
>> names = patients.Properties.VariableNames;
>> units = patients.Properties.VariableUnits;
Get the units associated with the variable named Age:
>> units(names == "Age")
If you have multiple variable names, use ismember with two outputs to determine which element of the names array corresponds to the variable names you seek.

1 Kommentar

Leon
Leon am 21 Okt. 2020
Many thanks!
This is what I'm looking for. I find curly parenthesis works even better.
units{names == "Age"}

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

drummer
drummer am 21 Okt. 2020
Try this:
opts = detectImportOptions('yourXLfile.xlsx')
opts.selectedVariableNames = {'dissolved_oxygen'};
T1 = readtable('yourXLfile.xlsx', opts);
summary(T1)
Cheers.

4 Kommentare

Leon
Leon am 21 Okt. 2020
Many thanks! I can find its index number from a search of the Units string array as well, but I really hope there is an easier way to do this, because I have a lot of variables and I need to be able to call its units easily.
drummer
drummer am 21 Okt. 2020
Bearbeitet: drummer am 21 Okt. 2020
Can you share a sample of your table where you can get 'dissolved_oxygen' and Unit{35}?
I made a different approach from Stephen's, using the same example.
His approach is more dynamic, though. I walked through all the headers.
T = readtable('patients.dat');
headers = T.Properties.VariableNames
for i = 1 : numel(headers)
if strcmp(headers(i), 'Gender') == 1 % if the header is your variable of interest
i % you could replace here by your Unit{i}, for example.
end
end
Leon
Leon am 21 Okt. 2020
Many thanks for sharing!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Distribution Plots finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2020b

Gefragt:

am 21 Okt. 2020

Kommentiert:

am 21 Okt. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by