Variable number of input arguments - function, MATLAB Programming

Variable number of input arguments:

For illustration, the below function areafori has a variable number of input arguments, either the 1 or 2. The name of the function stands for 'area, feet or inches.' When only one argument is passed to the function, it shows the radius in feet. When two arguments are passed, the second can be a character 'i' specifying that the result must be in inches (for any of other character, the default of feet is supposed). The function uses the built-in varargin that stores as a cell array of any number of input arguments. The nargin function returns a number of input arguments which were passed to the function. In this situation, the radius is the first argument passed; therefore it is stored in first element in the varargin. When a second argument is passed whenever nargin is 2, it is to identifyspecify the units.

1447_Variable number of input arguments.png

Note that the curly braces are used to refer the elements in the cell array varargin.

Here are some illustrations of calling this function:

>> areafori(3)

ans =

  28.2743

>> areafori(1,'i')

ans =

  452.3893

Posted Date: 10/22/2012 1:15:59 AM | Location : United States







Related Discussions:- Variable number of input arguments - function, Assignment Help, Ask Question on Variable number of input arguments - function, Get Answer, Expert's Help, Variable number of input arguments - function Discussions

Write discussion on Variable number of input arguments - function
Your posts are moderated
Related Questions
how to reverse the digits of positive number by using mod function?

khdjkafhoi woiruwoi epojrpeo

Problem 1:   What are the different methods that can be used to solve a differential equation? Please provide a numbered list. Problem 2:   For a first order linear d


Make a vector of 500 random numbers from a normal distribution with mean 2 and standard deviation 5(randn). a After you generate the vector, verify that the sample mean and stan

Generate a script: To generate a script, click File, then New, and then M-file. The new window will appear known as the Editor. To generate a new script, simply type the serie

how do you make a program for a parachute man falling to determine his terminal velocity, having in consideration the drag force (cv^2). I have the formula to be v(i+1)=v(i)*delta(

Illustration of script: For illustration, we will now generate a script known as script1.m which calculates area of the circle. It assigns a value for the radius, and then com

You are asked to create an american option multiplicative binomial model calculator in MatLab. Both put and call options should be valued. Given u, d, S 0 , K, r, and T (the usual

2)''dbcont'' command for debugging is used to Select one: a. Continue normal code execution from the debug prompt. b. List all breakpoints. c. Execute (step) one or more lines