MATLAB Answers

Stop some code from Publishing

134 views (last 30 days)
Richard on 22 Oct 2014
Answered: Matt J on 18 Jan 2018
I am looking for a control that lets me choose what gets published when using MATLABs publish feature.
For example:
1) I want to block all the plotting code in my .m file from getting published to a pdf. How can I do this?
2) I want to control the size of the figure that get placed in a pdf. I know the figures created in html output are proportional to the size you create in your code, but the pdf figured appear to be predetermined and are rather small.
Thanks, Rich

Answers (2)

Bruno Pop-Stefanov
Bruno Pop-Stefanov on 23 Oct 2014
1) How to hide some parts of the code
One way to publish parts of the code while hiding some other parts that you still want to execute, is to not include any code in the published document and add the code you want to be published as comments in the original M-file.
I attached publish_example.m to illustrate how you would go about that. To publish this file in PDF, use the following commands. We set the 'showCode' option to false so that none of the code gets published, and we'll have to add the code we want to see published as comments in the M-file.
>> options.format = 'pdf';
>> options.showCode = false;
>> publish('publish_example.m',options)
The file contains two pieces of code. The first one is made of 3 lines (8-10) that display a figure:
t = 0:.1:pi*4;
y = sin(t);
Since 'showCode' is set to false, these three lines will not be published.
The second piece of code (lines 36-45) has been included in the comments in lines 19-34. Therefore, it is published.
You could use the same approach for your code, but you would have to copy and paste all the code you want to see published in a comment section.
Another approach would be to encapsulate the plotting code you want to hide in another script or function and call it from your published script. Something like:
%%Now plot a figure
In this case, instead of publishing all the plotting code, only the call to the plotting function would be published.
This will be less work than copying and pasting all your other code in comments, but it will show functionThatPlotsAFigure in the published document.
2) How to control the size of a figure published to a PDF
Unfortunately, it is currently not possible to change the size of figures published to a PDF file. You can change the size of figures that get published to HTML, but not PDF. This would be a nice enhancement. Thank you for your feedback.

Matt J
Matt J on 18 Jan 2018
If there are only a minority of lines of code that you want to hide, I would first publish as HTML. It's easy enough to edit those lines out of the HTML file. Then convert the resulting file to PDF.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by