MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

4 M-File Programming addOptional(name, default, validator); In this case, the validator input is a handle to an anonymous function: p.addOptional('format', 'html', ... @(x)any(strcmpi(x,{'html','ppt','xml','latex'}))); Use addParamValue to specify any arguments that use a parameter-value format. The syntax is addParamValue(name, default, validator); For example, p.addParamValue('outputDir', pwd, @ischar); p.addParamValue('maxHeight', [], @(x)x>0 && mod(x,1)==0); p.addParamValue('maxWidth', [], @(x)x>0 && mod(x,1)==0); Listing the Arguments. At this point, the schema is complete. Here is the file publish_ip.m: function x = publish_ip(script, varargin) p = inputParser; % Create an instance of the class. p.addRequired('script', @ischar); p.addOptional('format', 'html', ... @(x)any(strcmpi(x,{'html','ppt','xml','latex'})));p. p.addParamValue('outputDir', pwd, @ischar); p.addParamValue('maxHeight', [], @(x)x>0 && mod(x,1)==0); p.addParamValue('maxWidth', [], @(x)x>0 && mod(x,1)==0); When you call the program, MATLAB stores the name of each argument in the Parameters property of object p. Addthefollowingtwolinestoyour publish_ip M-file to display p.Parameters: sprintf('%s\n %s\n %s\n %s\n %s\n %s', ... 'The input parameters for this program are:', ... p.Parameters{:}) 4-40

Function Arguments Save the M-file, and then run it as shown here: publish_ip('ipscript.m', 'ppt', 'outputDir', ... 'C:/matlab/test', 'maxWidth', 500, 'maxHeight', 300); The output is The input parameters for this program are: format maxHeight maxWidth outputDir script Parsing Parameter Values on the Function Call Once you have constructed a schema that represents all possible inputs to the function, the next task is to write the code that parses and verifies these arguments whenever the function is called. The parse method of the inputParser class reads and validates the required script argument and any optional arguments that follow it in the argument list: p.parse(script, varargin{:}); Execution of the parse method validates each argument and also builds a structure from the input arguments. The name of the structure is Results, which is accessible as a property of the object. To get the value of all arguments, type p.Results To get the value of any single input argument, type p.Results.argname where argname is the name of the argument. Continue with the publish_ip exercise started earlier in this section by removing the sprintf statement that was inserted in the last section, and then adding the following lines: % Parse and validate all input arguments. p.parse(script, varargin{:}); 4-41

4 M-File <strong>Programming</strong><br />

addOptional(name, default, validator);<br />

In this case, the validator input is a handle to an anonymous function:<br />

p.addOptional('format', 'html', ...<br />

@(x)any(strcmpi(x,{'html','ppt','xml','latex'})));<br />

Use addParamValue to specify any arguments that use a parameter-value<br />

format. The syntax is<br />

addParamValue(name, default, validator);<br />

For example,<br />

p.addParamValue('outputDir', pwd, @ischar);<br />

p.addParamValue('maxHeight', [], @(x)x>0 && mod(x,1)==0);<br />

p.addParamValue('maxWidth', [], @(x)x>0 && mod(x,1)==0);<br />

Listing the Arguments. At this point, the schema is complete. Here is the<br />

file publish_ip.m:<br />

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

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

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

p.addOptional('format', 'html', ...<br />

@(x)any(strcmpi(x,{'html','ppt','xml','latex'})));p.<br />

p.addParamValue('outputDir', pwd, @ischar);<br />

p.addParamValue('maxHeight', [], @(x)x>0 && mod(x,1)==0);<br />

p.addParamValue('maxWidth', [], @(x)x>0 && mod(x,1)==0);<br />

When you call the program, <strong>MATLAB</strong> stores the name of each argument<br />

in the Parameters property of object p. Addthefollowingtwolinestoyour<br />

publish_ip M-file to display p.Parameters:<br />

sprintf('%s\n %s\n %s\n %s\n %s\n %s', ...<br />

'The input parameters for this program are:', ...<br />

p.Parameters{:})<br />

4-40

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

Saved successfully!

Ooh no, something went wrong!