Programming


How to program

Matlab statements can be prepared with any editor, and stored in a file for later use. The file is referred to as a script, or an "m-file" (since they must have names of the form foo.m). You may prefer to do this, if you use the same data repeatedly, or have an editor that you like to use. Writing m-files will make you much more productive.

Select File -> New ->M-file to open Matlab's built-in editor/debugger, or use your favorite editor. Then create the following file, and save it as sketch.m:

	[x y] = meshgrid(-3:.1:3, -3:.1:3);
	z = x.^2 - y.^2;
	mesh(x,y,z);
Next, in Matlab, be sure that the directory where the m-file resides is in the path. Check this by typing pathtool and making sure that your directory is present. (Users without graphics can use addpath directory .)

Now enter
sketch
The result is the same as if you had entered the three lines of the file, at the prompt.

You can also enter data this way: if a file named mymatrix.m in the current working directory contains the lines

	A = [2 3 4; 5 6 7; 8 9 0];
then the command mymatrix reads that file and generates A. However, for large matrices, Matlab's own save and load commands are much safer to use.

Functions

Functions are like any other m-file, but they accept arguments, and they are compiled the first time they are used in a given session (for speed).

Use your favorite editor to create a file named sqroot.m, containing the following lines.

	function sqroot(x)
	% SQROOT compute squares root by Newton's method

	% Initial guess
	xstart = 1;

	for i = 1:100
		xnew = ( xstart + x/xstart)/2;
		disp(xnew);
		if abs(xnew - xstart)/xnew < eps, break, end;
		xstart = xnew;
	end
In Matlab enter the commands
format long
sqroot(19)
You should see the output from your function.

Two caveats:

  • A function has access to the variables in the "workspace" from which it was called, but the variables created within the function (xstart and xnew, in the preceeding example) are local, which means that they are not shared with the calling workspace. For more information, see the chapter "M-File Programming" in the manual, Using Matlab.
  • Note: if you edit a function during a session, use clear function_name to remove the compiled version, so that the new one will be read.

Batch Programming

MATLAB may be ran in "batch mode," in a very similar way. If a file named "test.m" contains the (non-graphics) commands you want processed, at the UNIX prompt type:
	% matlab < test.m > homework.out
This is read, "Run MATLAB, with input from test.in, and output to test.out." The input file does not need to be named "something-dot-m," but it must end with quit .


Programming ideas

The "m-files" which came with MATLAB provide lots of examples! To find their location, use path .This will also lead you to some demos.

You might also try typing demo to get a feel for the breadth of tasks that can be accomplished with Matlab.

See help function for an exercise for programmers.


Next: Further Reading
Previous: Graphics