问题
I have looked for the proper way to access a given field of a struct and the manual and online searches didn't help.
Formally, let MyStruct
be a 1xn struct
variable. It's easy to list all the elements stored in a field with:
MyStruct.Thisfield
ans =
0.7010
ans =
0.310
ans =
0.444
etc.
Now the only way I found to be able to access an element of this is to use a temporary variable, e.g. temp={MyStruct.Thisfield}
and then temp{1,2}
etc. I think it's clumsy but can't figure out what else to do.
This:
>> MyStruct{1,1}.Thisfield
Cell contents reference from a non-cell array object.
and this:
>> MyStruct.Thisfield{1,1}
Field reference for multiple structure elements that is followed by more reference blocks is an error.
are my unsuccessful attempts.
See an example in situ in this answer of mine https://stackoverflow.com/a/22813577/2777181
回答1:
If you currently do:
temp={MyStruct.Thisfield}
temp{1,2}
You can replace this by directly accessing the second element of the struct (As also mentioned by @Jucestain):
MyStruct(2).Thisfield
Note that in one dimensional calls, you don't need to specify all dimensions. So in your original code you could have done
temp{2}
来源:https://stackoverflow.com/questions/23613265/proper-way-of-accessing-field-of-a-1xn-stuct