MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

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

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!