Genvarname for array of structs with indexing

2 views (last 30 days)
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.

Accepted Answer

Steven Lord
Steven Lord on 13 Jan 2023
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 Comment
AR
AR on 13 Jan 2023
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!