带元胞数组的 Matlab 动态字段名结构
我如何使用动态字段名访问以下结构路径:
var = 'refxtree.CaseDefinition.FlowSheetObjects.MaterialStreamObjects{8}.MaterialStreamObjectParams.Pressure.Value.Text';
fields = textscan(var,'%s','Delimiter','.');
refxtree.(fields{:})
不起作用,因为 MaterialStreamObjects 包含一个单元格数组,我想访问其中的第 8 个单元格,然后继续向下结构路径。
最后我想获取并设置字段值。
How can i access the following structure path with dynamic fieldnames:
var = 'refxtree.CaseDefinition.FlowSheetObjects.MaterialStreamObjects{8}.MaterialStreamObjectParams.Pressure.Value.Text';
fields = textscan(var,'%s','Delimiter','.');
refxtree.(fields{:})
does not work because MaterialStreamObjects contains a cell array of which I want to access the 8th cell and then continue down the structure path.
In the end I want to get and set the fieldvalues.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要为
subsref
构建适当的输入,可能使用substruct
。查看 MATLAB 帮助。You need to build the appropriate input to
subsref
, possibly usingsubstruct
. Look at the MATLAB help.您可以定义一个匿名函数来导航这种特殊类型的结构,其形式为
top.field1.field2.field3{item}.field4.field5.field6.field7
(顺便说一句:真的有必要吗?有这么复杂的结构?)。您可以通过调用注意使用这些函数
,
fields
需要有七个元素。如果您想概括上述内容,则必须动态创建上述函数You can define an anonymous function to navigate this particular kind of structure of the form
top.field1.field2.field3{item}.field4.field5.field6.field7
(as an aside: is it really necessary to have such a complicated structure?).You use the functions by calling
Note that
fields
is required to have seven elements. If you want to generalize the above, you will have to dynamically create the above functions在这种情况下,使用 EVAL 会更容易:
In this case, it is easier to just use EVAL: