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 回表示 (過去 30 日間)
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 9 月 27 日
https://www.mathworks.com/matlabcentral/answers/600598-matlab-function-error#answer_501091

サインインしてコメントする。

回答 (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 Center および File ExchangeProgrammatic Model Editing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by