MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 290 and 291: 3 Basic Program Components Find con
- Page 292 and 293: 3 Basic Program Components statemen
- Page 294 and 295: 3 Basic Program Components Characte
- Page 296 and 297: 3 Basic Program Components Quantifi
- Page 298 and 299: 3 Basic Program Components Named To
- Page 300 and 301: 3 Basic Program Components Comma-Se
- Page 302 and 303: 3 Basic Program Components 40 Thisi
- Page 304 and 305: 3 Basic Program Components ans = 10
- Page 306 and 307: 3 Basic Program Components X = -pi:
- Page 308 and 309: 3 Basic Program Components Program
- Page 310 and 311: 3 Basic Program Components if A S1
- Page 312 and 313: 3 Basic Program Components end disp
- Page 314 and 315: 3 Basic Program Components while St
- 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 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 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
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