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

37 visualizzazioni (ultimi 30 giorni)
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 Commenti
Stephen23
Stephen23 il 14 Ago 2020
Modificato: Stephen23 il 14 Ago 2020
"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.

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 14 Ago 2020
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.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by