Programming Fundamentals - The MathWorks - #401

/ 840


catalogue search
P. 401
P. 402
P. 403
P. 404
P. 405
P. 406
P. 407
P. 408
P. 409
P. 410
P. 411
P. 412
P. 413
P. 414
P. 415
P. 416
P. 417
P. 418
P. 419
P. 420
P. 421
P. 422
P. 423
P. 424
P. 425
P. 426
P. 427
P. 428
P. 429
P. 430
P. 431
P. 432
P. 433
P. 434
P. 435
P. 436
P. 437
P. 438
P. 439
P. 440
P. 441
P. 442
P. 443
P. 444
P. 445
P. 446
P. 447
P. 448
P. 449
P. 450


See other catalogues for The MathWorks

Text version of the page
Function Arguments
A similar function builds the varargout array using diagonals of a 5-by-5 matrix:
function varargout = byDiag(a)
varargout{1} = ' With VARARGOUT constructed by diagonal for k = -4:4
varargout{k + 6} = diag(a, k);
end
Call the function with five output variables. Again, MATLAB assigns elements of varargout according to the manner in which it was constructed within the function:
[text d1 d2 d3 d4] = byDiag(magic(5)) text =
With VARARGOUT constructed by diagonal ...
d1 =
11
d2 =
10 18
d3 =
4 12 25
d4 =
23 6
19
2
Checking the Number of Input Arguments
The nargin and nargout functions enable you to determine how many input and output arguments a function is called with. You can then use conditional statements to perform different tasks depending on the number ofarguments. For example,
function c = testarg1(a, b) if (nargin == 1) c= a.A2;
4-43

pageCatalog pdf di En 2012-06-22-01