How to save figure in pdf without margins?

123 visualizaciones (últimos 30 días)
Mr M.
Mr M. el 21 de Jul. de 2018
Comentada: André de Castro el 19 de Dic. de 2019
Let us suppose I have a figure with definite pixel size:
set(gcf,'unit','pixel','position',[0 0 figure_width figure_height])
I would like to save it as a pdf. I know the aspect ratio, but how to set the size? I don't want unnecessary margins. I want to use something like this
set(gcf,'PaperPositionMode','auto','papersize',[a*figure_width a*figure_height]);
%print(gcf,'Fig','-dpdf','-r0')
But how to choose the value of 'a' ?

Respuestas (1)

Jan
Jan el 22 de Jul. de 2018
The value of a depends on the wanted size of the PDF. You can choose it freely to what you need. To remove the border use:
print(gcf, 'Fig.pdf', '-dpdf', '-fillpage')
  3 comentarios
Jan
Jan el 9 de Dic. de 2019
Did you try the -loose flag in the print command?
'-loose' — Use a loose bounding box. EPS and PS files only.
André de Castro
André de Castro el 19 de Dic. de 2019
Using -loose did improve the results. It worked for PDF, EPS and PS files. I also had to:
  • not use -fillpage;
  • set the Position property before printing, as below.
width = 3.5;
height = 2.2;
figure_handle.Units = 'inches';
figure_position = figure_handle.Position;
figure_handle.Position = [figure_position(1),figure_position(2), width, height];
It seems strange that I had to set the figure Position though, as I had already set PaperUnits and PaperSize appropriately.
Combining -loose and the above code with the solution suggested here, in the Expand Axes to Fill Figure section, gave even better results, cropping as close as possible to the drawn area. Said solution was not effective when I had tried it before because I was using -fillpage.

Iniciar sesión para comentar.

Categorías

Más información sobre Printing and Saving en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by