Retrieve column vectors from table with correspondent names?

15 views (last 30 days)
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!
  5 Comments
Peter Perkins
Peter Perkins on 29 Mar 2017
You most likely do not want to do this. Using eval to create workspace variables whose names are programmatically created is usually not a good idea, for reasons that have been explained elsewhere.
Why not just leave them in the table, and refer to them as tableName.Fe, etc.?
Stephen23
Stephen23 on 29 Mar 2017
Edited: Stephen23 on 29 Mar 2017
@Ana Castanheiro: this will make your code slow, buggy, hard to debug, and hard to read:
A much better solution is exactly like Peter Perkins already explained: leave your data in the table. This is much more efficient, neater, and will make your code easier to write and check.
"Is there an efficient way to do this almost automatically?"
Yes, the efficient solution is to keep your data in the table. Because then you do not create multiple unnecessary copies of data, and you do not use awful programming methods (like dynamically defining variable names).
Beginners often think that creating and accessing variable names dynamically is the best thing since sliced bread. Experts don't think this, and have many reasons for avoiding this awfully bad way of writing code. If you want to learn how to write efficient code (which is what you ask for), then you might like to consider why all of those experts are telling you to avoid eval.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 28 Mar 2017
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.

More Answers (1)

Sonam Gupta
Sonam Gupta on 28 Mar 2017
I assume that you are having the table loaded in MATLAB workspace. One way to create vectors corresponding to each column in the table is as shown below:
%creating a table
load patients;
T = table(Age,Gender,Height,Weight,Smoker,'RowNames',LastName);
% get the Column names
col_names = T.Properties.VariableNames;
%generate a variable name different then the column name and store the
%column of the table in that variable
for k=1:length(col_names)
v = genvarname(col_names{k}, col_names{k});
eval([v '= T.(col_names{k});']);
end
Here using genvarname, I am generating a variable similar to the column name. Using eval command, I am assigning the value of the kth column to this variable.
Please note that use of eval command is not recommended as it is not that efficient.
Hope this helps!
  4 Comments
Sonam Gupta
Sonam Gupta on 28 Mar 2017
Use only eval([z '= sizeSor.(col_names{k})']);
Do not assign its output to another variable. The eval statement itself is assigning column vector to the variable name contained in z.
Stephen23
Stephen23 on 29 Mar 2017
Edited: Stephen23 on 29 Mar 2017
"Please note that use of eval command is not recommended as it is not that efficient."
Inefficient. Slow. Turns off all of the code checker tools that help you write better code. Makes code harder to read. Very difficult to debug...
Why would a beginner even want to use eval?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!