Genvarname for array of structs with indexing
조회 수: 3 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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.
추가 답변 (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!