How to reference to a struct field with char array?

38 vues (au cours des 30 derniers jours)
Thana Seelaraj Baiskaran
Thana Seelaraj Baiskaran le 13 Mar 2023
Réponse apportée : Voss le 13 Mar 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.

Réponse acceptée

Voss
Voss le 13 Mar 2023
name = 'ABA';
idx = 1;
TestUnits.A.(name)(idx).Values

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by