Create single variable from workspace variables

10 views (last 30 days)
I have many variables in the form (:,1) with different names in my workspace. What i want to achieve is to write a script that generates me a single variable in a table form with the values of any variable in columns with the name of the variable on top of the column. Somehow this script should go to the first variable in the workspace and store the content and the name in the generated variable in (:,1), the second in (:,2) and so on.
What i am missing is exactly the idea/way how to achieve that the script gets the variables one by one in the order in which they are in the workspace. Has anyone of you an idea?
  3 Comments
Juri Ymeraj
Juri Ymeraj on 15 May 2018
i did, but who gets only the name of the variable and not the values of that variable, thanks anyway :)
Stephen23
Stephen23 on 15 May 2018
Edited: Stephen23 on 15 May 2018
@Juri Ymeraj: the best solution is to avoid this situation entirely. Luckily this is usually trivially easy to avoid, e.g. by load-ing into an output variable instead of simply spamming variables into the workspace:
S = load(...);
Note that dynamically accessing variable names is one way that beginners force themselves into writing slow, buggy, complex code that is hard to debug. You can read more about why here:
Thus the most important question is: how did you get all of those variables into the workspace?

Sign in to comment.

Accepted Answer

Aditya Salveru
Aditya Salveru on 15 May 2018
Edited: Aditya Salveru on 15 May 2018
Hi Juri,
You can get the names of all variables in your workspace using 'who' and you can access the variables with the corresponding names with 'eval'.
I am providing the code for your problem assuming all the variables in your workspace are of same dimensions (column arrays).
clc;
clear;
one=randi(100,10,1); %creating random number column arrays.
two=randi(100,10,1); %You can remove these three lines along with the clc and clear on top.
three=randi(100,10,1);
k=who; %getting the names of all the variables in workspace.
len=length(k); %calculating the number of rows(variables).
y=[];
for i=1:len
if(i==1)
y=eval(k{i});
else
y=[y eval(k{i})]; %creating a matrix from the variables.
end
end
t=array2table(y,'VariableNames',k') %creating the table from the matrix with the variables names on top of the coulumn.
't' here is the final table you wanted.
Hope this clears your query.
Thanks,
Aditya.
  3 Comments
Stephen23
Stephen23 on 15 May 2018
Edited: Stephen23 on 15 May 2018
@Aditya Salveru: in future you might like to add a comment and explain that doing this is not recommended. For example you could quote the MATLAB documentation: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array"
Then you will help beginners to learn how to write better code. If you are interested to learn why creating/accessing variable names dynamically is not recommended by MATLAB experts, then you can read many discussions on this topic:

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!