Create single variable from workspace variables

1 回表示 (過去 30 日間)
Juri Ymeraj
Juri Ymeraj 2018 年 5 月 15 日
編集済み: Stephen23 2018 年 5 月 15 日
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 件のコメント
Juri Ymeraj
Juri Ymeraj 2018 年 5 月 15 日
i did, but who gets only the name of the variable and not the values of that variable, thanks anyway :)
Stephen23
Stephen23 2018 年 5 月 15 日
編集済み: Stephen23 2018 年 5 月 15 日
@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?

サインインしてコメントする。

採用された回答

Aditya Salveru
Aditya Salveru 2018 年 5 月 15 日
編集済み: Aditya Salveru 2018 年 5 月 15 日
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 件のコメント
Aditya Salveru
Aditya Salveru 2018 年 5 月 15 日
Welcome!!
Stephen23
Stephen23 2018 年 5 月 15 日
編集済み: Stephen23 2018 年 5 月 15 日
@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:

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by