本文介绍了动态变量名称在matlab中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望从另一个结构( BT
)扩展一个结构( bac
)。这些字段的名称包含在单元格数组中(添加
)作为字符串。
I wish to expand a structure (bac
) with a number of fields from another structure (BT
). The names of these fields are contained in the cell array (adds
) as strings.
这是我拥有的现在(显然不做这个工作,解释这个帖子):
this is what i have now (and obviously doesn't do the job, explaining this post):
for i=1:numel(adds)
eval(genvarname('bac.',adds{i})) = eval(strcat('BT.',adds{i}));
end
我还尝试使用 sprintf
,似乎没有为我工作。我感到自信有一个人知道如何做,因为我觉得这应该很简单。
I also tried using sprintf
, which did not seem to work for me. I feel confident one of you knows how to do it, since I feel it should be rather easy.
推荐答案
最好的方法这样做是使用:
The best way of doing this is to use dynamic field names:
for i=1:numel(adds)
bac.(adds{i}) = BT.(adds{i});
end
这篇关于动态变量名称在matlab中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!