askvity

How to Export MATLAB Code as PDF?

Published in MATLAB Export 4 mins read

Exporting your MATLAB code as a PDF document is a useful way to share your work, including code, output, and formatted text.

You can export MATLAB code to PDF primarily through two methods: using the Live Editor for .mlx files or by publishing standard .m files.

1. Exporting Live Scripts to PDF

MATLAB's Live Editor (.mlx files) is designed for creating executable narratives that include formatted text, code, output, and visualizations. Exporting from the Live Editor is the most straightforward way to get a well-formatted PDF.

Steps to Export a Single Live Script:

  1. Open the .mlx file in the MATLAB Live Editor.
  2. On the Live Editor tab, in the Export section, click the Export dropdown.
  3. Select PDF.
  4. Choose the desired settings in the export dialog box (like paper size, orientation, and margins).
  5. Click Export.

This saves the entire content of your live script, including code, outputs (like plots and tables), and formatted text, into a single PDF file.

Exporting a Folder of Live Scripts:

According to the reference, MATLAB provides a convenient way to export multiple live scripts and functions at once.

  • To export all of the live scripts and functions in a folder, on the Live Editor tab, select Export > Export Folder.

This feature allows you to process multiple .mlx files in a directory, automatically converting each into its own PDF document. You can also configure export settings for the batch process.

Customizing PDF Output:

When exporting from the Live Editor to PDF (as well as Microsoft Word and LaTeX), you have options to customize the appearance of the output document.

  • Change the paper size, orientation, and margins when exporting to PDF, Microsoft Word, and LaTeX.

These options help ensure your exported PDF looks exactly how you want it, fitting content appropriately on the page.

2. Publishing Standard Scripts to PDF

For standard MATLAB scripts (.m files), the primary method for creating a formatted document that includes code and output is by "publishing" the script. Publishing generates a formatted report in various formats, including PDF.

Steps to Publish a Standard Script to PDF:

  1. Open the .m file in the MATLAB Editor.
  2. On the Editor tab (or Publish tab, depending on your MATLAB version), find the Publish section.
  3. Click the Publish button or the dropdown arrow next to it.
  4. If using the dropdown, select Edit Publishing Options... to configure settings.
  5. In the Publishing Options dialog box, under Output format, select pdf.
  6. Configure other options as needed (e.g., output folder, figure format, whether to execute code).
  7. Click Publish.

MATLAB will execute the script and generate a PDF containing the code (syntax highlighted), comments, and the output generated by the code (including plots).

Comparison of Methods

Here's a quick overview of the two main methods:

Feature Live Editor Export (.mlx) Publishing Standard Script (.m)
File Type Live Script (.mlx) Standard Script (.m)
Includes Code, Output (plots, tables, command window), Formatted Text Code, Output (plots, command window), Comments
Ease of Use Generally simpler for interactive content Requires configuration via publishing options
Batch Export Yes (Export Folder feature) Yes (using publish command or loop)
Customization Paper size, margins, orientation Various options via Publishing Options dialog

Both methods achieve the goal of getting your MATLAB code and results into a PDF format, but the Live Editor provides a more integrated experience for creating documents with mixed code and narrative. Publishing is versatile for older scripts or automating documentation.

Related Articles