How to split an array into individual values for passing them into a multiple input function?

33 views (last 30 days)
Dear community,
I need to pass an array as a multiple variables into an anonymous function.
I am wondering if there is a way to perform the following code in a single line:
fun = @(x1,x2,x3) whatever_function
x = [1 2 3];
y = num2cell(x);
fun(y{:})
The idea is to perform something like this:
num2cell(x){:}
But this code does not work.
The reason for that is passing an array into an anonymous function created by
matlabFunction
.

Answers (1)

Cris LaPierre
Cris LaPierre on 25 Feb 2021
If the function has 3 input arguments, MATLAB expects each input to be separated by commas. The way I would do this is to pass in one column for each input.
fun(y{:,1},y{:,2},y{:,3})

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!