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

조회 수: 2 (최근 30일)

답변 (1개)

Alvery
Alvery 2020년 9월 27일
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

카테고리

Help CenterFile Exchange에서 Programmatic Model Editing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by