dynamic variable names in matlab

亡梦爱人 提交于 2020-01-15 12:21:44

问题


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

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.


回答1:


The best way of doing this is to use dynamic field names:

for i=1:numel(adds)
    bac.(adds{i}) = BT.(adds{i});
end


来源:https://stackoverflow.com/questions/29545862/dynamic-variable-names-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!