MATLAB 新手:使用“saveas”时出错将绘图另存为 pdf 文件

发布于 2024-11-19 06:05:23 字数 1613 浏览 3 评论 0原文

我是 Mac (10.6.8) 用户。我编写了 MATLAB 代码来绘制计算结果,然后将图另存为 pdf。我使用“saveas”(参见下面的示例)。

我收到此错误:

??? Error using ==> print at 325
Problem converting PostScript. System returned error: -1.Failed to convert to output format; Ghostscript status: -100.**** Unable to open the initial device, quitting.

Error in ==> saveas at 155
        print( h, name, ['-d' dev{i}] )

Error in ==> Results_processor at 1219
saveas(gcf,saveFigTo1, 'pdf')

这是相关的代码:

calculationResultsPath    = '/Me/Projects/ThisProject';
calculationResultsDirectory          = strcat( calculationResultsPath,'MATLABProcessedResults' );
mkdir( calculationResultsDirectory );

% ...Code for importing results to be plotted from external files (works fine)...

% ...Code for plotting (works fine)... I get the figures I want.

% The problem is:
saveFigTo1    = strcat(resultsDirectory,'/majorsMgO.pdf') 
saveas(gcf,saveFigTo1, 'pdf') 
hold off
pause
clf;

一些进一步的信息...上周我第一次编写它时效果很好!从那时起,我想我从 10.6.7 更新到了 Mac OS 10.6.8,但我的代码或我使用的 Matlab 版本 (R2009a) 没有任何其他变化(除非我记性很差!)。

另外,我发现了一些关于类似问题的旧建议,以使用“打印”。我尝试使用:

print(gcf,'filename')。我确实得到了一个 pdf 文件,但它无法在任何 pdf 查看程序中打开。我假设(但不确定)这可能与我使用 Mac 有关。我注意到有一些东西(尤其是与外部文件操作相关的)不能在 Mac 上运行。

如果有人可以提供帮助,我将非常感激。


更新: 我找到了适用于 Mac 的 GhostScript 并按照 Chris 的建议安装了它。不幸的是,这不起作用。我在论坛上看到,目前许多 Mac 用户在使用 MATLAB 绘图时遇到问题,可能与 java 有关。上周进行了操作系统更新(至 OS X 10.6.8),问题就从那时开始出现。我的代码在那之前就可以工作了。

我仍然没有找到解决方案,我认为 MATLAB 人们也没有找到解决方案,所以如果有人对如何在不使用 saveas 的情况下保存绘图有建议,我很想听听他们的建议。 “打印”命令对我也不起作用——它生成的 PDF 是我无法打开的。

I am a Mac (10.6.8) user. I have written MATLAB code for plotting calculation results and then saving the plots as pdfs. I use "saveas" (see example below).

I get this error:

??? Error using ==> print at 325
Problem converting PostScript. System returned error: -1.Failed to convert to output format; Ghostscript status: -100.**** Unable to open the initial device, quitting.

Error in ==> saveas at 155
        print( h, name, ['-d' dev{i}] )

Error in ==> Results_processor at 1219
saveas(gcf,saveFigTo1, 'pdf')

Here is the relevant piece of code:

calculationResultsPath    = '/Me/Projects/ThisProject';
calculationResultsDirectory          = strcat( calculationResultsPath,'MATLABProcessedResults' );
mkdir( calculationResultsDirectory );

% ...Code for importing results to be plotted from external files (works fine)...

% ...Code for plotting (works fine)... I get the figures I want.

% The problem is:
saveFigTo1    = strcat(resultsDirectory,'/majorsMgO.pdf') 
saveas(gcf,saveFigTo1, 'pdf') 
hold off
pause
clf;

Some further information... This worked fine last week when I first wrote it! Since then, I think I updated to Mac OS 10.6.8 from 10.6.7, but nothing else has changed in my code or in the version of Matlab I use (R2009a) (unless I have a very bad memory!).

Also, I came across some older suggestions on similar questions to use "print." I tried using:

print(gcf,'filename'). I did get a pdf, but it would not open in any pdf viewing programme. I assume (but don't know for sure) that this could be related to the fact that I use a Mac. I have noticed that there are a few things (especially related to external file manipulation) that don't run on Mac.

If anyone could help, I would be very grateful.


Update:
I found a GhostScript for Mac and installed it as Chris suggested. Unfortunately, that didn't work. I read on a forum that many Mac users are having problems with MATLAB plots at present, possibly related to java. There was an operating system update (to OS X 10.6.8) last week and that is when the problems began. My code worked before then.

I still have not found a solution to this and I don't think the MATLAB people have either, so if anyone has suggestions about how to save plots WITHOUT using saveas, I would love to hear them. The "print" command does not work for me either--it produces PDFs that I can't open.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷…雨湿花 2024-11-26 06:05:23

我认为这里的问题是 GhostScript 的消亡,而不是 Matlab。 Google 搜索该 GS 错误后发现许多 页面,例如 。这完全适用吗?如果您在 Matlab 之外使用 GS,它可以工作吗?

顺便说一句,您可以查看此 FEX 提交 export_fig。它对我很好。最坏的情况是你可以输出为 png 并稍后转换为 PDF。

I think the problem here is GhostScript dying and not so much Matlab. A Google for that GS error turns up many pages such as this. Is this at all applicable? If you use GS outside of Matlab does it work?

As an aside, you might take a look at this FEX submission export_fig. It has treated me well. Worst case you could output to png and convert to PDF later.

才能让你更想念 2024-11-26 06:05:23

我可以提出的一个建议是使用 OS X 可以理解的不同格式,并通过系统调用简单地将结果转换为 pdf

看看以下方法是否有效:

% Load a test image
im = imread('cameraman.tif');

imshow(im); % display the image

saveas(gcf,'test.tif','tif');

% convert to pdf using a syscall to cupsfilter
!cupsfilter test.tif > test.pdf 2> /dev/null

% open the file with your default pdf viewer 
!open test.pdf

如果上述方法不起作用,另一种方法是从图中获取位图窗口并使用 imwrite 写入它。 注意,此方法无法受益于 saveasprint 的良好字体缩放功能。此方法假设上面的 im 变量仍然存在。

imagesc(im); colormap gray;

% Set the border color to white
set(h,'Color',[1 1 1]);

% Get the image in the figure
frame = getframe(gcf);
imout = frame.cdata;

% on OS X, the stretch window image 
% appears in the bottom right corner 
% of the image.  Remove it. 
imout = imout(10:end-9,10:end-9,:);

% Write the image out to a lossless tif
imwrite(imout,'test.tif','tif','Compression','none')

然后您可以将 tifs 转换为 pdf 文件,如上所述。人物的质量取决于人物的尺寸。在大多数情况下,我不会使用第二种方法,因为 saveas 可以很好地处理字体。 使用 getframe 只是为了解决 saveas 的真正问题

One recommendation I can make is to use a different format that OS X understands and simply convert the result to pdf with a system call

See if the following works:

% Load a test image
im = imread('cameraman.tif');

imshow(im); % display the image

saveas(gcf,'test.tif','tif');

% convert to pdf using a syscall to cupsfilter
!cupsfilter test.tif > test.pdf 2> /dev/null

% open the file with your default pdf viewer 
!open test.pdf

If the above is not working, yet another approach is to get the bitmap from the figure window and write it using imwrite. Note, this method doesn't benefit from the nice font scaling features of the saveas and print. This approach assumes the im variable above still exists.

imagesc(im); colormap gray;

% Set the border color to white
set(h,'Color',[1 1 1]);

% Get the image in the figure
frame = getframe(gcf);
imout = frame.cdata;

% on OS X, the stretch window image 
% appears in the bottom right corner 
% of the image.  Remove it. 
imout = imout(10:end-9,10:end-9,:);

% Write the image out to a lossless tif
imwrite(imout,'test.tif','tif','Compression','none')

You could then convert the tifs to pdf files as above. The quality of the figure will depend on the figure size. In most cases, I wouldn't use this second approach as saveas handles the fonts nicely. Using getframe is meant only as a workaround to the true problem with saveas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文