Exporting image to bounded pdf

If I do this:
set(gcf,'PaperPosition',[0,0,4,3]) print -depsc2 filename.eps
I get a nice image I can include in a latex document. However, if I need to use pdflatex, then I need a pdf image. Now, I know I can use epstopdf to convert the above eps image to pdf, and it works very well, but I wonder if I can do this directly from Matlab. When I try:
set(gcf,'PaperPosition',[0,0,4,3]) print -dpdf filename.pdf
I get a full page, with the image in the lower left corner. Now, I know of tools I can use to crop this result, but I am wondering if I can do this directly in Matlab code.
David Arnold College of the Redwoods Eureka, CA 95501

 Respuesta aceptada

Junaid
Junaid el 24 de En. de 2012

5 votos

There are couple of ways to do it
1. plot something do this code
plot([1:10]);
set(gcf, 'PaperPosition', [0 0 5 5]); %Position plot at left hand corner with width 5 and height 5.
set(gcf, 'PaperSize', [5 5]); %Set the paper to have width 5 and height 5.
saveas(gcf, 'test', 'pdf') %Save figure
2. if you have installed latex then pdfcrop is already installed with it so you can do like this.
saveas(gca,'test.pdf');
system('pdfcrop test.pdf test.pdf');
3. you make your own save function and follow 1 or 2 or may be some other procedure to do it.

4 comentarios

David Arnold
David Arnold el 24 de En. de 2012
Very, very nice. Thanks. I can't tell you how helpful this will be to my students.
One more question. I tried your approach in the following code, but it truncated my labels a bit. What is the best strategy to make room for my labels, while continuing to control the size of the figure pdf output?
%%
close all
t=linspace(0,2*pi,500);
P=1/17*exp(-t).*(4*sin(4*t)-cos(4*t))+18/17;
plot(t,P)
xlabel('$t$-axis',...
'interpreter','latex',...
'FontSize',10)
ylabel('$P$-axis',...
'interpreter','latex',...
'FontSize',10)
title('$P''=e^x/(y+1)$',...
'interpreter','latex',...
'FontSize',12)
grid on
axis tight
set(gcf, 'PaperPosition', [0 0 4 3]); %Position plot at left hand corner with width 5 and height 5.
set(gcf, 'PaperSize', [4 3]); %Set the paper to have width 5 and height 5.
saveas(gcf, 'test', 'pdf') %Save figure
Junaid
Junaid el 24 de En. de 2012
Hi,
I have updated the link, you can use that, that is also very effective
Junaid
Junaid el 24 de En. de 2012
Hi,
If you are linux user then Option 2 works perfectly for all cases. Just two lines without any configurations :-), I m not sure that pdfcrop command works on windows also after installing the latex :-).
David Arnold
David Arnold el 24 de En. de 2012
Nope, none of these work on the Mac. When using the latex interpreter, the labels are cut off.
http://msemac.redwoods.edu/~darnold/math55/matlab/test.pdf
D.

Iniciar sesión para comentar.

Más respuestas (3)

Junaid
Junaid el 24 de En. de 2012

2 votos

follow code generated good pdf check
close all;
t=linspace(0,2*pi,500);
P=1/17*exp(-t).*(4*sin(4*t)-cos(4*t))+18/17;
plot(t,P)
xlabel('$t$-axis',...
'interpreter','latex',...
'FontSize',10)
ylabel('$P$-axis',...
'interpreter','latex',...
'FontSize',10)
title('$P''=e^x/(y+1)$',...
'interpreter','latex',...
'FontSize',12)
grid on
axis tight
set(gcf, 'PaperPosition', [0 0 5 5]); %Position plot at left hand corner with width 5 and height 5.
set(gcf, 'PaperSize', [5 5]); %Set the paper to have width 5 and height 5.
saveas(gcf, 'test', 'pdf') %Save figure
Make sure to put width and height to 5
set(gcf, 'PaperPosition', [0 0 5 5]); %Position plot at left hand corner with width 5 and height 5.
set(gcf, 'PaperSize', [5 5]); %Set the paper to have width 5 and height 5.
saveas(gcf, 'test', 'pdf') %Save figure

2 comentarios

David Arnold
David Arnold el 24 de En. de 2012
Nice! Worked. See:
http://msemac.redwoods.edu/~darnold/math55/matlab/test2.pdf
But these are not the dimensions I want.
Junaid
Junaid el 25 de En. de 2012
I m sorry David, I didn't get what you mean by dimensions. If you mean the x and y label/ticks then you can set manually.
set(gca,'YTick',[1.0 1.05 1.10 1.15 1.2 ]);
or even exactly whatever your values are.

Iniciar sesión para comentar.

Florin Neacsu
Florin Neacsu el 24 de En. de 2012

1 voto

Hi,
The most popular file of fileexchange might do the trick:
Regards, Florin

3 comentarios

David Arnold
David Arnold el 24 de En. de 2012
Here is my first attempt with export_fig:
%%
close all;
t=linspace(0,2*pi,500);
P=1/17*exp(-t).*(4*sin(4*t)-cos(4*t))+18/17;
plot(t,P)
xlabel('$t$-axis',...
'interpreter','latex',...
'FontSize',10)
ylabel('$P$-axis',...
'interpreter','latex',...
'FontSize',10)
title('$P''=e^x/(y+1)$',...
'interpreter','latex',...
'FontSize',12)
grid on
axis tight
set(gcf, 'PaperPosition', [0 0 4 3]);
set(gcf, 'PaperSize', [4 3]);
export_fig test3 -pdf
And here is what I got:
http://msemac.redwoods.edu/~darnold/math55/matlab/test3.pdf
Grid lines are a bit light and background is not white. Let me look at some more options of export_fig.
David Arnold
David Arnold el 24 de En. de 2012
OK, this export_fig uses the screen position and coloring, so I tried this:
close all;
t=linspace(0,2*pi,500);
P=1/17*exp(-t).*(4*sin(4*t)-cos(4*t))+18/17;
plot(t,P)
xlabel('$t$-axis',...
'interpreter','latex',...
'FontSize',10)
ylabel('$P$-axis',...
'interpreter','latex',...
'FontSize',10)
title('$P''=e^x/(y+1)$',...
'interpreter','latex',...
'FontSize',12)
grid on
axis tight
set(gcf, 'Color','White')
set(gcf,'Units','Inches')
set(gcf,'Position',[5,4,4,3])
export_fig test4 -pdf
This gave me the following image, which has file size 16KB.
http://msemac.redwoods.edu/~darnold/math55/matlab/test4.pdf
Which appears like this in the tex file.
http://msemac.redwoods.edu/~darnold/math55/matlab/firsttex.pdf
Again, the grid lines appear a bit faded. But progress is being made thanks to some very helpful people. Much appreciated.
David.
Florin Neacsu
Florin Neacsu el 24 de En. de 2012
Hello,
You can set grid line width with the axes LineWidth property, but this will changes both the axes and the grid.
Also you change the style of the grid lines with
set(gca,'GridLineStyle','-')
Unfortunately there is not something like
set(gca,'GridLineWidth',10)
HTH,
Florin

Iniciar sesión para comentar.

David Arnold
David Arnold el 25 de En. de 2012

0 votos

I want to thank everyone for their help on this question. I am now going to close the question.
Thanks so much.
David.

Categorías

Más información sobre Graphics Object Properties en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 24 de En. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by