Evgeni Sergeev
Evgeni Sergeev

Reputation: 23593

matlab - arrayfun(..) call, "Not enough input arguments."

In the following boiled down example illustrating the error, the function f_what(..) should return the values of input argument y at indices in the array ts:

function Y = f_what(y, ts)

    function get_out = get(t)
        get_out = y(t);
    end

    Y = arrayfun(get, ts);

end

Calling it:

>> f_what(1:10, 1:5)
Error using f_what/get (line 4)
Not enough input arguments.

Error in f_what (line 7)
    Y = arrayfun(get, ts);

Also, for some reason, the following, where get(..) should be the same as the one above, works:

function Y = f_what(y, ts)

    get = @(t) y(t);

    Y = arrayfun(get, ts);

end

Calling it:

>> f_what(1:10, 1:5)

ans =

     1     2     3     4     5

"Not enough input arguments" ... arrayfun(..) should call its first argument with, in this case, one argument. And get(..) has one input argument. I don't get why it's not enough.

Edit: even more boiled down:

function Y = f_what

    function get_out = get_(t)
        get_out = t;
    end

    Y = arrayfun(get_, 1:5);

end

Still the same error.

Edit 2: It works if I supply @get to the first argument of arrayfun(..), instead of get. But I still don't get why it doesn't work without the @.

Upvotes: 2

Views: 5820

Answers (1)

angainor
angainor

Reputation: 11810

Looking at the arrayfun documentation

func

Handle to a function that accepts n input arguments and returns m output arguments.

A handle in matlab is denoted using @, so you need to pass @get as first parameter. Otherwise matlab tries to evaluate function get instead of obtaining its handle, which results in "not enough parameters" error.

In the example that works you defined get to be a handle to an anonymous function, that's why it worked.

Upvotes: 4

Related Questions