Overview of Formatting MATLAB Code for Publishing

This section describes ways to control how output that the MATLAB software generates when it evaluates executable MATLAB code for a published document. For example, you can direct MATLAB 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 MATLAB 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 MATLAB 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 MATLAB code.

Example of Formatting Code Output in a Published MATLAB File

This section provides an example to demonstrate how a MATLAB 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:

%% 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 Producing Published Output from MATLAB Code Files.

[important]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.[/important]

By placing cell breaks within a loop, you can display the output generated by MATLAB 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:

%% 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 Producing Published Output from MATLAB Code Files.

[important]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 Formatting MATLAB Code for Publishing for details.[/important]