Dynamic Variables in Loop

3 views (last 30 days)
Manuel Lauber
Manuel Lauber on 9 Nov 2020
Commented: Ameer Hamza on 9 Nov 2020
I need to import a lot of data from different files by specific criteria. Therefore i wrote an ImportLoop that reads all the data files.
Till now i have a problem generating different VariableNames in the Loop. The Variables shouldn't be named A1, A2,... like in the
eval( [ ['a' int2str(k) ] '=.....)
function. The VaribaleName MUST BE the name of the file i import in the loop, therefore i need the variable name to change dynamically inside:
path = 'C:\Users\(...)\*.txt';
files = dir(path);
names = {files.name}; %Array with all the relevant FileNames
rootdir = "C:\Users\(...)\";
for k=1:numel(names)
(...)
[regexprep(extractBetween((names{k}),"A","."), '-', '')] = readtable(fullfile(rootdir, names{k}), opts);
end
I have read the whole despription why i shouldn't use dynamic variables but i havn't found any other solution that works cause the file names get imported randomly and therefore i need the FileName as VariableName to refer to it later in the code specifically.
So please don't just post me the Link, i have seen it 200 times so far.
Ty so much in advance.
  1 Comment
Stephen23
Stephen23 on 9 Nov 2020
Edited: Stephen23 on 9 Nov 2020
Context of this question:
"I have read the whole despription why i shouldn't use dynamic variables but i havn't found any other solution that works..."
Indexing works.
"... cause the file names get imported randomly..."
Have you tried using the answer you were given to your earlier question?:
"The VaribaleName MUST BE the name of the file i import in the loop, therefore i need the variable name to change dynamically inside:"
I very much doubt that it "MUST BE" named the same, it is much easier to store meta-data (such as filenames) as data in their own right, and use indexing to store that data in a loop. Using indexing is also what that MATLAB documentation recommends:

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 9 Nov 2020
Edited: Ameer Hamza on 9 Nov 2020
Now that you are already aware of the problem with using eval to create variable names dynamically, a better approach is to use cell arrays; however, if you want to link filenames to the data, then use a struct. For example,
path = 'C:\Users\(...)\*.txt';
files = dir(path);
data = struct();
for k=1:numel(files)
opt = ..
filename = fullfile(files(i).folder, files(i).name);
variable_name = regexprep(extractBetween((names{k}),"A","."), '-', '');
data.(variable_name) = readtable(filename, opts);
end
data is a struct that contains the same field name as the filenames.
Another solution is to use the file struct to save the data directly.
path = 'C:\Users\(...)\*.txt';
files = dir(path);
for k=1:numel(files)
opt = ..
filename = fullfile(files(i).folder, files(k).name);
files(k).data = readtable(filename, opts);
end
  2 Comments
Stephen23
Stephen23 on 9 Nov 2020
Edited: Stephen23 on 9 Nov 2020
The second option is much simpler, more efficient, and a better use of memory
@Ameer Hamza: the indexing needs to be corrected (you used both i and k)
Ameer Hamza
Ameer Hamza on 9 Nov 2020
@Stephen, thanks for point out the issue with indexing.

Sign in to comment.

More Answers (0)

Categories

Find more on Variables 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!