Borrar filtros
Borrar filtros

How to reference to a struct field with char array?

44 visualizaciones (últimos 30 días)
Thana Seelaraj Baiskaran
Thana Seelaraj Baiskaran el 13 de Mzo. de 2023
Respondida: Voss el 13 de Mzo. de 2023
I have following problem and can't seem to find a solution, probably because I'm lacking some basic understanding.
I have a multilayer struct array (not sure if the term 'multilayer is used correctly here) and I am trying to reference to one of the 'sub'field.
The structure of the variable is as follows:
TestUnits [1x1 struct]: contains A and B
  • A [1x1 struct]: contains ABA, ABB, ABC ...... so on
  • >> ABA [1x5 struct]: contains following structure aas shown in the image below)
  • >> >> contains 5 [1x1] struct
  • >> ABB [1x5 struct]
  • >> ABC [1x5 struct]
  • B [1x1 struct] (same structure as A)
When I manually evaluate this statement:
>>TestUnits.A.ABA(1).Values
I get an answer listing the content of the struct array.
But when I try to define a char variable and then use it to access the content of the struct array i get an error.
>>man={'ABA(1)'}
>> TestUnits.A.(man).Values
Argument to dynamic structure reference must evaluate to a valid field name.
and when i try this , i get the following
>> TestUnits.A.(man{1}).Values
Reference to non-existent field 'ABA(1)'.
By the way, there no such field as ABA(1). ABS is a 1x5 struct as explained above. I am trying to access the contents of one of the 5 1x1 structs.
How do i do it? Can i not use the referencing method mentioned above? I hope I explained my problem good enough.

Respuesta aceptada

Voss
Voss el 13 de Mzo. de 2023
name = 'ABA';
idx = 1;
TestUnits.A.(name)(idx).Values

Más respuestas (0)

Categorías

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

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by