An example is a readable version of a MATLAB® code file that shows how to a solve particular problem. MATLAB and all MATLAB toolboxes include examples. You also can create your own examples from your source code files. Creating an example enables you to document the steps of a task clearly, because examples combine comments, code, and output together in a formatted document.
For instance, the code in the following figure demonstrates the Fourier series expansion for a square wave. When published, the example includes the explanatory text, code, and output.
MATLAB Code with Markup
To create an example:
Create a MATLAB script or function. Divide the
code into steps or sections by inserting two percent signs (
at the beginning of each section.
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.
Variable name in italics
Note: 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.
Publish the code. On the Publish tab, click Publish.
By default, MATLAB creates a subfolder named
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.
To share published examples with others in the MATLAB community,
submit them to the File Exchange. Include the original