MATLAB: Retrieve column vectors from table with correspondent names

column vectorretrieve columnstablevariablenames

Hi! I have this table (see image) from which I need to retrieve the individual columns and transform them into new variables – in the form of column vectors, and still easily recognizable by the column name. For example, retrieve the Fe column into a variable named e.g. Fe, and the Ni column into a variable named e.g. Ni, as I need to be able to investigate the relations between different chemical elements, i.e. different columns.
Is there an efficient way to do this almost automatically? This is because I need to deal with tables where the VariableNames can be different or appear in a different order within the table. Any help is appreciated!

Best Answer

It does sound like varfun is indeed what you need.
For example, to multiply all your columns by 1.5
newtable = varfun(@(col) col*1.5, yourtable)
You can easily include or exclude some particular columns by filtering the strings returned by yourtable.Properties.VariableNames and passing that as 'InputVariables' to varfun if required.
You can also easily rename the columns in the new table, for example, to add 'Wt' to all the names
newtable.Porperties.VariableNames = compose('%sWt', string(newtable.Porperties.VariableNames));
In any case, I would advise against creating new variables, particularly using eval.