Skip to Main Content Skip to Search
Product Documentation

publish - Publish MATLAB file with code cells, saving output to specified file type

Syntax

publish('file')
publish('file','format')
publish('file', options)
my_doc = publish('file',...)

Description

publish('file') publishes file.m by running it in the base workspace, one cell at a time. It saves the code, comments, and results to an HTML output file, file.html. The MATLAB software stores this output file, along with other supporting output files, in a subfolder of the folder containing file.m. The subfolder is named html.

publish('file','format') saves the code, comments, and results to an output file, file.format. The output subfolder is named html, regardless of the output file format.

publish('file', options) publishes file.m using the structure options.

my_doc = publish('file',...) returns the output resulting from publishing file.m to my_doc.

Input Arguments

file

Specifies the file to publish.

format

Specifies the format to which you want to publish the file. Valid formats appear in the "Options for the publish Function" table under options.

options

A structure with the fields listed in the following table.

Options for the publish Function

Field

Values

format

Specifies the output format for the published document:

  • 'doc'— Microsoft Word output format.

  • 'latex'— LaTeX output format.

  • 'ppt'— Microsoft PowerPoint output format.

  • 'xml'— Extensible Markup Language output format.

  • 'pdf'— Portable Document Format output format.

  • 'html' (default )— Hypertext Markup Language output format.

    If you specify html, MATLAB includes the code at the end of the published HTML file as comments, even when you set the showCode option to false. Because MATLAB includes the code as comments, the code does not display in a Web browser. Use the grabcode function to extract the MATLAB code from the HTML file.

    Note   MATLAB does not preserve syntax highlighting for MATLAB code published to these output types:

    • LaTeX

    • Microsoft Word (.doc)

    • Microsoft PowerPoint (.ppt)

stylesheet

Specifies the Extensible Stylesheet Language (XSL) file that you want MATLAB to use when you specify a format of 'html', 'xml', or 'latex':

  • '' (default) — The MATLAB default stylesheet

  • XSL file name — The full path of the XSL file

outputDir

Specifies the folder to which you want MATLAB to publish the output document and its associated image files:

  • '' (default) — MATLAB places output in the html subfolder of the current folder, which MATLAB creates.

  • full path — MATLAB places output in the folder you specify.

imageFormat

Specifies the file type you want MATLAB to use for images it produces when publishing files. The following table indicates which types of images you can include for each output file format. However, for greatest compatibility, MathWorks recommends using the default image format for each output type.

Output File FormatTypes of Images You Can IncludeDefault Image Type
doc

Any format that your installed version of Microsoft Office can import, including png , jpg, bmp, and tiff. If the figureSnapMethod is 'print', then you can also include eps, epsc, eps2, ill, meta, and pdf images.

png
html

Any format publishes successfully. Ensure that the tools you use to view and process the output files can display the output format you specify.

png
latex

Any format publishes successfully. Ensure that the tools you use to view and process the output files can display the output format you specify.

epsc2,except when figureSnapMethod is any one of the following, in which case the default is png:
  • getframe

  • entireFigureWindow

  • entireGUIWindow and the snapped window is a GUI window

pdf

bmp or jpg.

bmp
ppt

Any format that your installed version of Microsoft Office can import, including png, jpg, bmp, and tiff.

png
xml

Any format publishes successfully. Ensure that the tools you use to view and process the output files can display the output format you specify.

png

figureSnapMethod

Specifies how figure windows and GUI dialog boxes that the code creates appear in published documents. Window decorations are the title bar, toolbar, menu bar, and window border.

ValuesWindow Decorations for ...Background Color for ...
 GUIsFiguresGUIsFigures

'entireGUIWindow' (default)

Included

Excluded

Match screen

White

'print'

Excluded

Excluded

White

White

'getframe'

Excluded

Excluded

Match screen

Match screen

'entireFigureWindow'

Included

Included

Match screen

Match screen

useNewFigure

A logical value that specifies whether MATLAB creates a Figure window for figures that the code generates:
  • true (default) — If the code generates a figure, then MATLAB creates a Figure window with a white background, and at the default size before publishing.

  • false — MATLAB does not create a figure window.

    This value enables you to use a figure with different properties for publishing. Open a Figure window, change the size and background color, for example, and then publish. Figures in your published document use the characteristics of the figure you opened before publishing.

maxHeight

Specifies the maximum height, in pixels, for an image that the code generates:
  • [] (default) — Height is unrestricted. Always used when the format is pdf.

  • Any positive integer — Height is the specified value.

maxWidth

Specifies the maximum width, in pixels, for an image that the code generates:
  • [] (default) — Width is unrestricted. Always used when the format is pdf.

  • Any positive integer — Width is the specified value.

showCode

Logical value that specifies whether MATLAB includes the code in the published document:
  • true (default)

  • false

evalCode

Logical value that specifies whether MATLAB runs the code that it is publishing:
  • true (default)

    Use this option if you want to run the code. If set to true and you are publishing a function file that requires inputs, specify the codeToEvaluate option too.

  • false

    Use this option if you do not want to run the code, but do want to present it (without output) in the published document. If you use the publish command to publish the file that contains the command, set this option to false. Otherwise, MATLAB attempts to publish the file recursively.

catchError

Logical value that specifies what MATLAB does if there is an error in the code that it is publishing:
  • true (default) — MATLAB continues publishing and includes the error in the published file.

  • false — MATLAB displays the error and publishing ends.

codeToEvaluate

Specifies the code that MATLAB is to evaluate. By default, MATLAB evaluates the code in the file you are publishing.

createThumbnail

Logical value that specifies whether MATLAB creates a thumbnail image of the published document:
  • true (default)

  • false

maxOutputLines

Value that specifies the maximum number of output lines per cell that you want to publish before truncating the output:
  • Inf (default) — MATLAB includes all output lines.

  • Nonnegative integer — MATLAB includes, at most, the number of lines you specify.

Examples

Copy sine_wave.m, publish the file to HTML, and then view the published document:

copyfile(fullfile(matlabroot,'help','techdoc','matlab_env','examples', ...
'sine_wave.m'),'.','f')

% When you run the command that follows, MATLAB runs sine_wave.m, 
% and saves the code, comments, and results to 
% /html/sine_wave.html:

publish('sine_wave.m', 'html')

% View the published output file in the Web browser:
web('html/sine_wave.html')
 

Copy sine_wave.m, publish the file to Microsoft Word format by using a structure, and then view the published document:

copyfile(fullfile(matlabroot,'help','techdoc','matlab_env','examples', ...
'sine_wave.m'),'.','f')

% Define the structure, options_doc_nocode, 
% to exclude code from the output 
% and publish to Microsoft Word format:
options_doc_nocode.format='doc'
options_doc_nocode.showCode=false

% Publish sine_wave.m:
publish('sine_wave.m',options_doc_nocode)

% View the published output file in Microsoft Word:
winopen('html/sine_wave.doc')
 

Copy collatz.m, create a structure to specify the input values, publish the file to HTML, and then view the published document:

copyfile(fullfile(matlabroot,'help','techdoc','matlab_env','examples', ...
'collatz.m'),'.','f')

% Create a structure, opts, that contains the code that you 
% want collatz.m to evaluate when it runs:
opts.codeToEvaluate = 'n = 3; collatz(n)';

% In the MATLAB Web browser, display the results of
% publishing collatz.m when it runs with the values 
% specified in opts:
web(publish('collatz',opts))
 

Copy sine_wave.m, publish the file capturing window decorations, and then view the published document:

copyfile(fullfile(matlabroot,'help','techdoc','matlab_env','examples', ...
'sine_wave.m'),'.','f')

% Create an options file that causes the published document
%  to capture window decorations:
function_options.format='html';
function_options.figureSnapMethod='entireGUIWindow';

% Publish the script using the options file:
publish('sine_wave.m',function_options);

% View the output in the MATLAB Web browser
web('html/sine_wave.html')
 

Publish a demo file to PDF, and then open the published document:

open(publish('sparsity',struct('format','pdf','outputDir',tempname)))

Alternatives

To publish a file from the desktop:

  1. Open the MATLAB code file that you want to publish in the Editor.

  2. Apply text mark up to format your code and comments by choosing options from the Editor Cell menu.

  3. Choose one of the following:

    • Publish with default options by choosing File > Publish filename.

    • Publish with customized options by choosing File > Publish Configuration for filename > Edit Publish Configurations for filename, and then adjust the Publish settings.

See Also

grabcode | notebook

How To

  


» Learn more
» Download free kit
» Get trial software

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