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

Doug’s Pick of the Week

January 8th, 2008

Advanced MATLAB: Varargin and Nargin- variable inputs to a function

Most MATLAB functions are overloaded. That means that there are several ways to call them and that MATLAB will “Do The Right Thing” based on the number and types of inputs. As a simple example you can call SUM

sum(M) %this will sum along the columns
sum(M,2) %this will sum along the rows

This video shows how you can implement the same behavior based on using

VARARGIN: VARiable ARGuments IN
NARGIN: Number ARGuments IN


Video Content

Find the files here.
PodCast here

Other videos have been gathered here:
http://blogs.mathworks.com/pick/category/video/

Other Advanced MATLAB posts have been gathered here:
http://blogs.mathworks.com/pick/category/advanced-matlab/

6 Responses to “Advanced MATLAB: Varargin and Nargin- variable inputs to a function”

  1. Justin replied on :

    Can your output ever be a text and not a number?

  2. Markus replied on :

    Hi!

    I sometimes use “exist” in order to realize variable inputs:

    function bla(A,B,D)
    if ~exist(’D', ‘var’)
    D = 10; % default value
    end

    Here, I did not hard-code the order and number of input arguments. If I ever want to introduce an additional input C before D, like

    function bla(A,B,C,D)

    I do not have to change anything (except the function calls of course). I think both alternatives have their pros and cons.

    Regards
    Markus

  3. Doug replied on :

    Justin,

    You can use num2str to make a number into text. You can have a function output be string if that is what you want.

    Doug

  4. Luca Balbi replied on :

    Hello Doug, great work as always!

    I think that the recent introduction of the inputParser object has made things evolve a bit with respect to the nargin approach.
    Have you ever thought about creating a video with a demo of inputParser?

  5. dylan replied on :

    i followed your code exactly and i get the following error:
    >> main(1)
    ??? Undefined function or method ‘mtimes’ for input arguments of type ‘cell’.

    Error in ==> main at 19
    out=alpha * beta;

  6. Jiro replied on :

    dylan,

    Make sure you are using (curly) braces and not parentheses:

    alpha = varargin{1}

    “varargin” is a cell array, so you need to use braces to get the contents of each element. Parentheses will only give you back the element as a cell.

Leave a Reply


Doug Hull is an Application Engineer at The MathWorks. A MATLAB user since 1994, he gets paid to live, eat, and breathe MATLAB! This blog is dedicated to promoting the File Exchange by highlighting files and original video content.



  • pierre: Hi sherryl and thank you for answering me, Actually, I already tried before to use this property because I...
  • Sherryl: In Response to Post #10 by Bryan - Hi Bryan, By default the analog input object will acquire one second...
  • Sherryl: Hello Pierre, Please look at the OutOfDataMode property. http://www.mathworks .com/access/helpd...
  • Scott Hirsch: Elya - In v7.0, try aviread. This has straightforward syntax for reading a single frame - you could...
  • Scott Hirsch: Eric - That’s a nice suggestion. I often get frustrated when debugging GUIDE guis and ending up...
  • pierre: Hi all, I have the Data Acquisition Toolbox, and I’m trying for 2 weeks to send a step voltage, and...
  • Eric S: It would be great to stop the debugger from coughing somewhere inside the more “internal̶ 1;...
  • Tareq: coef1 = rand(1,3)-0.5; coef2 = rand(1,3)-0.5; lex1=roots(polyder(c oef1)) lex2=roots(polyder(c oef2)) hold all...
  • Luca Balbi: While we’re at it… Checking for a number to be zero is tricky in itself. We’re better...
  • david: perhaps some error checking is in order. After all, it is possible that our randomly generated quadratic...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics