How do I name a structure or matrix with variable names (without using eval)

I know this is a highly frowned upon topic, so my bad already....
I am trying to compile matrices from several .mat files that in groups belong to their own respective folder into one .mat file.
I have an array whose elements I'd like to use to name either a matrix (would convert to string in that case) or a structure that would correspond to the particular matrix I need in the .mat files. I have it running through a (unfinished) for loop:
TagNumbers = [067 068 069] %%%% must be filled in first with all folder names (which should be tag numbers)
subjects= {}; %%%%% for loop below will generate a cell with values that read 'm(Tag number)', e.g. 'm067'
for i = 1:length(TagNumbers)
subjects(i) = {['m' num2str(TagNumbers(1,i),'%03.f')]}; % '%.03.f' designates that tag number will remain a three digit string (modify if necessary)
end
for i = 1:length(TagNames)
y = 'C:\Users\lab\Documents\Results';
g = [y TagNumbers(i)];
d = dir(g);
s = cellfun(@load,{d.name});
The folders that I'm accessing are named according to the elements in TagNumbers, and within each folder, different iterations of the experiment are recorded in .mat files and named by date of experiment. So I'd like to create either a matrix named after the participant tag with concatenated rows of results in chronological order, or put them in a structure named after the tag with the field named by date, or something along those lines. I thought I could maybe feed in values from a cell to do it, but that's a bust too.

4 Comments

Instead of naming the matrices, store them as fields in a structure:
and then save using the '-struct' option. Simple, efficient, no eval required.
Thanks for the response.
The issue is that I have to go through a couple levels of folders to reach the data. So we may have a subject named 1 whose results will appear within a folder named 1 with .mat files like 08/01/20, 08/02/20, etc. Another subject will have their own folder and their .mat files may be named the same as 1 (and any other subjects run that day.
So I have to first localize the directory to a given subject, then load their data, then jump back out, find the next subject's directory and so on. Instead of having one structure with field names like Subject1Day1, Subject1Day2, Subject2Day1 etc. I was hoping to have individual structures named according to subject ID, then within, have field names according to trial day, e.g. subject1.(day). But the issue is that generating those subject structure names will change in the loop according to which directory data is coming from.
"Instead of having one structure with field names like Subject1Day1, Subject1Day2, Subject2Day1 etc. I was hoping to have individual structures named according to subject ID..."
Why were you hoping to have that?
Just as I wrote in my earlier comment, you can simply save a structure using the -struct option, then its fields will be saved as lots of indivudually-named variables exactly the same as if you had gone through the difficult and buggy approach of magically creating and naming lots of separate variables. You stated that your goal is to "I am trying to compile matrices from several .mat files that in groups belong to their own respective folder into one .mat file" and for that you certainly do not need lots of separate variables, just one single scalar structure and save with the -struct option. Of course you can nest other structures inside those fields, just as you describe. Try it.
Personally I would probably not do either of these things, as meta-data should be stored as data in its own right, but I guess that is a lesson to be learned the hard way or with experience.
Okay, thanks Stephen. I'll give it a try

Sign in to comment.

Answers (1)

put everything into one variable. You can use dynamic field names for a struct, new fields added as you go. Or you can hold everything in a cell array and use cell2struct to put it all together.
If you were to put them into separate variables you would have to keep using eval*() to fetch the contents given the name.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2019b

Asked:

on 14 Aug 2020

Commented:

on 15 Aug 2020

Community Treasure Hunt

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

Start Hunting!