File Exchange 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/

12 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.

  7. siva replied on :

    i have a doubt in using varargin. if i give this as it is in the calling function, i get “Attempted to execute script varargin as a function”. if i omit varargin, it gives the error as “One or more output arguments not assigned during call to…”. what should i have to do? plz send the answer to my mail soon. thanks in advance

  8. Doug replied on :

    Siva,

    Please post the actual code, the function, and how you call it. You can simplify the function so it is shorter for this purpose.

    Doug

  9. Seun replied on :

    Hi Doug,

    Sorry to trouble you, I am trying to plot a 3axis(XYZ) bar graph with some data I have in excel, would attach the data here but it doesn’t seem possible to.

    x-Height
    y-Ergo_Index
    z-Distance

    I also want to plot the mean, standard deviation +/- of the Ergo_Index on the same graph.

    Could you point me in the right direction, I am not too good at matlab, I have tried the gui method to do this but never seem to get a suitable graph or one in 3d either.

    Thanks

  10. Doug replied on :

    Seun,

    There are a few steps to this process, it is not clear to me what you are having problems with.

    1.) Import data from Excel.
    http://blogs.mathworks.com/videos/category/excel/

    2.) Doing a 3-d bar graph.
    http://www.mathworks.com/matlabcentral/fileexchange/23439

    3.) Adding more visualization to a standard plot type.
    http://www.mathworks.com/access/helpdesk/help/techdoc/creating_plots/f7-20419.html

    What have you tried, and specifically where are you stuck?

    -Doug

  11. Seun replied on :

    Thanks for your reply, I tried the steps you had in your tutorial, I actually don’t know how to use matlab so I am learning on the go.

    I have been able to import the data but when i try to plot the graph i seem to get line graphs which are not correct.

    I have posted sample data below so i can explain better what i am trying to plot

    I will go over the links you sent again.

    Thanks

    x-Height
    y-Ergo_Index, Ergo_Index_+Sta. Dev., Ergo_Index_-Sta. Dev.
    z-Distance

    Height Distance Ergo_Index Eg_Index_+SDev. Erg_Index_-SDev.
    1 1 8.02810 8.48252 7.57368
    1 1 7.23410 7.68852 6.77968
    1 1 6.77290 7.22732 6.31848
    1 1 7.26650 7.72092 6.81208
    1 1 7.85150 8.30592 7.39708
    1 1 7.44710 7.90152 6.99268
    1 2 6.85090 7.92199 5.77981
    1 2 7.54950 8.62059 6.47841
    1 2 8.84650 9.91759 7.77541
    1 2 7.35340 8.42449 6.28231
    1 2 9.51260 10.58369 8.44151
    1 2 8.99790 10.06899 7.92681
    1 3 7.90830 8.11751 7.69909
    1 3 7.92300 8.13221 7.71379
    1 3 7.99560 8.20481 7.78639
    1 3 8.15680 8.36601 7.94759
    1 3 7.87130 8.08051 7.66209
    1 3 8.08800 8.29721 7.87879
    1 3 8.34180 8.55101 8.13259
    1 3 7.64370 7.85291 7.43449
    2 1 9.67030 10.07334 9.26726
    2 1 8.95520 9.35824 8.55216
    2 1 8.68640 9.08944 8.28336
    2 1 8.75610 9.15914 8.35306
    2 1 8.99480 9.39784 8.59176
    2 1 9.44720 9.85024 9.04416
    2 1 9.73000 10.13304 9.32696
    2 1 9.30680 9.70984 8.90376
    2 2 7.59970 7.85001 7.34939
    2 2 7.38770 7.63801 7.13739
    2 2 7.45730 7.70761 7.20699
    2 2 7.86410 8.11441 7.61379
    2 2 7.61410 7.86441 7.36379
    2 2 7.70390 7.95421 7.45359
    2 2 8.15410 8.40441 7.90379
    2 2 7.87890 8.12921 7.62859
    3 3 7.28780 7.86754 6.70806
    3 3 7.57240 8.15214 6.99266
    3 3 8.52170 9.10144 7.94196
    3 3 7.88650 8.46624 7.30676
    3 3 7.39120 7.97094 6.81146
    3 3 8.50380 9.08354 7.92406
    3 3 8.30720 8.88694 7.72746
    3 3 8.81800 9.39774 8.23826

  12. Doug replied on :

    Seun,

    For us to give you some help, you will need to show us what you tried, in code. There are many things that could have been done incorrectly. The data you have shown would plot well.

    Doug

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Zach: Hi Doug and Les, I didn’t have a lot of time to mess with this, but I did find a work-around. I plotted...
  • hamed: k
  • Les: @Zach This isn’t exactly what you are looking for but at least it puts all three parameters on the same...
  • Zach: Thanks for your suggestions Doug. I’ll give that a shot and see what happens. I’ve seen many of...
  • Doug: @Zach, I would say to use plotYYY, because that is close to what you want, but using depth as Y makes sense....
  • Doug: @Teja, I think this will work: http://www.mathworks .com/access/helpdesk /help/techdoc/ref...
  • Gify: merry christmas :) nice christmas tree! Regards, Janet Gify
  • Teja: Dear Doug Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i...
  • Zach: I’m working with geophysical data, so I’d like to produce a depth profile. The y-axis would be...
  • Doug: @Ashok First, please do not use variable names that are MATLAB commands (std and mean). Second, p(j) should be...

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