正确访问1xn结构的字段的方法 [英] Proper way of accessing field of a 1xn stuct
问题描述
我一直在寻找访问结构的给定字段的正确方法,而手动搜索和在线搜索都无济于事.
I have looked for the proper way to access a given field of a struct and the manual and online searches didn't help.
形式上,让MyStruct
为1xn struct
变量.使用以下命令可以很容易地列出存储在字段中的所有元素:
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
等
现在,我发现能够访问此元素的唯一方法是使用临时变量,例如temp={MyStruct.Thisfield}
然后是temp{1,2}
等.我认为这很笨拙,但无法弄清楚该怎么办.
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.
此:
>> MyStruct{1,1}.Thisfield
来自非单元格数组对象的单元格内容引用.
Cell contents reference from a non-cell array object.
和这个:
>> MyStruct.Thisfield{1,1}
多个结构元素的字段引用,后跟 更多参考块是一个错误.
Field reference for multiple structure elements that is followed by more reference blocks is an error.
是我失败的尝试.
在我的 https://stackoverflow.com/a/22813577/2777181
推荐答案
如果您当前正在这样做:
If you currently do:
temp={MyStruct.Thisfield}
temp{1,2}
您可以通过直接访问该结构的第二个元素来替换它(如@Jucestain所述):
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}
这篇关于正确访问1xn结构的字段的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!