Using Print command to save pdf of figure to a different directory

33 次查看(过去 30 天)
Hi,
I have struggled to create a pdf of a figure and put it in a Latex directory for inclusion in an academic paper. I can create the pdf with no difficulty to the present matlab working directory which is C:\Thomas Jacobs\Dropbox\Matlab:
...
h=gcf;
set(h,'Position',[0 0 1200 800]);
set(h,'PaperOrientation','landscape');
set(h,'PaperPositionMode','auto');
print(gcf,'-dpdf','SVGL_2Yr_Annual.pdf');
however, when I try to change the target directory to a Latex subfolder:
...
h=gcf;
set(h,'Position',[0 0 1200 800]);
set(h,'PaperOrientation','landscape');
set(h,'PaperPositionMode','auto');
print(gcf,'-dpdf','C:\Thomas Jacobs\Dropbox\Latex\Research\FHLB Advances\SVGL_2Yr_Annual.pdf');
I get:
>> FHLB_SVGL2_Analysis Error using name (line 102) Cannot create output file 'C:\Thomas Jacobs\Dropbox\Latex\Research\FHLB Advances\SVGL_2Yr_Annual.pdf'
Error in print (line 210) pj = name( pj );
Error in FHLB_SVGL2_Analysis (line 70) print(gcf,'-dpdf','C:\Thomas Jacobs\Dropbox\Latex\Research\FHLB Advances\SVGL_2Yr_Annual.pdf');
This target directory is not in my matlab path and the delay between the creation of the figure on my screen and the program completion makes me wonder if this is some sort of write permission issue. While I have seen many combinations of using other commands like saveas, sprintf, etc., I have not found an exact analogy for my issue on the forum and would appreciate it if one of you could share some further troubleshooting steps for me. Thanks.
Tom
  3 个评论
Thomas Jacobs
Thomas Jacobs 2014-3-22
Salaheddin,
Thanks for the help. It is my own personal machine and my account so I hope this is not an issue, but have no idea if there is some setting Matlab has that I would have to modify. I presented more in the final answer to the question.
Tom
Mehdi Zomorodian
Mehdi Zomorodian 2017-9-14
Dear Thomas,
As I understood, the print function cannot create a new directory, so if you try to save your file in a new one, you will face this problem. So, a quick solution is to create the directory manually or by coding before trying to use print function.

请先登录,再进行评论。

采纳的回答

Thomas Jacobs
Thomas Jacobs 2014-3-22
Joseph,
Thanks very much for the reply! I was unfamiliar with the movefile command (been years since I was an active user and just getting back in the swing). I played with that command for some time before finally getting it to work. Ultimately tried to write in successive directories failing to C:\ and C:\Users\Thomas Jacobs\Dropbox but all the others worked (odd). The most important part was discovering that I had left the Users directory step out of everything so far. Ultimately, was able to get this print command to work:
print(gcf,'-dpdf','C:\Users\Thomas Jacobs\Dropbox\Latex\Research\FHLB Advances\SVGL_2Yr_Annual');
Probably would not have found it save for the movefile suggestion and all the time I spent trying to get it to work. Proper format for that was:
movefile('SVGL_2Yr_Annual.pdf','C:\Users\Thomas Jacobs\Dropbox\Latex\Research\FHLB Advances');
So, thank you again for your timely assistance and have a good weekend!
Tom
  1 个评论
Joseph Cheng
Joseph Cheng 2014-3-22
Good to hear that you were able to figure it out. Cannot believe I missed the Users, however I still try to go to Documents and Settings once and a while to look for my documents.

请先登录,再进行评论。

更多回答(1 个)

Joseph Cheng
Joseph Cheng 2014-3-21
can you use
...
h=gcf;
set(h,'Position',[0 0 1200 800]);
set(h,'PaperOrientation','landscape');
set(h,'PaperPositionMode','auto');
print(gcf,'-dpdf','SVGL_2Yr_Annual.pdf');
movefile('SVGL_2Yr_Annual.pdf','C:\Thomas Jacobs\Dropbox\Latex\Research\FHLB Advances\SVGL_2Yr_Annual.pdf')
If that's the case it might not be a permissions issue.
  2 个评论
Joseph Cheng
Joseph Cheng 2014-3-21
I don't like it either! I guess I could have suggested trying to fid = fopen a random temp file? But it was something that would test out writing to the directory and I cannot see anything wrong with his command.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by