Retrieve column vectors from table with correspondent names?
Show older comments
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
Ana Castanheiro
on 27 Mar 2017
Guillaume
on 28 Mar 2017
I don't understand the question. Why do you want to rename the columns of the table? Why wherever you want to use FeWt can't you use tablename.Fe?
If you are asking how to apply the same function to all the columns, then use varfun.
Ana Castanheiro
on 28 Mar 2017
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.?
@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.
Accepted Answer
More Answers (1)
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
Ana Castanheiro
on 28 Mar 2017
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.
"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?
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!