How to save a function output to a variable with variable name changing dynamically?

2 views (last 30 days)
I have two folders in two different paths. These two folders contain .csv data files. I want to load these .csv data files from each folder and fun PCA on them. I want to save output variable in a format so that I can keep track that from which data folder I imported those .csv files.
For example, in my code below, I have Data1 and Data2 folders containing these .csv files. In the first iteration of for loop, I will like to load .csv files from folder Data1, and save output variable from pca function as Data1_Name_PCA. For the second iteration, I will like to save the output variable from pca as Data2_Name_PCA, and so on.
Here _PCA is fixes. Only Data1_Name and Data2_Name should change for each iteration.
My code works fine, but I am trying to figure out how to save output of PCA on these dynamically changing names.
For testing my code below, you can use arbitrary .csv files.
Path1 = 'C:\Users\Anonymous\MATLAB\Data1';
Path2 = 'C:\Users\Anonymous\MATLAB\Data2';
DataFiles1 = dir(fullfile(Path1, '*.csv'));
DataFiles2 = dir(fullfile(Path2, '*.csv'));
Filename1 = {DataFiles1.name};
Filename2 = {DataFiles2.name};
filename = {Filename1, Filename2};
[rows, cols] = size(filename);
for i = 1:cols
data1 = importdata(filename{i}{1});
data2 = importdata(filename{i}{2});
data1_Name = filename{i}{1}(1:end-4);
data2_Name = filename{i}{2}(1:end-4);
[data1_Name_PCA, eigVec1_PCA, lambda1_PCA] = pca(data1, 3);
end
  2 Comments
Stephen23
Stephen23 on 13 Jul 2018
Edited: Stephen23 on 13 Jul 2018
Do NOT do this. The MATLAB documentation specifically warns against doing exactly this: "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."

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 13 Jul 2018
  11 Comments
Walter Roberson
Walter Roberson on 14 Jul 2018
If you follow the link above, you will find links to information on how it is possible to create variable names dynamically. However, we really do not recommend it.
Stephen23
Stephen23 on 14 Jul 2018
Edited: Stephen23 on 14 Jul 2018
"But I really wanted to have an output variable as..."
Sure, we understand what you want. And we are advising you, that that approach is not recommended by any MATLAB experts, experienced MATLAB users, or by the MATLAB documentation. Wanting code to do a particular thing does not correlate with it being a good idea, nor with it even being tractable at all, which is why we are giving advice on how to write better code: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval#comment_577030

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!