Genvarname for array of structs with indexing

4 visualizaciones (últimos 30 días)
AR
AR el 13 de En. de 2023
Comentada: AR el 13 de En. de 2023
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.

Respuesta aceptada

Steven Lord
Steven Lord el 13 de En. de 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 comentario
AR
AR el 13 de En. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by