How to reference to a struct field with char array?

32 次查看(过去 30 天)
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.

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by