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 visualización (últimos 30 días)

Respuestas (1)

Alvery
Alvery el 27 de Sept. de 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

Categorías

Más información sobre Arduino Hardware en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by