Recent Changes - Search:

Basics

Languages

Tools

OS

Resources

PmWiki

pmwiki.org

edit SideBar

PassingArguments

Passing arguments allows you to call one function from within another function, without foreknowledge of how many arguments the user will supply. This is done by passing the array of inputs as InputArray{:} to the function being called.

For example:

h = plotRows(x,y,varargin)
%% Function to plot rows of a matrix

%Call plot for the transpose of x and y
%and give the rest to plot as properties for the lines
h(i) = plot(x',y',varargin{:})

This will create a function which you can call as:

h = plotRows(rand(10),rand(10))

To get 10 lines, which will all have default formatting. Or as:

h = plotRows(rand(10),rand(10),'LineWidth',2)

To create 10 lines of line width 2. You can supply however many properties like the line width, and they will all be passed to plot without having to change anything. Of course, this function is pointless in practice since you can call:

h = plot(rand(10)',rand(10)','LineWidth',2)

to get the same thing, unless you prefer to not have to think which way matlab plots a matrix. And that's exactly where this shines: Making wrappers so you don't have to think.

Edit - History - Print - Recent Changes - Search
Page last modified on January 20, 2017, at 03:59 PM