MATLAB: How to access a table column using a string of the column name

datasetMATLABstruct

If you have a table called table1 with column names X1, X2, X3, how can you access a column X1 with the variable v =  'X1'?

Best Answer

This can be done using dynamic field indexing.
>> table1.('X1');
If you have a variable that contains the string.
>> v = 'X1';
>> table1.(v);
Note: This indexing also works for dataset arrays and structs.