Department of Engineering

IT Services

Matlab, presentations and publishing

Matlab and its editor has many facilities to help with code development. What's perhaps not so obvious is that some of facilities are of particular use to teachers during talks and when creating documentation.

Shortcuts

menuIn the main Matlab window there's a menubar so that you can create short-cuts on-the-fly. Suppose during your talk you repeatedly want to clear variables and create some matrices. Rather than repeatedly type

clear all
a=magic(5);
b=a';

you can copy-paste these lines onto the shortcut menubar. You'll be invited to choose a name (e.g. restart) for the short-cut after which it will appear as an icon on the menubar that you can click on to re-run the code.

Code Cells

menuCode Cells (which are nothing to do with cell arrays in the matlab language) are regions of code. There's a Cell menu in the editor to control them, and a menubar. A new cell is started in the code by beginning a line with %%. The text on the rest of the line is ignored during execution but comes into play when creating documentation (as you'll see later). For example if you type these lines into the editor, you can select a cell by clicking on it, and you can run the code in a cell by using the icons in the cell menubar

%% Setting up
clear all
a=magic(5);
b=a';
%% Multiplying
b*a
%% Now some maths
int('tan(x)+x^3')
plot(tan(a)+a.^3)

Docking

Having several floating windows (for graphics, editing, etc) can cause useful windows to be obscured. If you click on the menu icon (top-right on matlab windows) the window will "dock" itself into the matlab desktop.

Breakpoints and code-folding

menuWhen you have many lines of code you may want to run the code up to a certain line. You can do this by setting breakpoints. Put the cursor on the line where you want to stop and click on the "Set/clear breakpoint icon". When you run the code, execution will stop at the line. You can continue execution line by line or until the next breakpoint by clicking on the appropriate icon in the menubar

The editor's code-folding facilities let you hide/show details of functions.

Publishing

In the editor there's a "Publish" icon and a "Publish" item in the File menu. This not only converts the code into Web pages but also displays the output (both text and graphics) of the executed code on the web page. An advantage of using "cell mode" in the editor is that a cell is treated as a section of the document, the text following %% being used as a section heading. Matlab comments are formatted as text, the maths being prettified. So if you have a file called ttt.m containing

%% Setting up
% We'll create a few matrices, do a few sums, then plot
clear all
a=magic(5);
b=a';
%% Multiplying
b*a
%% Now some maths
% We'll try $\int(tan(x)+x^3)$
int('tan(x)+x^3')
plot(tan(a)+a.^3)

and you publish it as HTML you should get something like this document (before publishing, you may need to use the "change configuration" menu item to select an output folder). Note that a table of contents is created. You can also publish as LaTeX.