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!