本文介绍了在Matlab中动态访问嵌套字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
(如何)可以动态访问Matlab中的嵌套字段?我在考虑这样的测试用例:
(How) Can I dynamically access nested fields in Matlab? I was thinking about a test case like this one:
a = struct;
a.foo.bar = [];
place = {'foo', 'bar'};
a.(place{:})
% instead of the following, which only works if know in advance
% how many elements 'place' has
a.(place{1}).(place{2})
推荐答案
我不太满意的一个解决方案是:
One solution I am not very happy with, mainly because it lacks the elegance of the .( )
dynamical field names syntax, is:
getfield(a, place{:})
这篇关于在Matlab中动态访问嵌套字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!