Save MATLAB subplot as PDF - matlab

In my project i have 10 subplot commands. I want the subplots to be saved in PDF format. I tried by using saveas(gcf,'Trial_Plot.pdf') command at the end of all these subplot commands, then i get a PDF with all the subplots in a crowded manner (with large margins in the PDF).
How can I avoid this? Is it possible to create the PDF for the first subplot command, then for the rest the subplots are appended to that same PDF? Is this appending method possible ?

Related

Problems saving figure in Matlab with right proportions in pdf file

I have a figure with 2x3 subplots in Matlab where each subplot contains 2 histograms. I typically save Matlab figures in eps but because doing so removes the transparency between the histograms in each subplot, I resorted to saving as pdf.
Currently, this is how I'm saving the file:
figure;
set(gcf,'Position',[100 100 1400 500])
set(gcf,'PaperPositionMode','auto')
print(gcf, 'filename.pdf', '-dpdf','-r0');
However, it produces a pdf file in portrait layout which means some subplots get cut out.
Now, I've been scouring for answers online and the recommended approach to fix my problem is given in this link.
Following the approach, I don't have any subplots cut out but the problem is that this forces the figure to fill-in the space of my pdf. In otherwords, my histograms are stretched out to fill the space of the pdf file in landscape format. I'd simply prefer a pdf file that has exactly the same proportions as my figure size above which is set(gcf,'Position',[100 100 1400 500]). This is how it works with eps so I'd like to have the same behavior with pdf.
Can anyone help me to fix it?
To make sure the output is landscape:
set(gcf,'PaperOrientation','landscape')
Then use the "bestfit" option. That will scale it as large as possible but maintain the aspect ratio.
print(gcf, 'filename.pdf', '-dpdf','-r0','-bestfit')
There some more good info on Matlab's website
MATLAB’s PDF output is always on a full A4 or Letter-sized page by default. With 'PaperPositionMode' set to 'auto', this does not change. For other output file formats (e.g. EPS) that setting causes the page size to be adjusted to the figure size. Not so for PDF. You need to manually set the 'PaperPosition' and 'PageSize' properties.
This File Exchange submission handles this for you (I’m the author).

White lines in figure after exporting to PDF

A 2D data matrix was plotted in MATLAB 2016a using contour (the first Figure below), and then I saved as the figure in the *.emf format. Next, I inserted the figure (emf) into a MS word document. And finally, the word document was converted to a pdf file.
I found that there are many white lines in the figure (when in a pdf format) as shown in the second figure below. My question is how can I remove those white lines?
The code is attached here:
path = 'C:\Users\Administrator\Desktop\';
data = importdata([path, 'lsa2.txt'], ' ', 6);
cdata = data.data;
n = 25;
contourf(cdata,n, 'LineStyle', 'none');
colormap(jet);
axis equal;
The data can be accessed here: https://www.dropbox.com/s/hzf75qiju6zsy9i/lsa2.txt?dl=0
As I mentioned in my comment, this is a bug with the way MATLAB exports graphics, as explained by Yair Altman and Dene Farrell:
I discovered that these white line artifacts happen when the painters renderer is used ... [which] is the default rendering [format engine] for vectorized (EPS/PDF) formats.
There are two separate issues with the Matlab export:
1. The main thing that everyone notices is that patches are broken up into triangles, each of which is a separate path object if inspected in illustrator.
2. Matlab sometimes adds extraneous 'cropping paths' that create an apparent white line even when there is no issue with fractured paths.
One workaround suggested there by ambramson is the following:
1. Save the figure as an .eps file (using the print command).
2. Using a text editor, change the line in the eps header from:
/f/fill ld
to:
/f{GS 1 LW S GR fill}bd
and move the line down several lines, to right below the /LW/setlinewidth ld line.
From here, your eps file should display fine on all pdf viewers.

Exporting and printing matlab figure [duplicate]

This question already has answers here:
Saving MATLAB figures as PDF with quality 300 DPI, centered
(2 answers)
Closed 2 years ago.
I have a Matlab figure. I need to use it as a picture so I can insert it in Word, then save that new Word document as a PDF file and then print that PDF file. I have tried saving Matlab figure as .JPG and . PNG file but the picture gets "smaller" when inserted in Word and converted to PDF and when printed, you can hardly distinguish anything. What can I do so that the quality of the figure dosen't change?
You have a couple of options here.
Specify the Resolution
When saving the figure, by default 72 dpi is used (your screen resolution) which may causeyour images to appear small when being inserted into a Word document. Rather than using saveas, you can use print to specify the resolution of the resulting image. For a PNG
print(hfig, 'filename.png', '-dpng', '-r300') % Resolution = 300 DPI
Note that if you're using print, you will also need to specify a few other properties of the figure to get it to look similar to the on-screen appearance.
set(hfig, 'Units', 'inches');
pos = get(hfig, 'Position');
set(hfig, 'PaperPositionMode', 'manual', ...
'PaperPosition', [0 0 pos(3:4)], ...
'InvertHardCopy', 'off');
Use an EPS
If you're ultimately exporting to a PDF and your image is line art, consider exporting to an EPS (a vector-graphic). Again, this can be done using print.
print(hfig, 'filename.eps', '-depsc2')
Use export_fig
The FEX submission export_fig is a widely-recommended utility for creating figures from your MATLAB figures in a way that preserves the way that they appear within MATLAB.
You don't need to save it as a file, just copy it to the clipboard and then paste it, it will be copied as a metafile (vector graphics) that looks good when resized. It is best to resize by dragging the corners to maintain the original aspect ratio.
After you make a figure, select from the figure menu: Edit / Copy Figure and then paste into Word or PowerPoint. You can change some of the options for copying by selecting Edit / Copy Options... from the figure menu

Matlab figures: Which format to reuse in Matlab?

I have two scripts that produce some plots. I want to reuse those figures in another script, which will create subplots with the plots from the first script on the left-hand side, and plots from the second script on the right-hand side.
I tried to save the figures from the first scripts in tiff and then loading them in the last script, but the result is blurry (probably due to resizing). Which format should I use to save the figures with the first scripts?

make grid lines bigger in matlab figures

How can I make grid lines bigger (more fat for printing purpose) in my matlab figures?
I'm including matlab figures in to my .tex document after using the following
print -depsc testFig.eps
to convert the figure into .eps for inclusion in my .tex doc.
But my grid lines don't look good at all. i.e they appear faint when I print the document. Is there anyway I can increase the size/width of the grid lines?
If you use
set(gca,'LineWidth',10)
after "grid on" this should increase the boarders of all axes, including the grid lines.