I have 20 different structures with different names but identical fields. How can I create a loop to reference each different structure?

1 view (last 30 days)
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 Comments
Stephen23
Stephen23 on 20 Jun 2017
Edited: Stephen23 on 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,...
Chris
Chris on 20 Jun 2017
Even if i load all my data into one variable won't I suffer from a similar problem? I'd need still transfer everything from my structures which I got from a 3rd party. They are stored in a .mat file.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Jun 2017
file_data = load('NameOfFile.mat');
result = structfun( CalculateTemperature, file_data );

More Answers (2)

Guillaume
Guillaume on 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 Comments
Walter Roberson
Walter Roberson on 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.)
Chris
Chris on 20 Jun 2017
I agree changing it to not number variables is ideal however I also have other groups such as M8_T and M7_T. Would you suggest putting these into the same or different variable?

Sign in to comment.


Jan
Jan on 20 Jun 2017
Edited: Jan on 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.

Categories

Find more on Structures in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!