How to export a PDF with figures on multiple pages? - matlab

I am trying to export a larger number of Matlab figures that are generated in for loop to a single PDF file. Right now the best thing I could come up with is to all print them to a PostScrip file using the -append option like this:
print('Temp_Plots','-dpsc','-append')
After that I could convert the PS file to a PDF file. This workflow was okay until I started to use plots with 2 y axis. Unfortunately it seems like Matlab's PS export cannot properly handle this situation and does not color the lines appropriately.
As there is no -append option for the direct PDF export what other methods do I have to append all my plots to a single file without losing the assigned colors or other hickups?

I would recommend trying out the publish command and push that to its limits first.
Following the documentation:
options = struct('format','pdf','outputDir','C:\myPublishedOutput');`
publish('myCode.m',options);
Take a look at Publishing Markup to see how to get the look you want.
This search brings up some possibly related posts, but none that I saw that directly match your issue.
References:
1. Publishing Markup (Mathworks)
2. Output Preferences for Publishing (Mathworks)
3. Publishing M-Files in MATLAB
4. Publish Your Work in Matlab

Related

Merge 2 pdf files and preserve forms

I'd like to merge at least 2 PDF files into one while preserving all the form elements in the original PDFs. The form elements include text fields, radio buttons, check boxes, drop down menus and others. Please have a look at this sample PDF file with forms:
http://foersom.com/net/HowTo/data/OoPdfFormExample.pdf
Now try to merge it with any other arbitrary PDF file.
Can you do it?
EDIT: As for the implementation, I'd ideally prefer a command line solution on a linux plattform using open source tools such as 'ghostscript', or any other tool that you think is appropriate to solve this task.
Of course, everybody is welcome to supply any working solution to this problem, including a coded solution that involves writing a script which makes some API calls to a pdf-processing library. However, I'd suggest to take the path of least resistance first (CMD Solution).
Best Regards
EDIT #2: Well there are indeed several CMD tools that merge PDFs. However, these tools don't seem to, AFAIK, to preserve the forms in the original PDFs! These tools appear to simply just concatenate the printouts of all those PDFs into a single Printout, which is then presented as a single PDF.
Furthermore, If you printout a PDF file with forms into a file, you lose all the forms in it. This clearly not what I'm looking for.
I have found success using pdftk, which is an open-source software that runs on linux and can be called from your terminal.
To concatenate multiple pdfs into one (and preserve form-fillable elements), you can use the following command:
pdftk input1.pdf input2.pdf cat output output-file.pdf

Matlab can't find publish file. Where is it?

I have found the publish menu in Matlab. I would like to reference it in my code so as to save output ( without the commands) and all figures. I am using only figure1 throughout. I want to save in two formats pdf and doc.
Where do I put the publish command at the beginning or at the end of my code or putting it another way should I execute the publish commands at beginning? Matlab software keeps telling me it cannot find the file? Further I want to include all graphs. I have added snapnow after each plot.
I have used the following code both at beginning and end of my program. I want to include all graphs but no input commands only output. Options can be seen on edit publishing options window. I tried to start simple.
Code:
publish('c:\data\output.doc')
publish('c:\data\output.doc','doc')
Error message
Cannot find "c:\data\output.doc".
Thank you.
MM

Code folding in Live Scripts

Is there a way to fold sections of codes in Live Scripts as there is in regular scripts?
I have tried to look for it in the preferences and enabled all kind of foldings, but none of them relate to the Live Scripts. I would expect it to work since the notation of %% is similar in both types of scripts.
Any idea how to enable/workaround this?
No, I don't believe you can do this. Seems like a nice thing to want to do, though, so I would enter an enhancement request with MathWorks. I have no suggestion for a workaround, I'm afraid.
I know this is old but in case anyone else is wondering, below are the workarounds suggested to me by tech support when I submitted an enhancement request. I encourage the reader to add their vote by submitting a request if they still want to see this feature added to the live editor.
Convert to M file and "publish" with 'showcode' as false. First, convert the Live Script into an M file. You can do this interactively
by clicking "Save As" and then choosing the "M" file option in the
File Type section or you can do it programmatically as follows:
>> matlab.internal.liveeditor.openAndConvert('live_script_to_be_converted.mlx',
'output_script.m');
Once you have the converted M file which has all the section breaks
and formatting, you can use the "publish" function as follows from the
MATLAB Command Window:
>> options.format = 'pdf';
>> options.showCode = false;
>> publish('output_script.m',options)
This will publish the PDF file to a sub-directory called "html" inside
the current working directory.
Here is a documentation link for the "publish" function and
specifically, the "showCode" option:
https://www.mathworks.com/help/matlab/ref/publish.html#input_argument_namevalue_d119e823467
Please note, when saving the MLX file as M file and then publishing,
it does not convert the LaTeX expressions to something readable. It
also automatically inserts a table of contents at the beginning.
Encapsulate the code in another script or function. An alternate approach would be for you to encapsulate the code you want to hide in
another script or function and call it from your published Live
Script. For example, something like:
>> %% Now plot a figure
>> functionThatPlotsAFigure
In this case, instead of publishing all the plotting code, only the
call to the plotting function would be published.
Publish the Live Script as an HTML and then modify the HTML page to hide the code sections and then publish it as a PDF.
The options stated above are simply workarounds as we do not currently have this functionality. A request has been submitted to our developers to add in this feature and they are considering this for future releases of MATLAB. We appreciate your feedback as we constantly strive to improve our products.

MATLAB publish, formatting LaTeX output header

I have thousands of lines of MATLAB ranging over several scripts that I have commented and broken up into cells. I would now like to publish. I have some universal formatting that I would like to apply. I would also like to be able to publish each script independently. At the moment I have written all these formats into a file called 'Formats.m'. So at the start of every script it publishes with the word 'Format' at the top.
Is there a way I can call in the formatting file without seeing the name published in the final script?
publish has an option codeToEvaluate which might be what you're looking for here.
To publish foo, but run Format first, try this:
>> opts.codeToEvaluate = 'Formats; foo';
>> publish('foo',opts)
If you're working from the Editor, the same option is available as a publish configuration.

How to change PowerPoint chart data with .NET?

I have a PowerPoint template that contains one slide and on that slide is a chart. I'd like to be able to manipulate that chart's data using .NET.
So far I have code that...
unzips the Powerpoint file.
unzips the embedded excel file (ppt\embeddings\Microsoft_Office_Excel_Worksheet1.xlsx)
It successfully manipulates the data in the excel sheet and zips it back up.
Opens and manipulates ppt\charts\chart1.xml
Powerpoint is then zipped up and delivered to the user
The result of this is a PowerPoint file that shows a blank chart. But when I click on the chart and go to edit data it updates the data and shows the correct chart.
I believe my problem is with the chart1.xml that I am generating. I have compared my generated version with a version created by PowerPoint and they are almost identical. The only differences are in the values for <c:crossAx/> and <c:axId/>.
There are also some rounding differences in the data. But I do not feel like that would result in a blank chart.
Is there another file that I need to edit? Does anyone have any ideas as to what else I should try to get this working?
It's likely a combination the axID value and the rounding issues. The Axis ID, likely, is asking for an integer value and you may be supplying a single/double. So the cached data in chart1.xml doesn't know how to display.
Try your same manipulation that you've been doing, but instead of opening the result in PowerPoint, change the .pptx extention to .zip, unzip and then manually fix the rounding issues to match the original rounding. Then zip back up, change extension back to .pptx and open in PowerPoint. If this fixes the issue of display, you can confirm it is the rounding issue.
Alternatively, and along the same lines. Open your resulting PPTX in PowerPoint as you have been doing, and once you've right-clicked and rehydrated the chart, save as a different file name and compare that with your automated result.