Retrieve column vectors from table with correspondent names?

7 次查看(过去 30 天)
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 个评论
Peter Perkins
Peter Perkins 2017-3-29
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 2017-3-29
编辑:Stephen23 2017-3-29
@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.

请先登录,再进行评论。

采纳的回答

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

更多回答(1 个)

Sonam Gupta
Sonam Gupta 2017-3-28
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 个评论
Sonam Gupta
Sonam Gupta 2017-3-28
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 2017-3-29
编辑:Stephen23 2017-3-29
"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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by