23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Function Arguments<br />

• addParamValue — Add an optional parameter-value pair to the schema<br />

Creating the inputParser Object. Call the class constructor for<br />

inputParser to create an instance of the class. This class instance, or object,<br />

gives you access to all of the methods and properties of the class.<br />

Begin writing the example publish_ip M-file by entering the following two<br />

statements:<br />

function x = publish_ip(script, varargin)<br />

p = inputParser; % Create an instance of the class.<br />

After calling the constructor, use the addRequired, addOptional, and<br />

addParamValue methods to add arguments to the schema.<br />

Note The constructor and all methods and properties of the inputParser<br />

class are case sensitive.<br />

Adding Arguments to the Schema. Add any required arguments to the<br />

schema using the addRequired method. This method takes two inputs: the<br />

name of the required parameter, and an optional handle to a function that<br />

validates the parameter:<br />

addRequired(name, validator);<br />

Put an addRequired statement at the end of your publish_ip code. The two<br />

arguments for addRequired in this case are the filename input, script, anda<br />

handle to a function that will validate the filename, ischar. Afteraddingthe<br />

addRequired statement, your publish_ip function should now look like this:<br />

function x = publish_ip(script, varargin)<br />

p = inputParser; % Create an instance of the class.<br />

p.addRequired('script', @ischar);<br />

Use the addOptional method to add any arguments that are not required.<br />

The syntax for addOptional is similar to that of addRequired except that<br />

youalsoneedtospecifyadefaultvaluetobeusedwhenevertheoptional<br />

argument is not passed:<br />

4-39

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!