Department of Mathematics

Math 300: Mathematical Computing

Matlab Publish

Matlab can be used purely as a command interface, but doing so uses only a tiny fraction of its power. A more powerful way to use Matlab is to create scripts and functions. These are ways to save the commands you want to run in a file, so that you can run them repeatedly, and edit them so that they work better.

One way in which Matlab has changed relatively recently is in its treatment of scripts. We can save a collection of Matlab commands in a file ending with the extension ".m". Then in Matlab we run the publish() command, which creates an HTML page with the same name as the script. We can then view that HTML using a browser. The point is that this approach allows us to embed text and descriptions in the web page that results, so as to describe the code and results that come from it.

This process is best described through an example. We will create a script and publish it, viewing the results. First, we must type some Matlab commands into a file and save it. We open our favorite editor (which in this case is probably the Matlab editor itself) and type the following code.

%% Introduction to Matlab Publish % The publish() command in Matlab allows us to use script files to create % html documents, or other formats, which can include bodies of text to % describe what is going on, with the actual commands to make it happen % embedded in the document. The text mode makes use of the comment % character - the percent sign. Two adjacent percent signs starting a line % indicate the start of a cell of text. Text on the line with the two % percent signs is interpreted as a header. % % Here is an example of how we might use Matlab as a simple calculator. (3+4^2)/2.5

We see that the first line of code starts with two percent signs, indicating the start of a text cell. The text on the line with the two percent signs is interpreted as a heading. The text on lines with single percent signs is interpreted as ordinary descriptive text. Lines without percent signs are interpreted as Matlab commands.

Now we save this file with the name sample.m. If Matlab is not open, we start it, and surf (using the cd command) to the directory where sample.m is saved. Next we run the Matlab command publish('sample.m') on the Matlab command line. This creates a new subdirectory called html, which contains a file called sample.html. If it is on our own computer we can force our browser to locate to it by entering file:/// on the URL line of the browser, and surf around until we find that html file.

At this point, we can make changes to the .m file in the editor, save the file, run the publish command above (using the up-arrow key!), and the reload the browser to see our results. We can write text copiously to describe what is being done in the script, or discuss the results that arise.

Assignment C is posted.

The test solution is available.

Department of Mathematics, PO Box 643113, Neill Hall 103, Washington State University, Pullman WA 99164-3113, 509-335-3926, Contact Us
Copyright © 1996-2015 Kevin Cooper