MATLAB Programming
MATLAB Programming MATLAB Programming
4 M-File Programming On the other hand, variables that contain strings do not need to be enclosed in quotes: dirname = 'myapptests'; mkdir(dirname) Passing Filenames You can specify a filename argument using the MATLAB command or function syntax. For example, either of the following are acceptable. (The .mat file extension is optional for save and load): load mydata.mat load('mydata.mat') % Command syntax % Function syntax Ifyouassigntheoutputtoavariable,youmustusethefunctionsyntax: savedData = load('mydata.mat') Specify ASCII files as shown here. In this case, the file extension is required: load mydata.dat -ascii load('mydata.dat','-ascii') % Command syntax % Function syntax Determining Filenames at Run-Time. There are several ways that your function code can work on specific files without your having to hardcode their filenames into the program. You can • Passthefilenameasanargument: function myfun(datafile) • Prompt for the filename using the input function: filename = input('Enter name of file: ', 's'); • Browse for the file using the uigetfile function: [filename, pathname] = uigetfile('*.mat', 'Select MAT-file'); 4-62
Calling Functions Passing Function Handles The MATLAB function handle has several uses, the most common being a means of immediate access to the function it represents. You can pass function handles in argument lists to other functions, enabling the receiving function to make calls by means of the handle. To pass a function handle, include its variable name in the argument list of the call: fhandle = @humps; x = fminbnd(fhandle, 0.3, 1); The receiving function invokes the function being passed using the usual MATLAB calling syntax: function [xf, fval, exitflag, output] = ... fminbnd(fhandle, ax, bx, options, varargin) . . . 113 fx = fhandle(x, varargin{:}); Passing Arguments in Structures or Cell Arrays Instead of requiring an additional argument for every value you want to pass in a function call, you can package them in a MATLAB structure or cell array. Passing Arguments in a Structure Make each input you want to pass a separate field in the structure argument, using descriptive names for the fields. Structures allow you to change the number, contents, or order of the arguments without having to modify the function. They can also be useful when you have a number of functions that need similar information. This example updates weather statistics from information in the following chart. 4-63
- 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 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 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
- Page 432 and 433: 5 Types of Functions 0.6667 filt2(1
- Page 434 and 435: 5 Types of Functions unless you dec
- Page 436 and 437: 5 Types of Functions Accessing Help
- Page 438 and 439: 5 Types of Functions 5-38
- Page 440 and 441: 6 Data Import and Export Working wi
- Page 442 and 443: 6 Data Import and Export • “Dat
Calling Functions<br />
Passing Function Handles<br />
The <strong>MATLAB</strong> function handle has several uses, the most common being<br />
a means of immediate access to the function it represents. You can pass<br />
function handles in argument lists to other functions, enabling the receiving<br />
function to make calls by means of the handle.<br />
To pass a function handle, include its variable name in the argument list of<br />
the call:<br />
fhandle = @humps;<br />
x = fminbnd(fhandle, 0.3, 1);<br />
The receiving function invokes the function being passed using the usual<br />
<strong>MATLAB</strong> calling syntax:<br />
function [xf, fval, exitflag, output] = ...<br />
fminbnd(fhandle, ax, bx, options, varargin)<br />
.<br />
.<br />
.<br />
113 fx = fhandle(x, varargin{:});<br />
Passing Arguments in Structures or Cell Arrays<br />
Instead of requiring an additional argument for every value you want to pass<br />
in a function call, you can package them in a <strong>MATLAB</strong> structure or cell array.<br />
Passing Arguments in a Structure<br />
Make each input you want to pass a separate field in the structure argument,<br />
using descriptive names for the fields. Structures allow you to change the<br />
number, contents, or order of the arguments without having to modify the<br />
function. They can also be useful when you have a number of functions that<br />
need similar information.<br />
This example updates weather statistics from information in the following<br />
chart.<br />
4-63