]>

Publish Your Work

Solving problems in Matlab's command window is very convenient until you wish to report on the work that you have completed. Copying and pasting your Command Window commands and results to a Word Processor is unnecessary thanks to the File -> Publishing options available in Matlab. This lesson shows you how to conveniently save your commands to a Matlab script (an m-file) and then use Matlab to create an HTML page that shows your commands and their results in a very user-friendly format. First, you must save the desired commands to an m-file.

1) Save commands to m-file

  1. Use "Ctrl-Left Click" to select individual commands, from the Command History, that solve the problem.
  2. Right-click while the cursor is in the Command History window.
  3. Select Create m-file.
  4. Click the Save file icon to save the file.
  5. Change to the correct directory for your work.
  6. Enter a valid and descriptive name for your file.
  7. Run your Matlab script. There are several ways to do this:
    • Select Debug->Run from the Editor window menu.
    • Click the Save and Run icon to save and run the script.
    • Type the name of the m-file in the Command Window and press Enter.
    • Type the F5 key.
  8. Fix any syntax or runtime errors that occur.
commands in the history window and right-click save to m-file

2) Create Cell Divisions

A cell is a logical block of code. Each cell has a header, a comment block, and the code, that solve that part of the problem. You must decide where the cell divisions must be. A good start is to have one cell for each part of a problem's solution.

  • Select Cell -> Enable Cell Mode from the Editor menu to turn on cell syntax highlighting.
  • Add a cell header for each cell that you identify. The cell's header starts with %%. It must be followed by a blank space and then a descriptive title for that part of the solution.
  • Use the (%) to add a regular comment that describes the cell's purpose in the larger context of the problem's solution.
  • Edit and add code as needed to complete the work.
  • Save and Run your script (m-file).

You may also include equations in the first comment section of any cell in your m-files. All capabilities can't be shown here, but the basic idea is to put your equation in a comment block with blank lines before and after and the characters $$ at the start and end of the equation.

The commentswill produce
	%
	% $$y = \frac{1}{x^2}$$
	%
	
y equals x squared

Notice, how you must enter fractions using:

\frac{numerator}{denominator}

The blank lines before and after the equations are required.

Caution: Comment lines that describe equations must be in the first comment block in the cell. If the comment lines come after any MATLAB code statements in the given cell, they will be listed as is in the code and will not be translated into the equation form we are showing here.

add cell divisions

3) Publish to HTML

Just saving the commands in an m-file is a big step. You can easily print the contents of an m-file and you can edit, rerun, and retry your solution in many variations. However, the results of each command are not shown in an m-file and the text-only format is not as nice as a more polished document can show. A published document is better for handing in work for homework assignments and for presenting your work to a co-worker or Supervisor. To publish your solution to an easy to read (and print) HTML file:

  1. Be sure that the editor window for your m-File is active (highlighted).
  2. Select File -> Publish from the Menu bar.

This will save your file, run your file, create a web page and open the new HTML file in a Web Browser. The name of the html file will be the name of your script.html and it will be saved in a folder named html in the same folder as your script. For CS310 homeworks, print out this file and handin as directed on the homework.

final html result

[OPTIONAL] Link to a function definition m-file

It is possible to publish the code of user-defined functions to HTML, but this action is rarely necessary and is not covered in this course. Instead, print the m-file that contains the function code that you wish to handin. If you wish to include a link to a function definition (as seen in some of our posted solutions), you may follow these instructions to do so.

To include a link to a function definition from the finished HTML page, include a cell with the following information. This example assumes that you have defined a function named MB that accepts three input parameters, (k,T,E). It also assumes that the function definition file is in the same directory as the script that is being published to HTML.

%% Part 3.b Define MB(k,T,E) to compute Maxwell-Boltzmanm
% See <../MB.m MB.m>
    

See the online help for the publish command for more publishing options.

image of link to a function definition