3

I have a function that takes a variable number of inputs, say myfun(x1,x2,x3,...).

Now if I have the inputs stored in a structure array S, I want to do something like myfun(S.x1,S.x2,...). How do I do this?

2 Answers 2

4

You can first convert your structure to a cell array using STRUCT2CELL, and then use that to generate the list of multiple inputs.

S = struct('x1','something','x2','something else');
C = struct2cell(S);
myfun(C{:});

Note that the order in which the fields in S are defined are the order in which the inputs are passed. To check that the fields are in the proper order, you can run fieldnames on S, which returns a cell with field names corresponding to the values in C.

Sign up to request clarification or add additional context in comments.

Comments

0

Something to add to Jonas' answer: Actually you can omit the struct and go right for the cell which is then expanded into a list for the function arguments:

c = {125, 3};
nthroot(c{:})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.