Publishing MATLAB Code as PDF
Publishing MATLAB Code
MATLAB allows you to convert your code into a PDF document. By default, MATLAB runs the code during publishing and will include in the PDF any figures or text displays that the code produces. MATLAB also allows you to format your m-file to allow certain types of formatting in the PDF, like header and paragraph text.
The process is slightly different if you are on MATLAB Online or MATLAB Desktop.
Download these files to practice publishing: publish_quadScript.m and publish_quadFunction.m
MATLAB Desktop
- Open your m-file in MATLAB editor window.
- In the top menu ribbon, click the Publish tab.
- Click on the bottom half of the Publish button.
- Click Edit Publishing Options to open the Edit Configurations dialog window.
- Functions with inputs or outputs only: In the MATLAB Expression area, replace the last line with a valid function call. Use proper input argument values. Include the semicolon (;) at the end of the call.
- Set Output Format to pdf.
- Click Publish.
MATLAB creates a folder named html in your current working folder and saves the PDF inside. It also opens the file in a PDF viewer for you to see, if you have a PDF viewing program installed on your computer.
If you do not have a PDF viewer, you may get an error in the Command Window stating the file did not open. This error does not affect the actual PDF file, which you can opt to open it in a browser window if you want to review it before submission.
You can access the file from your computer’s folder structure.
Scripts and functions with no inputs or outputs
Functions with inputs and/or outputs
MATLAB Online
Important: Before you start, set your browser to allow popup windows for https://matlab.mathworks.com. Search the internet for instructions specific to your browser.
Scripts and functions with no inputs or outputs
- In MATLAB Online, open your m-file in the Editor.
- In the top menu ribbon, click the Publish tab.
- Click on the Publish as PDF button in the Publish section of the top menu.
- Download the generated PDF, if necessary.
MATLAB creates a folder named html in your current working folder on MATLAB Drive and saves the PDF inside. It also opens the file in a browser window (because you allowed popups). You can download the file from browser window, or you can download from MATLAB Drive.
Functions with inputs and/or outputs
- Open your m-file in the Editor and then click the Publish tab.
- Click on the bottom half of the Publish button.
- Click Edit Publishing Options to open the Edit Configurations dialog window.
- In the MATLAB Expression area, replace the last line with a valid function call. Use proper input argument values. Include the semicolon (;) at the end of the call.
- Set the Output Format to pdf.
- Click Publish.
Just like for scripts, MATLAB creates a folder named html in your current working folder on MATLAB Drive and saves the PDF inside. It also opens the file in a browser window (because you allowed popups). You can download the file from browser window, or you can download from MATLAB Drive.
Troubleshooting
Issue | Cause | Possible Fix |
---|---|---|
Formatting issues in PDF (extra whitespace, duplicated figure, etc) | Lag in MATLAB/internet when publishing | Close the faulty PDF and republish. This usually fixes formatting issues. |
Trying to publish while using the INPUT command | INPUT requires interaction with a user, which is not possible during publishing. | Follow instructions in assignment. |
Red error messages in PDF | Error in code execution during publishing. | Fix any errors that occur when you execute your code normally. Then confirm the errors are not due to the publishing process (using input command, did not include a valid function call, etc.) |
Error indicating PDF did not open | No PDF viewing app on your computer, or popups not allowed in MATLAB Online. The publishing process requires MATLAB to attempt to open the final PDF. | Check the html folder. If the file is present, you can view the PDF in a browser if you want to review it before submitting. |
Unsuppressed information in PDF | One or more commands in m-file are unsuppressed. In functions, the function call during publishing is not suppressed. | Add semicolon to the end of all variable assignment commands and function calls. |
My command window is full of orange or red Java errors | Varies based on computer and installation of MATLAB. | Publishing will still work even with those errors. Clear them with clc if they are distracting you. They will not appear in the PDF. |