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

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.

1 Comment

Syntaxes given in the documentation for returning the content of one column/variable:
mydata = array2table(randi(9,5,3), 'VariableNames',{'BLUE','GREEN','WHITE'})
mydata = 5x3 table
BLUE GREEN WHITE ____ _____ _____ 1 2 7 1 9 5 1 6 4 4 4 1 5 7 2
mydata.('BLUE') % dot notation
ans = 5×1
1 1 1 4 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mydata{:,'BLUE'} % curly braces
ans = 5×1
1 1 1 4 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Sign in to comment.

 Accepted Answer

labels = mytable.Properties.VariableNames;
for i = 1 : length(labels);
mydata.(labels{i})
end
Also consider using varfun()

More Answers (1)

You can use dot notation to refer to any individual variable within a table.
x = mytable.Xdata;
y = mytable.Ydata;
The first line of code extracts the variable Xdata from the table mytable and stores the result in a new variable named x. Similarly, the second line of code extracts the variable Ydata into y.
Task
Visualize the letter by plotting the X variable of letter on the horizontal axis and the Y variable on the vertical axis.
HintSee SolutionReset
Submit
Incorrect
Is there a plot?
Does the plot have the correct data?
Hint
Pass letter.X and letter.Y to the plot function.

Categories

Asked:

Sim
on 14 Dec 2021

Commented:

on 26 Sep 2024

Community Treasure Hunt

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

Start Hunting!