Genvarname for array of structs with indexing

조회 수: 3 (최근 30일)
AR
AR 2023년 1월 13일
댓글: AR 2023년 1월 13일
I have an array of structures, say AB. I operate upon it by using the nomenclature: AB(1).somefield or AB(5).someotherfield, etc. I now find I have to expand AB further. I would prefer to programmatically get to AB1(1).somefield or AB2(5).someotherfield, etc, taking the previous examples.
However, it looks as though I can't write:
(genvarname(['AB' num2str(index)]))(differentindex).somefield
Here, the variable 'index' would help generate various struct names (AB1, AB2, AB3, ...). 'differentindex' would be used to index into the array of structs.
I suppose I could instead expand the dimension of AB, and go with AB(x,y), but I don't prefer this. If there were an alternative I'd appreciate it, or if there were affirmation that AB(x,y) is the way to go, that's good to know as well.

채택된 답변

Steven Lord
Steven Lord 2023년 1월 13일
Can you dynamically create variables with numbered names like AB1, AB2, AB3, etc.? Yes.
Should you do this? The general consensus is no. That Answers post explains why this is generally discouraged and offers several alternative approaches.
  댓글 수: 1
AR
AR 2023년 1월 13일
Thanks - I didn't read all of it but read enough to get the drift. I suppose multidimensional struct array works ok. It's just hard to visualize and display the data when each struct contains numerous fields with large matrices stored in the fields.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by