I have 20 different structures with different names but identical fields. How can I create a loop to reference each different structure?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Chris
am 20 Jun. 2017
Kommentiert: Walter Roberson
am 20 Jun. 2017
I have many different structures from a mat file called:
M10_T1, M10_T2, M10_T3, M10_T4, M10_T5, M10_T6, M10_T7, M10_T8
I'm trying to create a loop that cycles the structures listed through a function. This is basically what I'm trying to do although the code is nowhere near correct
NamesOfStructures= %Names or reference to each structure
for i = 1:NumberOfStructures
CalculateTemperature(NameOfStructure(1))
end
3 Kommentare
Stephen23
am 20 Jun. 2017
Bearbeitet: Stephen23
am 20 Jun. 2017
"I have many different structures from a mat file called:"
"M10_T1, M10_T2, M10_T3, M10_T4, M10_T5, M10_T6, M10_T7, M10_T8"
Well, actually there is a way to access those variables in a loop, but you might like to first know what the MATLAB documentation says about it: "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."
You might also like to read what experienced MATLAB users say about what you are trying to do (hint: they strongly advise against it):
A much better solution is to load your data into one variable, and then simply access the data using indexing and/or fieldnames, e.g. if you use load then always load into an output variable. This will more efficient, neater, more robust, easier to check, easier to debug, faster,...
Akzeptierte Antwort
Walter Roberson
am 20 Jun. 2017
file_data = load('NameOfFile.mat');
result = structfun( CalculateTemperature, file_data );
1 Kommentar
Weitere Antworten (2)
Guillaume
am 20 Jun. 2017
And this is exactly why we keep on saying that you should not number variables or name then sequentially. If these structures were all stored in one variable as elements of an array your loop would have been trivial to write. So save yourself some grief, get rid of these multiple variables:
M10_T = eval(sprintf('[%s]', strjoin(compose('M10_T%d', 1:numofstructs), ', ')));
And use that array of structures instead. Your loop is then trivial:
for i = 1 : numel(M10_T)
CalculateTemperature(M10_T(i))
end
So again, do not number variable names
2 Kommentare
Walter Roberson
am 20 Jun. 2017
Ah, but they are in a .mat file, so we can get them into one struct just by using load() . Then the names and order of them do not matter (unless, that is, there were other variables in the .mat as well.)
Jan
am 20 Jun. 2017
Bearbeitet: Jan
am 20 Jun. 2017
@Chris: Blame the 3rd party. It is really ugly to provide data consisting of variables called "M10_T8", which obviously hide parameters in the names. This is such a bad idea!
Nevertheless, you have these data and need to work with it.
Data = load(MatFile);
Name = fieldnames(Data);
Value = struct2cell(Data);
Now you can extract the strange indices from the names using sscanf on demand and process the values using a loop:
for iName = 1:numel(Name)
CalculateTemperature(Value{iName});
end
This equals the suggested structfun of Walter.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!