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 Working with M-Files MATLAB provides a full programming language that enables you to write a series of MATLAB statements into a file and then execute them with a single command. You write your program in an ordinary text file, giving the file anameoffilename.m. Thetermyouusefor filename becomes the new command that MATLAB associates with the program. The file extension of .m makesthisaMATLABM-file. This section covers • “Types of M-Files” on page 4-8 • “Basic Parts of an M-File” on page 4-9 • “Creating a Simple M-File” on page 4-13 • “ProvidingHelpforYourProgram”onpage4-16 • “Creating P-Code Files” on page 4-16 TypesofM-Files M-files can be scripts that simply execute a series of MATLAB statements, or they can be functions that also accept input arguments and produce output. MATLAB scripts: • Are useful for automating a series of steps you need to perform many times. • Do not accept input arguments or return output arguments. • Store variables in a workspace that is shared with other scripts and with the MATLAB command line interface. MATLAB functions: • Are useful for extending the MATLAB language for your application. • Can accept input arguments and return output arguments. • Store variables in a workspace internal to the function. 4-8

Working with M-Files Basic Parts of an M-File This simple function shows the basic parts of an M-file. Note that any line that begins with % is not executable: function f = fact(n) Function definition line % Compute a factorial value. H1 line % FACT(N) returns the factorial of N, Help text % usually denoted by N! % Put simply, FACT(N) is PROD(1:N). Comment f = prod(1:n); Function body The table below briefly describes each of these M-file parts. Both functions and scripts can have all of these parts, except for the function definition line which applies to functions only. These parts are described in greater detail following the table. M-File Element Function definition line (functions only) H1 line Help text Function or script body Comments Description Defines the function name, and the number and order of input and output arguments A one line summary description of the program, displayed when you request help on an entire directory, or when you use lookfor A more detailed description of the program, displayed together with the H1 line when you request help on a specific function Program code that performs the actual computations and assigns values to any output arguments Text in the body of the program that explains the internal workings of the program 4-9

Working with M-Files<br />

Basic Parts of an M-File<br />

This simple function shows the basic parts of an M-file. Note that any line<br />

that begins with % is not executable:<br />

function f = fact(n)<br />

Function<br />

definition line<br />

% Compute a factorial value. H1 line<br />

% FACT(N) returns the factorial of N, Help text<br />

% usually denoted by N!<br />

% Put simply, FACT(N) is PROD(1:N). Comment<br />

f = prod(1:n);<br />

Function body<br />

The table below briefly describes each of these M-file parts. Both functions<br />

and scripts can have all of these parts, except for the function definition line<br />

which applies to functions only. These parts are described in greater detail<br />

following the table.<br />

M-File Element<br />

Function definition line<br />

(functions only)<br />

H1 line<br />

Help text<br />

Function or script body<br />

Comments<br />

Description<br />

Defines the function name, and the number and<br />

order of input and output arguments<br />

A one line summary description of the program,<br />

displayed when you request help on an entire<br />

directory, or when you use lookfor<br />

A more detailed description of the program,<br />

displayed together with the H1 line when you<br />

request help on a specific function<br />

Program code that performs the actual<br />

computations and assigns values to any output<br />

arguments<br />

Text in the body of the program that explains<br />

the internal workings of the program<br />

4-9

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

Saved successfully!

Ooh no, something went wrong!