Search code examples
matlabworkspace

matlab: is there a way to import/promote variables from a structure to the current workspace?


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

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);   % ????

Solution

  • 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