MATLAB Troubleshooting Multiple Inputs in Matlab Functions: Common Errors and Solutions

  • Thread starter Thread starter PieceORock
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
The discussion centers on issues related to passing multiple inputs into a MATLAB function. The function defined, my_func1, takes two parameters, r and V, and calculates a value using these inputs. The user encounters an error stating that the second variable, V, is undefined when attempting to call the function. It is clarified that the function works correctly when called with two arguments, such as my_func1(2,3). The error arises because the user is incorrectly invoking the function within another function, Trap, using a string reference to my_func1 instead of calling it directly. This leads to the function Trap not passing the required inputs correctly, resulting in the undefined variable error. The solution involves ensuring that my_func1 is called with the correct number of arguments within the Trap function.
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.
 

Similar threads

Replies
4
Views
1K
Replies
4
Views
3K
Replies
9
Views
3K
Replies
6
Views
2K
Replies
1
Views
3K
Replies
5
Views
8K
Replies
3
Views
1K
Replies
1
Views
8K
Back
Top