This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Publishing MATLAB Code

Publishing a MATLAB® Code file (.m) creates a formatted document that includes your code, comments, and output. Common reasons to publish code are to share the documents with others for teaching or demonstration, or to generate readable, external documentation of your code. To create an interactive document that contains your code, formatted content, and output together in the MATLAB Editor, see Create Live Scripts in the Live Editor.

This code demonstrates the Fourier series expansion for a square wave.

MATLAB Code with Markup

Published Document

To publish your code:

  1. Create a MATLAB script or function. Divide the code into steps or sections by inserting two percent signs (%%) at the beginning of each section.

  2. Document the code by adding explanatory comments at the beginning of the file and within each section.

    Within the comments at the top of each section, you can add markup that enhances the readability of the output. For example, the code in the preceding table includes the following markup.


    %% Square Waves from Sine Waves

    %% Add an Odd Harmonic and Plot It

    %% Note About Gibbs Phenomenon

    Variable name in italics

    % As _k_ increases, ...

    LaTeX equation

    % $$ y = y + \frac{sin(k*t)}{k} $$


    When you have a file containing text that has characters in a different encoding than that of your platform, when you save or publish your file, MATLAB displays those characters as garbled text.

  3. Publish the code. On the Publish tab, click Publish.

    By default, MATLAB creates a subfolder named html, which contains an HTML file and files for each graphic that your code creates. The HTML file includes the code, formatted comments, and output. Alternatively, you can publish to other formats, such as PDF files or Microsoft® PowerPoint® presentations. For more information on publishing to other formats, see Specify Output File.

    In MATLAB Online, MATLAB publishes to either HTML or PDF, and stores the output and supporting files in your Published folder. To allow MATLAB to open the output automatically, enable pop-up windows in your Web browser. Results are available publicly using a URL of the form

The sample code that appears in the previous figure is part of the installed documentation. You can view the code in the Editor by running this command:

edit(fullfile(matlabroot,'help','techdoc','matlab_env', ...

See Also

Related Topics