MATLAB Programming
MATLAB Programming MATLAB Programming
4 M-File Programming Here is a more advanced example that finds the first token in a character string. A token is a set of characters delimited by white space or some other character. Given one input, the function assumes a default delimiter of white space; given two, it lets you specify another delimiter if desired. It also allows for two possible output argument lists: function [token, remainder] = strtok(string, delimiters) % Function requires at least one input argument if nargin < 1 error('Not enough input arguments.'); end token = []; remainder = []; len = length(string); if len == 0 return end % If one input, use white space delimiter if (nargin == 1) delimiters = [9:13 32]; % White space characters end i = 1; % Determine where nondelimiter characters begin while (any(string(i) == delimiters)) i = i + 1; if (i > len), return, end end % Find where token ends start = i; while (~any(string(i) == delimiters)) i = i + 1; if (i > len), break, end end finish = i - 1; token = string(start:finish); % For two output arguments, count characters after % first delimiter (remainder) 4-34
Function Arguments if (nargout == 2) remainder = string(finish+1:end); end The strtok function is a MATLAB M-file in the strfun directory. Note The order in which output arguments appear in the function declaration line is important. The argument that the function returns in most cases appears first in the list. Additional, optional arguments are appended to the list. Passing Variable Numbers of Arguments The varargin and varargout functions let you pass any number of inputs or return any number of outputs to a function. This section describes how to use these functions and also covers • “Unpacking varargin Contents” on page 4-36 • “Packing varargout Contents” on page 4-36 • “varargin and varargout in Argument Lists” on page 4-37 MATLAB packs all specified input arguments into a cell array, aspecialkind of MATLAB array that consists of cells instead of array elements. Each cell can hold any size or kind of data — one might hold a vector of numeric data, another in the same array might hold an array of string data, and so on. For output arguments, your function code must pack them into a cell array so that MATLABcanreturntheargumentstothecaller. Here is an example function that accepts any number of two-element vectors anddrawsalinetoconnectthem: function testvar(varargin) for k = 1:length(varargin) x(k) = varargin{k}(1); % Cell array indexing y(k) = varargin{k}(2); end xmin = min(0,min(x)); ymin = min(0,min(y)); 4-35
- Page 316 and 317: 3 Basic Program Components Program
- Page 318 and 319: 3 Basic Program Components Asterisk
- Page 320 and 321: 3 Basic Program Components Numeric
- 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 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 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
Function Arguments<br />
if (nargout == 2)<br />
remainder = string(finish+1:end);<br />
end<br />
The strtok function is a <strong>MATLAB</strong> M-file in the strfun directory.<br />
Note The order in which output arguments appear in the function declaration<br />
line is important. The argument that the function returns in most cases<br />
appears first in the list. Additional, optional arguments are appended to<br />
the list.<br />
Passing Variable Numbers of Arguments<br />
The varargin and varargout functions let you pass any number of inputs<br />
or return any number of outputs to a function. This section describes how to<br />
use these functions and also covers<br />
• “Unpacking varargin Contents” on page 4-36<br />
• “Packing varargout Contents” on page 4-36<br />
• “varargin and varargout in Argument Lists” on page 4-37<br />
<strong>MATLAB</strong> packs all specified input arguments into a cell array, aspecialkind<br />
of <strong>MATLAB</strong> array that consists of cells instead of array elements. Each cell<br />
can hold any size or kind of data — one might hold a vector of numeric data,<br />
another in the same array might hold an array of string data, and so on. For<br />
output arguments, your function code must pack them into a cell array so that<br />
<strong>MATLAB</strong>canreturntheargumentstothecaller.<br />
Here is an example function that accepts any number of two-element vectors<br />
anddrawsalinetoconnectthem:<br />
function testvar(varargin)<br />
for k = 1:length(varargin)<br />
x(k) = varargin{k}(1); % Cell array indexing<br />
y(k) = varargin{k}(2);<br />
end<br />
xmin = min(0,min(x));<br />
ymin = min(0,min(y));<br />
4-35