Use a single variable name/label with the dot notation, to get access to a table content

44 views (last 30 days)
Hi, I have a table called "mydata"
>> class(mydata)
ans = 'table'
from which I usually retreive data (stored in its columns) throught the following dot notation command:
mydata.BLUE
mydata.GREEN
mydata.WHITE
...
This time, I would like to use a single "table variable name/label" which contains all the "columns names"
label = {'BLUE','GREEN','WHITE'}
in order to get access to all the data I need. Something like this, that obviously does not work:
for i = 1 : 3
mydata.label(i)
end
Any suggestion?
P.S.: I am not sure if I used the correct matlab terminology to explain my question and for the corresponding title. If both title and question are not clear, I will change them.

Accepted Answer

Walter Roberson
Walter Roberson on 14 Dec 2021
labels = mytable.Properties.VariableNames;
for i = 1 : length(labels);
mydata.(labels{i})
end
Also consider using varfun()

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!