Hi, what should I do to get the 'Not enough input arguments'-error,
Show older comments
If I call my own function with not enough input arguments MATLAB realizes this not until the missing variable is used.
Example:
function d = inputErrorCheck(a,b,c)
if a > b
d = a+b;
else
d = a+c;
end;
end
Output:
>> inputErrorCheck(3,2)
ans = 5
>> inputErrorCheck(2,3)
Not enough input arguments.
Error in inputErrorCheck (line 6) d = a+c;
Is there a setting or something to get MATLAB to check the amount of input arguments before it starts the function?
Thanks!
Accepted Answer
More Answers (1)
Mischa Kim
on 9 Nov 2016
Rebecca, yes. Use something like
function d = inputErrorCheck(a,b,vargin)
switch nargin
case 3
if a > b
d = a + b;
else
d = a + vargin;
end
case 2
d = a + b;
end
end
2 Comments
Rebecca Sippel
on 10 Nov 2016
Jan
on 10 Nov 2016
@Rebecca: There cannot be a switch in Matlab to restrict the number of inputs, because many built-in functions use optional arguments. It is a typical programming style in Matlab.
Categories
Find more on Argument Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!