MATLAB Answers

How can I use NARGIN/NARGOUT to determine the number of input/output arguments of an object method?

111 views (last 30 days)
I am trying to determine the number of input/output arguments of an object's method. When the name of any object method is passed to NARGIN/NARGOUT, the returned value is always -1 regardless of the number of inputs/outputs specified in the method signature.
The following syntax returns -1 for any MethodName:
nargout('MethodName')

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 20 Sep 2010
The ability to call NARGIN/NARGOUT directly on object methods is not available in MATLAB. As a workaround, specify the class and method names explicitly using the following syntax:
nargout('ClassName>ClassName.MethodName')

  0 Comments

Sign in to comment.

More Answers (4)

Markus Leuthold
Markus Leuthold on 16 Apr 2015
It's almost five years later and this bug still exists. Any news from Mathworks on how to handle this situation in 2015?
Example:
Class C
classdef C < handle
methods
function r=fcn1(~)
r='run fcn1';
end
function fcn2(~)
disp('run fcn2');
end
end
end
function run
function run(f)
switch nargout(f)
case 0
feval(f);
case 1
disp(feval(f));
case -1
disp('bug')
end
What I expect from the following code
>> c=C;
>> run(@c.fcn1)
run fcn1
>> run(@c.fcn2)
run fcn2
Instead I get
>> c=C;
>> run(@c.fcn1)
bug
>> run(@c.fcn2)
bug
How do you recommend to workaround in case where you want to know nargout from a function handle AND evaluate a function handle?

Eric Salemi
Eric Salemi on 12 Jul 2018
Edited: Eric Salemi on 12 Jul 2018
I suggest you use the meta information provided by the MCOS class system:
>> class = ?containers.Map;
>> method = class.MethodList({mclass.MethodList.Name} == "subsref");
>> method.InputNames
ans =
2×1 cell array
'A'
'S'
>> method.OutputNames
ans =
cell
'B'
It is reliable in most cases except when your class definition uses the old-style @ folder or if you query a non-MCOS class such as a Java class or such. On the side note, the accepted answer will not work for a qualified class such as above, which just shows that the NARGIN/NARGOUT functions are function-oriented are have not been adapted for the MCOS class system.

Christopher Hummersone
Christopher Hummersone on 18 Sep 2015
An indirect solution is to test for the existence of a particular variable:
exist('var_name','var')==1
This works for input variables; I haven't tested it for output variables.

  1 Comment

Madhav Rajan
Madhav Rajan on 2 Feb 2016
Although it helps, with exist you will need to the know the names of the variables that you want to track. NARGIN/NARGOUT makes it easier since you do not need any book keeping.

Sign in to comment.


Aruna Chhatkuli
Aruna Chhatkuli on 20 Jan 2021
This answer was flagged by Steven Lord
#include<stdio.h>
#define ROT 13
int main(void)
{
int c,e;
while((c=getchar())!=EOF)
{
if(c >='A' && c <='Z')
{
if((e = c + ROT) <= 'Z')
putchar(e);
else
{
e = c - ROT;
putchar(e);
}
}
else if(c >='a' && c <='z')
{
if((e= c + ROT) <= 'z')
putchar(e);
else
{
e = c - ROT;
putchar(e);
}
}
else
putchar(c);
}
return 0;
}

  3 Comments

Steven Lord
Steven Lord on 20 Jan 2021
This is unrelated to the original question. Please ask it as a separate question. When you ask it as a separate question, show what you've tried so far and you'll be more likely to receive a response.
Walter Roberson
Walter Roberson on 21 Jan 2021
That C code cannot be converted to MATLAB code. It uses getchar(), which reads from standard input, but MATLAB does not have standard input.

Sign in to comment.

Products


Release

R2010b

Community Treasure Hunt

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

Start Hunting!

Translated by