Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Fetching MATLAB function agrument list

Subject: Fetching MATLAB function agrument list

From: Roman

Date: 8 Aug, 2007 09:29:06

Message: 1 of 10

Is there a way in MATLAB to get a list of function
arguments (both input and output) as declared in M-file?

I.e. given a function
function [out1, out2] = foo(in1, in2, in3)
defined in the foo.m file I'm interested in something like

[input_arg_list, output_arg_list] = fetch_arg_list('foo.m');

Which would return
input_arg_list = {'in1', 'in2', 'in3'};
outpuy_arg_list = {'out1', 'out2'};

Thank you,
Roman




Subject: Fetching MATLAB function agrument list

From: Michael Wild

Date: 8 Aug, 2007 12:58:50

Message: 2 of 10

Roman wrote:
> Is there a way in MATLAB to get a list of function
> arguments (both input and output) as declared in M-file?
>
> I.e. given a function
> function [out1, out2] = foo(in1, in2, in3)
> defined in the foo.m file I'm interested in something like
>
> [input_arg_list, output_arg_list] = fetch_arg_list('foo.m');
>
> Which would return
> input_arg_list = {'in1', 'in2', 'in3'};
> outpuy_arg_list = {'out1', 'out2'};
>
> Thank you,
> Roman
>
>
>
>

no. you could do it yourself though quite easily. but what are you going
to do in the many cases where varargin and varargout are used? or where
default arguments are provided in the code? and what would be the use of
such a fragile thing be anyways?


michael

Subject: Fetching MATLAB function agrument list

From: Roman

Date: 8 Aug, 2007 16:11:01

Message: 3 of 10

Michael Wild <themiwi.REMOVE.THIS@student.ethz.ch> wrote in
message <46b9be5c$1@news1-rz-ap.ethz.ch>...
> Roman wrote:
> > Is there a way in MATLAB to get a list of function
> > arguments (both input and output) as declared in M-file?
> >
> > I.e. given a function
> > function [out1, out2] = foo(in1, in2, in3)
> > defined in the foo.m file I'm interested in something
like
> >
> > [input_arg_list, output_arg_list] = fetch_arg_list
('foo.m');
> >
> > Which would return
> > input_arg_list = {'in1', 'in2', 'in3'};
> > outpuy_arg_list = {'out1', 'out2'};
> >
> > Thank you,
> > Roman
> >
> >
> >
> >
>
> no. you could do it yourself though quite easily. but
what are you going
> to do in the many cases where varargin and varargout are
used? or where
> default arguments are provided in the code? and what
would be the use of
> such a fragile thing be anyways?
>
>
> michael

Hi,

I need this for some dynamic framework that would call
functions of certain type (no vararg-s) automatically using
'eval'. In order to compose the correct calling string for
the 'eval' I need to know the list of input and output
arguments to the function.
This is not a general purpose solution - just an automation
trick to make things easier for me.

By the way, do you know an easy and elegant way to parse
MATLAB source to fetch these?

Thank you,
Roman

Subject: Fetching MATLAB function agrument list

From: us

Date: 8 Aug, 2007 17:07:05

Message: 4 of 10

Roman:
<SNIP looking for a peek preview...

> input_arg_list = {'in1', 'in2', 'in3'};
> outpuy_arg_list = {'out1', 'out2'};

a hint:

% this is getting close
     help nargin;
     help nargout;
% eg,
     nargin('unique')
% ans = 3
     nargout('unique')
% ans = 3

us

Subject: Fetching MATLAB function agrument list

From: Roman

Date: 8 Aug, 2007 17:14:01

Message: 5 of 10

"us " <us@neurol.unizh.ch> wrote in message
<f9ct7p$qko$1@fred.mathworks.com>...
> Roman:
> <SNIP looking for a peek preview...
>
> > input_arg_list = {'in1', 'in2', 'in3'};
> > outpuy_arg_list = {'out1', 'out2'};
>
> a hint:
>
> % this is getting close
> help nargin;
> help nargout;
> % eg,
> nargin('unique')
> % ans = 3
> nargout('unique')
> % ans = 3
>
> us
>

nargin and nargout are good if you used them INSIDE the
function.
I'm talking about getting the argument list OUTSIDE the
function (without calling it).

Thank you,
Roman

Subject: Fetching MATLAB function agrument list

From: us

Date: 8 Aug, 2007 17:42:52

Message: 6 of 10

Roman:
<SNIP does not read replies properly...

> nargin and nargout are good if you used them INSIDE the
function...

you are so wrong - and obviously did not try the examples
that were shown... nor did you bother to read the help as
suggested...

% at the command prompt, type
     nargin('unique')

us

Subject: Fetching MATLAB function agrument list

From: Roman

Date: 8 Aug, 2007 18:01:27

Message: 7 of 10

"us " <us@neurol.unizh.ch> wrote in message
<f9cvar$hak$1@fred.mathworks.com>...
> Roman:
> <SNIP does not read replies properly...
>
> > nargin and nargout are good if you used them INSIDE the
> function...
>
> you are so wrong - and obviously did not try the examples
> that were shown... nor did you bother to read the help as
> suggested...
>
> % at the command prompt, type
> nargin('unique')
>
> us

yes, you're right - nargin can indeed return the number of
arguments.
Still, I'm interested in the list of input and output
arguments.

Thank you,
Roman

Subject: Fetching MATLAB function agrument list

From: us

Date: 8 Aug, 2007 19:49:22

Message: 8 of 10

Roman:
<SNIP a nag...

> I'm interested in the list of input and output
arguments...

well, then i guess you'll be interested in my utility
<farg>, which i will upload to the FEX...
it should be available in 1-2 days...

us

here's how an exemplary output looks like

     s=farg('grep');
     disp(s);

FILE F:\usr\matlab\unix\grep.m
  FUNCTION
  no/found line: definition
---------------------------------------------------------
   1/ 17 68: [pout,p]=grep(varargin)
   2/ 17 227: p=xhelp(p,fnam,tag)
   3/ 17 247: p=ini_grep(ver,tim,varargin)
   4/ 17 370: [p,msg]=set_opt(otbl,p,defopt,varargin)
   5/ 17 644: lst=get_arg(otbl,varargin)
   6/ 17 664: p=get_folders(p)
   7/ 17 673: p=get_folder(p,frot,crot,depth,ix)
   8/ 17 732: p=get_files(p)
   9/ 17 759: p=get_file(p)
  10/ 17 780: [tf,p]=chk_path(mode,p,fnam,frot)
  11/ 17 841: p=get_string(p)
  12/ 17 881: p=get_match(p,eol)
  13/ 17 1001: p=update(mode,p,varargin)
  14/ 17 1053: p=show_res(mode,p,varargin)
  15/ 17 1098: p=show_entry(mode,p,varargin)
  16/ 17 1233: p=ini_detab(opt)
  17/ 17 1247: ss=run_detab(ss,varargin)

Subject: Fetching MATLAB function agrument list

From: Yair Altman

Date: 8 Aug, 2007 20:46:28

Message: 9 of 10

> yes, you're right - nargin can indeed return the number of
> arguments.
> Still, I'm interested in the list of input and output
> arguments.


For plot-related functions (plot, line, bar, scatter etc.),
you can use the following highly-undocumented method
(substitute 'bar' by your function name):

args =
cell(com.mathworks.mlwidgets.graphics.PlotMetadata.getPlotSignature('bar').getArgs);
for argsIdx = 1 : length(args)
    axis = args{argsIdx}.getAxis;
    name = args{argsIdx}.getName;
    label = args{argsIdx}.getLabel;
    dims = args{argsIdx}.getNumDimensions;
    reqObj = args{argsIdx}.getRequired;
    requiredFlag = reqObj.equals(reqObj.REQUIRED);
end

type the following to see the full list of supported
functions for the above:
com.mathworks.mlwidgets.graphics.PlotMetadata.listAllSignatures;

Yair Altman
http://ymasoftware.com

Subject: Fetching MATLAB function agrument list

From: us

Date: 16 Aug, 2007 00:06:48

Message: 10 of 10

us:
<SNIP down to delay...

> > I'm interested in the list of input and output
> arguments...
>
> well, then i guess you'll be interested in my utility
> <farg>, which i will upload to the FEX...
> it should be available in 1-2 days...

well, it finally is available... here at

http://www.mathworks.com/matlabcentral/fileexchange/loadFile
.do?objectId=15924&objectType=FILE

http://www.mathworks.com/matlabcentral/files/15924/content/h
tml/farghtml.html

sorry for the delay
us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
undocumented Yair Altman 8 Aug, 2007 16:50:19
java Yair Altman 8 Aug, 2007 16:50:19
nargout us 8 Aug, 2007 13:08:50
nargin us 8 Aug, 2007 13:08:50
output us 8 Aug, 2007 13:08:50
input us 8 Aug, 2007 13:08:50
argument list parsing Roman 8 Aug, 2007 05:30:20
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics