How to Get MATLAB to Save Non-Corrputed PDF Files?

3 views (last 30 days)
BM
BM on 30 Oct 2017
Commented: BM on 30 Oct 2017
Ok, I have an issue that seems quite strange to me, and I don't exactly understand why I am getting mixed results when I run the same code. I wrote a test code sample below to illustrate the problem I am having.
cd('~/Documents/MATLAB/plots/test')
for x = 1:1:10;
y = x+2;
plot(x,y,'*')
filename = strcat('x_',num2str(x));
print(gcf,'-painters','-dpdf',strcat(filename,'.pdf'));
%print(strcat(filename,'.jpg'));
%save(strcat(filename,'.pdf'))
end
This code literally takes x = 1, 2, ... and adds a value of 2 to it. I wrote this test program to see if indeed I could make 10 figures that automatically save as PDF files, dynamically assigning the filename, with each plot graphing a single point. This works exactly as intended except for one major issue - the PDF files are corrupted. Running the same code without modifications sometimes gives the first 3 plots correctly saved, and able to be viewed, with the rest corrupted. Other times, all 10 are corrupted. Does anyone have a clue as to why I would obtain such results?
Furthermore, when I use code, the filename being modified to include more variables, in my actual program, I obtain the following error:
Error using ghostscript (line 186)
Problem converting PostScript. System returned error: -1.Failed to convert to output format; Ghostscript status: -100.Error: /ioerror in
--.outputpage--
Does anyone have any suggestions on how I might attack this issue? Furthermore, I know I can vectorize code to forgo for-loops. If I wanted to graph the entire line y = x+2 from [1,10], then using x = 1:1:10 is the obvious way to vectorize the code. In the above, I used a for-loop to separate out the graphs so that each graph contains one point. Vectorizing it via x = 1:1:10 would graph the whole line at once, which is what I don't want. Is there a way to do what my code does above without a for-loop?
  2 Comments
BM
BM on 30 Oct 2017
Just FYI, the error message had a bit more information in it. Hopefully this should also help.
Error in print>LocalPrint (line 307)
pj = ghostscript( pj );
Error in print (line 233)
LocalPrint(pj);
Error in ShG_1singularity_Analytic_Continuation_HAMILTON_V2 (line 516)
print(gcf,'-painters','-dpdf',strcat(filename,'.pdf'));
BM
BM on 30 Oct 2017
As an update, I literally went to do something else, and came back to this program. I ran the above code again, and now all plots are saved as PDF files without issue! I changed nothing in the code! If anyone has any ideas removing the for-loop and still obtaining 10 plots, I would love their advice on that as well!

Sign in to comment.

Answers (1)

BM
BM on 30 Oct 2017
Please discard the first part of this question. Everything now seems to work, and I am a little perplexed, as I literally changed nothing in the code. I just waited a bit, and re-ran the code. I did clear the workspace and command window, but I did this before as well! I don't really have an answer for why it is now working fine when it didn't before.
  1 Comment
BM
BM on 30 Oct 2017
The part still open is how to get the code above to do the same thing it does currently without a for-loop.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!