How to delete a loaded variable with a changing name in a for loop?

1 view (last 30 days)
Hi all,
I am attempting to delete a loaded variable with a changing name in a written for loop.
Essentially, I have the following code inside a nested for loop:
PA_fname = strcat('PA_', mouseinfo(n).cage,'_W', num2str(mouseinfo(n).week), '_left_spectro', num2str(pos), '_fr_', num2str(frame));
load([PA_fname, '.mat']);
eval(['PA = ' PA_fname ';']);
In this code, I create a variable PA_fname that contains a specific string value. This string value matches an actual .mat file that I load into MATLAB to extract a certain complex double value that I assign to the variable 'PA'. It is important to note that the variable 'pos' and 'frame' are changing on various iterations of the for loop.
The issue I'm having is that I'd like to delete the loaded .mat file that is put into the workspace on every iteration but I do not know how to do this since the loaded variable name is changing on each iteration so a simple 'clear' function will not work (to my knowledge). I do have many files and many iterations so I cannot clear the workspace on each iteration either. Is there a way to do this? Due to the number of files, loading in each file slows down the computational time immensely.
Ideally, I'd essentially like to write 'clear' and then the string value in PA_fname. But if I write 'clear PA_fname', only the actual variable PA_fname is cleared from the workspace, as one would expect. I hope this makes sense! What do you recommend?
Thanks!
Joseph
  1 Comment
Stephen23
Stephen23 on 21 Sep 2018
Ugh, do NOT do this. Dynamically accessing variable names in one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know why:
In your case it is trivial to write much better code: simply load that file data into an output argument (which is a structure):
S = load(...);
and then access the fields of that structure.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 20 Sep 2018
The whole mess would have been avoided if you'd given an output to load. That would remove the need for eval and for clearing anything.
At the same time, we can make your filename creation more readable:
PA_fname = sprintf('PA_%s_W%d_left_spectro_%d_fr_%d', mouseinfo(n).cage, mouseinfo(n).week, pos, frame);
PA = load([PA_fname, '.mat'); %load content of mat file into structure PA
PA = PA.(PA_fname); %only keep field whose name is contained in PA_fname as PA

More Answers (1)

Stephen23
Stephen23 on 21 Sep 2018
If there is only one variable saved in each file then you can do this very easily:
F = sprintf('%s.mat',PA_fname);
S = load(F);
C = struct2cell(S);
PA = C{1};
If there are multiple variables in the file, then consider using the regexp option to load only the variable that you need:
S = load(F,'-regexp','PA_....')
...
where you need to define the regular expression to match the required variable name.

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!