Error using nargin You can only call nargin/nargout from within a MATLAB function.
33 views (last 30 days)
Show older comments
if nargin &&ischar(varargin{1})
0 Comments
Answers (1)
Walter Roberson
on 9 Mar 2021
nargin cannot be used at the command line, and it cannot be used in a script file or Live Script (except inside a function defined within a script.)
Exception to those: it can be used in the definition of an anonymous function, and those can be inside a script or command line.
1 Comment
Steven Lord
on 9 Mar 2021
nargin can only be called from the MATLAB prompt if you call it with an input argument. If you do this will return the number of input arguments the function handle that you pass into it can accept.
nargin(@sin)
This says the function handle @sin accepts one input argument.
When called without an input it returns the number of input arguments that were passed in when the function in which nargin was called was called. Since there is no "function in which nargin was called" at the MATLAB prompt, that throws an error.
nargin
That error message is mostly correct (it shows the 0-input calls) but perhaps a little misleading. I'll note that to the development team.
See Also
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!