I have 20 different structures with different names but identical fields. How can I create a loop to reference each different structure?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2017-6-20
编辑:Stephen23
2017-6-20
"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,...
采纳的回答
Walter Roberson
2017-6-20
file_data = load('NameOfFile.mat');
result = structfun( CalculateTemperature, file_data );
1 个评论
更多回答(2 个)
Guillaume
2017-6-20
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 个评论
Walter Roberson
2017-6-20
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
2017-6-20
编辑:Jan
2017-6-20
@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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!