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 Arguments That Default Any arguments that you do not include in a call to your function are given their default values by MATLAB. You defined these default values when you created your schema using the addOptional and addParamValue methods. The UsingDefaults property is actually a structure that contains the names of any arguments that were not passed in the function call, and thus were assigned default values. Add the following to your M-file: % Show which arguments were not specified in the call. disp(' ') disp 'List of arguments given default values:' for k=1:numel(p.UsingDefaults) field = char(p.UsingDefaults(k)); value = p.Results.(field); if isempty(value), value = '[]'; end disp(sprintf(' ''%s'' defaults to %s', field, value)) end Save the M-file and run it without specifying the format, outputDir, or maxHeight arguments: publish_ip('ipscript.m', 'maxWidth', 500); List of arguments given default values: 'format' defaults to html 'outputDir' defaults to D:\work_r14 'maxHeight' defaults to [] Validating the Input Arguments When you call your function, MATLAB checks any arguments for which you have specified a validator function. If the validator finds an error, MATLAB displays an error message and aborts the function. In the publish function example, the outputDir argument validates the value passed in using @ischar. Pass a number instead of a string for the outputDir argument: 4-44

Function Arguments publish_ip('ipscript.m', 'outputDir', 5); ??? Argument 'outputDir' failed validation ischar. Error in ==> publish_ip at 14 p.parse(varargin{:}); Handling Unmatched Arguments. MATLAB throws an error if you call your function with any arguments that are not part of the inputParser schema. You can disable this error by setting the KeepUnmatched property to true. WhenKeepUnmatched is in the true state, MATLAB does not throw an error, but instead stores any arguments that are not in the schema in a cell array of strings accessible through the Unmatched property of the object. KeepUnmatched defaults to false. At some point in your publish_ip M-file before executing the parse method, set the KeepUnmatched property to true, and following the parse statement, examine the Unmatched property: p.KeepUnmatched = true; % Parse and validate all input arguments. p.parse(script, varargin{:}); disp(' ') disp 'List of unmatched arguments:' p.Unmatched Save and run the function, passing two arguments that are not defined in the schema: publish_ip('ipscript.m', s, ... 'outputDir', 'C:/matlab/R2007a/temp', ... 'colorSpace', 'CMYK', 'density', 200); List of unmatched arguments: colorSpace: 'CMYK' density: 200 4-45

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

Arguments That Default<br />

Any arguments that you do not include in a call to your function are given<br />

their default values by <strong>MATLAB</strong>. You defined these default values when you<br />

created your schema using the addOptional and addParamValue methods.<br />

The UsingDefaults property is actually a structure that contains the names<br />

of any arguments that were not passed in the function call, and thus were<br />

assigned default values.<br />

Add the following to your M-file:<br />

% Show which arguments were not specified in the call.<br />

disp(' ')<br />

disp 'List of arguments given default values:'<br />

for k=1:numel(p.UsingDefaults)<br />

field = char(p.UsingDefaults(k));<br />

value = p.Results.(field);<br />

if isempty(value), value = '[]'; end<br />

disp(sprintf(' ''%s'' defaults to %s', field, value))<br />

end<br />

Save the M-file and run it without specifying the format, outputDir, or<br />

maxHeight arguments:<br />

publish_ip('ipscript.m', 'maxWidth', 500);<br />

List of arguments given default values:<br />

'format' defaults to html<br />

'outputDir' defaults to D:\work_r14<br />

'maxHeight' defaults to []<br />

Validating the Input Arguments<br />

When you call your function, <strong>MATLAB</strong> checks any arguments for which you<br />

have specified a validator function. If the validator finds an error, <strong>MATLAB</strong><br />

displays an error message and aborts the function. In the publish function<br />

example, the outputDir argument validates the value passed in using<br />

@ischar.<br />

Pass a number instead of a string for the outputDir argument:<br />

4-44

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

Saved successfully!

Ooh no, something went wrong!