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

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
.

回答 (1 件)

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})

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

質問済み:

2021 年 2 月 25 日

回答済み:

2021 年 2 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by