MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 322 and 323: 3 Basic Program Components Cell Arr
- Page 324 and 325: 3 Basic Program Components Dynamic
- Page 326 and 327: 3 Basic Program Components The purp
- Page 328 and 329: 3 Basic Program Components Function
- Page 330 and 331: 3 Basic Program Components MATLAB F
- Page 332 and 333: 3 Basic Program Components Forcing
- Page 334 and 335: 4 M-File Programming Function Argum
- Page 336 and 337: 4 M-File Programming Saving the Pro
- Page 338 and 339: 4 M-File Programming Improving Perf
- Page 340 and 341: 4 M-File Programming Working with M
- Page 342 and 343: 4 M-File Programming Function Defin
- Page 344 and 345: 4 M-File Programming This is the fi
- Page 346 and 347: 4 M-File Programming The process lo
- Page 348 and 349: 4 M-File Programming Providing Help
- Page 350 and 351: 4 M-File Programming M-File Scripts
- Page 352 and 353: 4 M-File Programming Simple Functio
- Page 354 and 355: 4 M-File Programming 3 Type inmem t
- Page 356 and 357: 4 M-File Programming sqr = @(x) x.^
- Page 358 and 359: 4 M-File Programming Call plotFhand
- Page 360 and 361: 4 M-File Programming This false res
- Page 362 and 363: 4 M-File Programming Create two equ
- Page 364 and 365: 4 M-File Programming • Overloadin
- Page 366 and 367: 4 M-File Programming Here is a more
- Page 368 and 369: 4 M-File Programming axis([xmin fix
- Page 370 and 371: 4 M-File Programming This section c
- Page 374 and 375: 4 M-File Programming % Display the
- Page 376 and 377: 4 M-File Programming Arguments That
- Page 378 and 379: 4 M-File Programming Enabling Case-
- Page 380 and 381: 4 M-File Programming Property Funct
- Page 382 and 383: 4 M-File Programming Using nargin a
- Page 384 and 385: 4 M-File Programming 4-52
- Page 386 and 387: 4 M-File Programming Determining Wh
- Page 388 and 389: 4 M-File Programming 7 Function els
- Page 390 and 391: 4 M-File Programming While the comm
- Page 392 and 393: 4 M-File Programming strcmp str1 st
- Page 394 and 395: 4 M-File Programming On the other h
- Page 396 and 397: 4 M-File Programming City Temp. Hea
- Page 398 and 399: 4 M-File Programming function [vout
- Page 400 and 401: 4 M-File Programming Running Extern
- Page 402 and 403: 5 Types of Functions Overview of MA
- Page 404 and 405: 5 Types of Functions Note Function
- Page 406 and 407: 5 Types of Functions Arrays of Anon
- Page 408 and 409: 5 Types of Functions Display the da
- Page 410 and 411: 5 Types of Functions If you change
- Page 412 and 413: 5 Types of Functions • “Example
- Page 414 and 415: 5 Types of Functions Supply the val
- Page 416 and 417: 5 Types of Functions Nested Functio
- Page 418 and 419: 5 Types of Functions • From a fun
- Page 420 and 421: 5 Types of Functions not shared bet
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