Hi, I'm new to MATLAB and I am having some trouble.Could somebody please explain what this error is and how to fix it? line 2 showing error
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
1 Commento
Walter Roberson
il 27 Set 2020
https://www.mathworks.com/matlabcentral/answers/600598-matlab-function-error#answer_501091
Risposte (1)
Alvery
il 27 Set 2020
It's almost certainly the way you have called the function. You probably didn't pass the same number of input parameters as the function is declared with. The function only discovers that this is a problem when you try to use a parameter that isn't passed.
function [a1, b1, c1] = testfun(a, b, c)
a1 = a;
b1 = b;
c1 = c;
end
To give an example, if you try to run this function from the command-line:
>> testfun(1)
Not enough input arguments.
Error in testfun (line 3)
b1 = b;
>> testfun(1,2)
Not enough input arguments.
Error in testfun (line 4)
c1 = c;
>>
Note that it gives a different error line number based on the number of parameters passed.
Matlab is a funny language - it allows you to define a function with as many parameters as you like, and call it with a different number of parameters (usually less). There's a variable "nargin" which allows the function to make conditional decisions based on missing parameters. A classic use for nargin is to provide default values for extra parameters. e.g.
function [a1, b1, c1] = testfun(a, b, c)
if nargin == 2
c = 0;
end
...
end
0 Commenti
Vedere anche
Categorie
Scopri di più su Arduino Hardware in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!