Documentation Center

  • Trial Software
  • Product Updates

publish

Generate view of MATLAB file in specified format

Syntax

Description

example

publish(file) generates a view of a MATLAB® file in HTML format for sharing your code. For example, publish('myfile.m') executes the code in myfile.m using the base workspace and saves the formatted code and results in /html/myfile.html. The html subfolder is relative to the file folder.

When MATLAB publishes a file, it can delete existing files from the output folder that start with the same name as file.

example

publish(file,format) generates a view of a MATLAB file in the specified file format. The resulting files save to the html subfolder for all file formats.

example

publish(file,Name,Value) generates a view of the MATLAB file, file, with options specified by one or more name-value pair arguments.

example

publish(file,options) uses the options structure to customize the output, which is useful when you want to preconfigure and save your options for repeated use. The options structure fields and values correspond to names and values of name-value pair arguments, respectively.

example

my_doc = publish(file,___) generates a view of the MATLAB file file, and returns a string indicating the path of the resulting output file. It can include any of the input arguments in previous syntaxes.

Examples

expand all

Generate HTML View of MATLAB Script

Generate an HTML view of the code, MATLAB results, and comments in a MATLAB script.

Copy the example file, fourier_demo2.m, to your current folder.

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

Generate an HTML view of the MATLAB file.

 publish('fourier_demo2.m');

The publish command executes the code for each cell in fourier_demo2.m, and saves the file to /html/fourier_demo2.html.

View the HTML file.

web('html/fourier_demo2.html')

Generate View of MATLAB Script in Microsoft Word Format

Generate a Microsoft® Word view of the code, MATLAB results, and comments in a MATLAB script.

Copy the example file to your current folder.

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

Publish the file in Microsoft Word format.

publish('fourier_demo2.m','doc');

View the published output.

winopen('html/fourier_demo2.doc')

Publish MATLAB Script Using Name-Value Pairs to Customize Output

Generate an HTML view of the code, MATLAB results, and comments in a MATLAB script. Use name-value pair arguments to include window decorations in the published output.

Copy the example file to your current folder.

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

Publish the example MATLAB file to HTML format.

publish('fourier_demo2.m', 'figureSnapMethod', 'entireFigureWindow')

The 'figureSnapMethod','entireFigureWindow' name-value pair argument specifies to include the window decorations in the figures, and to match the figure background color to the screen color.

View the published output.

web('html/fourier_demo2.html')

Customize publish Output Using Options Structure

Generate a Microsoft Word view of the code, MATLAB results, and comments in a MATLAB script. Use a structure to customize the published output.

Specifying options as a structure is useful when you want to preconfigure and save your options for repeated use.

Copy the example file to your current folder.

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

Define options to customize the published output as a structure, options_doc_nocode.

options_doc_nocode.format = 'doc';
options_doc_nocode.showCode = false;

Publish the file, specifying the options structure.

publish('fourier_demo2.m',options_doc_nocode);

Save File Path of Published Script to Variable

Generate an HTML view of a MATLAB script, and save the path of the published HTML file to a variable.

This example assumes that the current folder is C:\my_MATLAB_files.

Copy the example file to your current folder.

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

Publish the MATLAB file, and save the path of the resulting published HTML file to an output variable.

mydoc = publish('fourier_demo2.m')
mydoc =

C:\my_MATLAB_files\html\fourier_demo2.html

Input Arguments

expand all

file — MATLAB filestring

Full or partial path of the MATLAB file for which you want to generate a presentation view, specified as a string.

Example: 'myfile.m'

format — Output format of published file'html' (default) | 'doc' | 'latex' | 'ppt' | 'xml' | 'pdf'

Output format of published MATLAB file, specified as one of the following string values.

Output FormatString Value
Hypertext Markup Language'html' (default)
Microsoft Word'doc'
LaTeX'latex'
Microsoft PowerPoint®'ppt'
Extensible Markup Language'xml'
Portable Document Format 'pdf'

Example: publish('myfile.m','ppt');

options — Options for published outputMATLAB structure

Options for published output, specified as a structure. Use the options structure instead of name-value pair arguments when you want to reuse the same configuration for publishing multiple MATLAB code files.

The options structure field and values correspond to names and values of the name-value pair arguments, respectively.

For example, to specify the PDF output format and the output folder C:\myPublishedOutput, use:

options = struct('format','pdf','outputDir','C:\myPublishedOutput')

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'format','latex','showCode',false specifies LaTeX output file format and excludes the code from the output.

Output Options

'format' — Published output file format 'html' (default) | 'doc' | 'latex' | 'ppt' | 'xml' | 'pdf'

Published output file format, specified as the comma-separated pair consisting of 'format' and one of the following string values.

Output FormatString Value
Hypertext Markup Language'html' (default)
Microsoft Word'doc'
LaTeX'latex'
Microsoft PowerPoint'ppt'
Extensible Markup Language'xml'
Portable Document Format 'pdf'

'outputDir' — Output folder'' (default) | full path

Output folder to which the published document is saved, specified as the comma-separated pair consisting of 'outputDir' and the full path. You must specify the full path as a string, for example 'C:\myPublishedOutput'.

The default value, '', specifies the html subfolder of the current folder.

'stylesheet' — Extensible Stylesheet language (XSL) file'' (default) | full path to XSL file name

Extensible Stylesheet Language (XSL) file to use when publishing MATLAB code to HTML, XML, or LaTeX format, specified as the comma-separated pair consisting of 'stylesheet' and the full path to the XSL file. The full path must be a string, for example, 'C:\myStylesheet\stylesheet.xsl'

The default value, '', specifies the MATLAB default style sheet.

Figure Options

'createThumbnail' — Thumbnail image creationtrue (default) | false

Thumbnail image creation for the published document, specified as the comma-separated pair consisting of 'createThumbnail' and either true or false. You can use this thumbnail to represent your file in HTML pages.

'figureSnapMethod' — Published figure window appearance'entireGUIWindow' (default) | 'print' | 'getframe' | 'entireFigureWindow'

Appearance of published figure windows, including window decorations and background color, specified as the comma-separated pair consisting of 'figureSnapMethod' and one of the following string values.

String ValueWindow DecorationsBackground Color
 GUIsFiguresGUIsFigures

'entireGUIWindow' (default)

Included

Excluded

Matches screen

White

'print'

Excluded

Excluded

White

White

'getframe'

Excluded

Excluded

Matches screen

Matches screen

'entireFigureWindow'

Included

Included

Matches screen

Matches screen

'imageFormat' — Published image file format'png' | 'epsc2' | 'jpg' | ...

Published image file format, specified as the comma-separated pair consisting of 'imageFormat' and one of the following string values.

'format' ValueValid 'imageFormat' ValueDefault 'imageFormat' Value
'doc'

Any image 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 specify 'eps', 'epsc', 'eps2', 'ill', 'meta', and 'pdf'.

'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:
  • 'getframe'

  • 'entireFigureWindow'

  • 'entireGUIWindow' and the snapped window is a GUI window

In these cases the default is 'png'
'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'

'maxHeight' — Maximum height of published images[] (default) | positive integer value

Maximum height of published images that the code generates, specified as the comma-separated pair consisting of 'maxHeight' and one of the following values:

  • [] (default)—Unrestricted height. This value is always used when the 'format' value is 'pdf'.

  • Positive integer value that specifies the image height in pixels.

'maxWidth' — Maximum width of published images'' (default) | positive integer value

Maximum width of an image that the code generates, specified as the comma-separated pair consisting of 'maxWidth' and one of the following values:

  • [] (default)—Unrestricted width. This value is always used when the 'format' value is 'pdf'.

  • Positive integer value that specifies the image width in pixels.

'useNewFigure' — Create new figuretrue (default) | false

A logical value that determines if MATLAB creates a new Figure window for figures that the code generates, specified as the comma-separated pair consisting of 'useNewFigure' and one of the following:

  • 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. For example, open a Figure window, change the size and background color, and then publish your code. Figures in your published document use the characteristics of the figure you opened before publishing.

Code Options

'evalCode' — Option to run codetrue (default) | false

Option to evaluate code and include the MATLAB output in the published view, specified as a logical value.

'catchError' — Error handling during publishingtrue (default) | false

Error handling during publishing, specified as the comma-separated pair consisting of 'catchError' and one of the following logical values:

  • true (default)—MATLAB continues publishing and includes the error in the published file.

  • false—MATLAB displays the error at the command line and does not produce a published file.

'codeToEvaluate' — Additional code to evaluate during publishingstring

Additional code to evaluate during publishing, specified as the comma-separated pair consisting of 'codeToEvaluate' and the string with the corresponding code. Use this option to specify code that does not appear in the MATLAB file, for example to set the value of an input argument for a function being published.

If this option is unspecified, MATLAB evaluates only the code in the MATLAB file you are publishing.

'maxOutputLines' — Maximum number of lines Inf (default) | nonnegative integer value

Maximum number of lines in MATLAB output per cell evaluated during publishing, specified as the comma-separated pair consisting of 'maxOutputLines' and one of the following values:

  • Inf (default)—MATLAB includes all output lines in the published output.

  • Nonnegative integer—MATLAB truncates the number of lines in the output at the number of lines you specify.

'showCode' — Option to include code in published filetrue (default) | false

Option to include code in published file, specified as the comma-separated pair consisting of 'maxOutputLines' and a logical value.

More About

expand all

Window Decorations

Window decorations include window title bar, toolbar, menu bar, and window border.

Syntax Highlighting

Syntax highlighting colors various elements in code to help you identify these elements while reading or editing code. By default, keywords are blue, strings are purple, and unterminated strings are maroon.

MATLAB does not preserve syntax highlighting when you set 'format' to 'latex' or 'ppt'.

Tips

  • If 'format is 'html', MATLAB includes the code being published 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, this code does not display in a Web browser. This enables you to use the grabcode function to extract the MATLAB code from an HTML file, even when the file does not display the code.

See Also

|

Was this topic helpful?