matlab:有没有办法将变量从结构导入/提升到当前工作空间? [英] matlab: is there a way to import/promote variables from a structure to the current workspace?

查看:120
本文介绍了matlab:有没有办法将变量从结构导入/提升到当前工作空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function y = myfunc(param)
C = param.C;
L = param.L;
Kp = param.Kp;
Ki = param.Ki;
...

有没有一种方法可以概括以上代码?我知道如何使用fieldnames()getfield()来概括结构访问,但是不知道如何在不调用eval()的情况下设置变量(这很糟糕).

Is there a way to generalize the above code? I know how to generalize the structure access using fieldnames() and getfield(), but not how to set variables without calling eval() (which is evil).

for n = fieldnames(param)'
  name = n{1};
  value = param.(name);
  do_something_with(name,value);   % ????

推荐答案

没关系,我想通了;此辅助功能起作用:

never mind, I figured it out; this helper function works:

function vars_pull(s)
    for n = fieldnames(s)'
        name = n{1};
        value = s.(name);
        assignin('caller',name,value);
    end

这篇关于matlab:有没有办法将变量从结构导入/提升到当前工作空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆