MATLAB Answers

Storing the inputs of a function on a structure (or cell or array)

3 views (last 30 days)
James Otterson
James Otterson on 15 Aug 2019
Edited: madhan ravi on 15 Aug 2019
given a function such as:
function [z,w] = tempFun(x,y)
z = x+1;
w = y+1;
end
is it possible to store a given pair x,y as a structure (struct('x',1,'y',2), say, or as a cell or anything else...) and then unpack it and apply the function to the output?
(In python this could be done as: temFun( **{'x':1, 'y':2})

  0 Comments

Sign in to comment.

Accepted Answer

madhan ravi
madhan ravi on 15 Aug 2019
d=struct('x',1,'y',2);
[z,w] = tempFun(d)
function [z,w] = tempFun(s)
z = s.x+1;
w = s.y+1;
end

  6 Comments

Show 3 older comments
James Otterson
James Otterson on 15 Aug 2019
(the only slight upper hand the python code above has over this is that it will map the variables to their righ places, ie. you won't need to store then in the right order:
tempFun( **{'x':1,'y':10}) == tempFun(**('y':10,'x':1})
madhan ravi
madhan ravi on 15 Aug 2019
Well, I'm a beginner in python (didn't get to this point still) , but this topic would be interesting to be debate about. But then do not underestimate the power of MATLAB ;-)

Sign in to comment.

More Answers (0)

Sign in to answer this question.