MATH/CMSC 206 - Introduction to Matlab

Announcements Syllabus Tutorial Projects Submitting

Publishing M-Files

Contents

What is publishing?

MATLAB's publish feature is a powerful tool that allows you to create nicely formatted documents from your m-files. We used publish to create all of the beautiful web pages that you are viewing!

Publish allows you to easily format text and also to execute "live" MATLAB commands, inserting the results directly into your documents. The output from the publish command can be formatted as html, pdf, Word documents, Power Point documents, and LaTex. The default setting is to produce html files -- perfect for creating web pages like these!

Example

We don't want to overwhelm you with too many details, so we will just show you the basics using one simple example.

Begin by editing an M-file file called publishexample.m. Just type

edit publishexample.m

at the command prompt in Matlab, or use your favorite text editor.

Now enter the following into your m-file (we highly suggest cutting and pasting.) If you're using the Matlab editor then it may highlight the code in different colors.

%% First Thing
% The previous line begins with %% followed by some text.  When
% published this will show up as a distinct section with its
% own label in the html document.  Now here is a calculation.
% To do the calculation we stop using % in front.  The blank
% lines are not mandatory but they do make things much easier
% to read:
 
2+2
 
%%
% The previous line just has %% but no text.  It's still critical to
% have it there because we're beginning a new section of text.  It will
% NOT have a label though.  You can't just go from Matlab code
% straight back to %, you need to have %% first.  Now here's some
% other stuff:
 
syms x;
ezplot('x^2')
 
%% A New Section Label
% Back to text with another %% but now a section label again because
% it's %% followed by the label.
%
% Notice that we can have % by itself which just begins a new
% paragraph within the text section.
%
% Here are some _italics_ and here is some *boldface*.  Here is some
% |fixed-width font| which looks like code.
%
% Here's another calculation:
 
solve('2*x+3')
 
%%
% Now back to text.  We can do some numbering but before the numbering
% we must have a line with %% or just a simple %.
%
% # One
% # Two
% # Three
%
% A simple % line ends the counting.  Here is another calcuation:
 
diff('sin(x^2)')
 
%%
% Now if we go back to text and try more numbers they restart at
%
% # One
% # Two
%
% The thing is that the # symbol will do numbering but
% _only if they're in the same text section_.
%
% Finally here are some bullets, again preceeded by %:
%
% * Bam!
% * Kapow!
%
% Done!

Now... try publishing this m-file by pressing the "publish" button in the editor which looks like little document speeding across the page. Like this:

Did you try it? When you click "publish", a nicely formatted webpage should appear appear on your screen in your default web browser. The resulting html file is also automatically saved on your computer in a directory called "html", which is created inside the current working directory. The output is impressive! Really compare it to the m-file which you published and think about how it worked.

You can also do this from the MATLAB command line by typing

publish('publishexample')

Click here to see the output that "publish" should have produced from this m-file.

Basic Structure

The basic structure is pretty easy. The publishable m-file is broken up into blocks of text and blocks of Matlab code which are actually executed.

Blocks of text start with either %% and a section heading or just %%. If there's a section heading then it publishes in boldface and gets a link at the top of the webpage.

Within each text block you can make the text pretty by:

You can construct numbered lists by first starting with a line containing just % and then each succesive numbered item has a % # in front. End the list with another % line.

Likewise you can construct bulleted lists by first starting with a line containing just % and then each succesive numbered item has a % * in front. End the list with another % line.

You can do more with publishing but those are the basics.

Self-Test

1. Try writing an m-file that will produce a pretty html file like the one in this link.

Answers to Self-Test

Next Topic: Limitations of MATLAB