MATLAB Programming
MATLAB Programming MATLAB Programming
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
- 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 372 and 373: 4 M-File Programming addOptional(na
- Page 374 and 375: 4 M-File Programming % Display the
- 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
- Page 422 and 423: 5 Types of Functions You can now us
- Page 424 and 425: 5 Types of Functions Using a Subfun
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