MATLAB Programming
MATLAB Programming MATLAB Programming
4 M-File Programming Using nargin and nargout When nargin or nargout appears in a nested function, it refers to the number of inputs or outputs passed to that particular function, regardless of whether ornotitisnested. In the example shown above, nargin in function A is the number of inputs passed to A, andnargin in function C is the number of inputs passed to C. Ifa nested function needs the value of nargin or nargout from an outer function, youcanpassthisvalueinasaseparateargument,asdoneinfunctionB. Example of Passing Optional Arguments to Nested Functions This example references the primary function’s varargin cell array from each of two nested functions. (Because the workspace of an outer function is shared with all functions nested within it, there is no need to pass varargin to the nested functions.) Both nested functions make use of the nargin value that applies to the primary function. Calling nargin from the nested function would return the number of inputs passed to that nested function, and not those that had been passed to the primary. For this reason, the primary function must pass its nargin value to the nested functions. function meters = convert2meters(miles, varargin) % Converts MILES (plus optional FEET and INCHES input) % values to METERS. if nargin < 1 || nargin > 3 error('1 to 3 input arguments are required'); end function feet = convert2Feet(argsIn) % Nested function that converts miles to feet and adds in % optional FEET argument. feet = miles .* 5280; if argsIn >= 2 feet = feet + varargin{1}; end 4-50
Function Arguments end % End nested function convert2Feet function inches = convert2Inches(argsIn) % Nested function that converts feet to inches and adds in % optional INCHES argument. inches = feet .* 12; if argsIn == 3 inches = inches + varargin{2}; end end % End nested function convert2Inches feet = convert2Feet(nargin); inches = convert2Inches(nargin); meters = inches .* 2.54 ./ 100; end % End primary function convert2meters convert2meters(5) ans = 8.0467e+003 convert2meters(5, 2000, 4.7) ans = 8.6564e+003 Returning Modified Input Arguments If you pass any input variables that the function can modify, you will need to include the same variables as output arguments so that the caller receives the updated value. For example, if the function readText, shown below, reads one line of a file each time is it called, then it must keep track of the offset into the file. But when readText terminates, its copy of the offset variable is cleared from memory. To keep the offset value from being lost, readText must return this value to the caller: function [text, offset] = readText(filestart, offset) 4-51
- 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 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 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
- Page 426 and 427: 5 Types of Functions count = 125 22
- Page 428 and 429: 5 Types of Functions shape. The mak
- Page 430 and 431: 5 Types of Functions % FILTFCN. Eac
Function Arguments<br />
end<br />
% End nested function convert2Feet<br />
function inches = convert2Inches(argsIn)<br />
% Nested function that converts feet to inches and adds in<br />
% optional INCHES argument.<br />
inches = feet .* 12;<br />
if argsIn == 3<br />
inches = inches + varargin{2};<br />
end<br />
end % End nested function convert2Inches<br />
feet = convert2Feet(nargin);<br />
inches = convert2Inches(nargin);<br />
meters = inches .* 2.54 ./ 100;<br />
end % End primary function convert2meters<br />
convert2meters(5)<br />
ans =<br />
8.0467e+003<br />
convert2meters(5, 2000, 4.7)<br />
ans =<br />
8.6564e+003<br />
Returning Modified Input Arguments<br />
If you pass any input variables that the function can modify, you will need to<br />
include the same variables as output arguments so that the caller receives<br />
the updated value.<br />
For example, if the function readText, shown below, reads one line of a file<br />
each time is it called, then it must keep track of the offset into the file. But<br />
when readText terminates, its copy of the offset variable is cleared from<br />
memory. To keep the offset value from being lost, readText must return this<br />
value to the caller:<br />
function [text, offset] = readText(filestart, offset)<br />
4-51