Formatting MATLAB Code for Publishing

April 14th, 2010 Posted in Documents

Overview of

This section describes ways to control how output that the software generates when it evaluates executable code for a published document. For example, you can direct to include the last, or all plots generated by a for loop. You can interweave comments, code, and output throughout your published document to draw your readers’ attention to certain areas of interest.

Formatting Code Output in a Published Code File

The method you use to specify how output displays in the document is the same method you use to specify document titles and section headers; namely the cell break (%%). When you insert a cell break into a file, it directs to publish the code and output contained in the cells created by the break. Because the entire file is a cell, inserting a cell break results in the file containing two cells. The first cells is the one above the cell break, and the second is the one below the cell break. The examples in the remaining topics demonstrate how you can use this behavior to control the output produced by code.

Example of Formatting Code Output in a Published File

This section provides an example to demonstrate how a file appears when published. It demonstrates how the published example file appears before and after cell breaks are added to achieve the published results.

Suppose your file contains the following code:

1
2
3
4
5
%% Scale magic Data and Display as Image
 
for i=1:3
    imagesc(magic(i)) 
end

The following image illustrates how the code presented appears when you publish it to HTML. The plot in the figure is smaller than it appears if you publish the code using factory default settings. For information on setting publishing properties for images, see .

Notice that the published document displays the plot after the end of the for loop and that only the last plot generated by the code is included.

By placing cell breaks within a loop, you can display the output generated by code when iterating a loop.

To include the plot generated by each iteration of the loop in the published document, insert a cell break after the opening for statement. Position the cursor at the end of the first line of the for loop, and then select Cell > Insert Cell Break.

The code now appears like this:

1
2
3
4
5
6
%% Scale magic Data and Display as Image
 
for i=1:3
    %%
    imagesc(magic(i)) 
end

Now when you publish the code to HTML, it appears as follows. The plots in the figure are smaller than they appear if you publish the code using factory default settings. For information on setting publishing properties for images, see .

Notice that the published document displays the plot within the for loop code. You can also use text markup for similar results with figures. See for details.

Related posts

Leave a Reply

You must be logged in to post a comment.