Glup
Glup

Reputation: 11

Matlab - bug with multiple inputs

This is my function:

function [o1  o2] = f(t, y)
o1 = y(2);
o2 = -sin(y(1));
end

When I call it from command prompt:

f(1, [2,3])
ans = 3

why do I only see o1? Also, this doesn't work

feval(f, 1, [2 3])

The error message is

Input argument "y" is undefined.

Error in ==> f at 2
o1 = y(2);

Please help, I have no idea what's going on.

Upvotes: 1

Views: 981

Answers (2)

jmetz
jmetz

Reputation: 12773

feval should have been called with a function handle or string, so use

feval(@f, 1, [2,3])

or

feval('f', 1, [2,3])

As you will see this also returns just the first output of the function. To receive further outputs you must assign them, e.g.

[o1, o2] = feval(@f, 1, [2, 3])

Upvotes: 3

abcd
abcd

Reputation: 42225

If you just call the function without explicit output variable, it will only return the first output argument, which is o1 and assign it to the bit-bucket, ans. To get both outputs, do the following.

[o1,o2]=f(1, [2,3])

To use feval, you should pass a function handle, which is the name of the function preceded by an @ sign. So, feval(@f,1,[2,3]) should work.

Upvotes: 3

Related Questions