Function with two inputs gives "undefined" error for second argument

  • Context: MATLAB 
  • Thread starter Thread starter PieceORock
  • Start date Start date
  • Tags Tags
    Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 8K views
PieceORock
Messages
2
Reaction score
0
I am having problems putting multiple inputs into a function file. This is what I have.

function y = my_func1(r ,V)
y=6.28.*r.*V;

When I try to use this function, it says:

? Input argument "V" is undefined.

Switching r and V gives the same error, but it is always the second variable that throws the error. If anybody knows if I am putting this in wrong, please let me know
 
Physics news on Phys.org
I don't see any problem with that function. Are you sure your calling the function properly?
 
There's nothing wrong with that function. (I assume you're saving it as my_func1.m).

It works fine for me as long as I call it with 2 arguments, i.e
> my_func1(2,3)

I can reproduce your error message exactly if I call it with only 1 argument. You wouldn't be doing that by any chance, would you?
 
I don't know if I'm calling it incorrectly. I made two arrays, one for r and one for V like this r=[1 2 3] and V=[1 2 3] in the command window. Then I am called another function with the line I = Trap('my_func1', 0, 1, 1)
where the numbers are just inputs to another function.
 
By writing the line

I=Trap('my_func1', 0, 1, 1)

You are not calling the function my_func1.

You are calling the function Trap with the inputs being the string (not the function) 'my_func1' and then three input variables.

Inside the function Trap, you will probably find a line which calls whatever the input string is. That function call most likely only has one input variable which is why you are getting undefined input variables.