Formatting M-File Code for Publishing

Overview of Formatting M-File Code for Publishing

This section describes ways to control how output that the MATLAB® software generates when it evaluates executable M-file code appears in 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.

The tool you use to specify how output is presented in the document is the same tool you use to specify document titles and section headers; namely the double percent sign (%%) which is referred to as a cell break or cell divider. 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 divider. Because MATLAB considers the entire M-file to be a cell, when you insert a cell break, MATLAB considers the file to contain two cells; one above the cell break and one below. The examples in the remaining topics demonstrate how you can use this behavior to control the output produced by M-file code.

Example of Published M-File Output

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

Sample M-File Before Inserting Cell Breaks in Code

Suppose your M-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 M-code using factory default settings. For information on setting publishing properties for images, see Producing Published Output from M-Files.

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.

Image of Web Browser with the title, Scale magic Data and Display as Image, in large, red font, followed by the for loop code. The loop is followed by the last image generated by the for loop.

Sample M-File After Inserting Cell Breaks in Code

By placing cell breaks within a loop, you can display the output generated by M-file 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 Divider.

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 M-code using factory default settings. For information on setting publishing properties for images, see Producing Published Output from M-Files.

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 M-File Code for Publishing for details.

Image of Web Browser with the title, Scale magic Data and Display as Image, in large, red font, followed by the first two lines of the for loop code. Within the loop code are each of the images generated by the for loop. Following the images is the for loop end statement.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS