MATLAB Answers

How to save a figure as a PDF with given aspect ration, resolution (and size)

29 views (last 30 days)
Benoît on 8 Sep 2017
Commented: Cedric Wannaz on 8 Sep 2017
I'm looking for a way to save my figures in a way that they can be properly inserted in latex (PDF is preferable but another format will also do). My requirements are the following:
1) Keep a predefined aspect ratio for the picture 2) Make all font sizes (ticks, labels, legend, ...) consistent with the image size. 3) Impose the resolution of the image
I tried several things and what worked best for me until now was
if true
pbaspect([1.5 1 1])
Regarding 1), I used the command pbaspect to keep the aspect ratio of the plot constant and independent of the plot window size. This is OK Regarding 2), the font size is dependent of the resolution: the highest the largest the font size. How to avoid that? Regarding 3), I use '-r200' to impose a 200dpi but this doesn't work quite well with fonts as I mentioned above.
When trying to make a pdf I used
if true
set(fig,'PaperPositionMode','Auto','PaperSize',[p1(3), p1(4)+p2(4)])
print('AccuracyVsPerf','-dpdf','-r100') % code
However, the xlimit is not respected anymore, maybe because the aspect ratio I imposed doesn't allow it..
I must say that I tried a lot of different things with the available options with print, saveas, etc, and none of them worked as I wanted so I'm a bit lost and would appreciate any robust implementation for this purpose. Thanks!

Answers (1)

CARLOS RIASCOS on 8 Sep 2017

Hello friend, maybe the following code will serve you.

Basically what it does is to change the characteristics of the plot, putting a font (Times new roman), a font size and a resolution that you can choose The same code exports the image as .eps which is the best format for inserting images in LaTeX. You would get something like the following:

y1=sin(x); %function
hold on
grid on
 set(gca,'Units','normalized',... %characteristics of plot.
       %(format export,name file, resolution
print ('-depsc', 'export_hgraph', '-r200')end


Community Treasure Hunt

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

Start Hunting!