1

我是 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)没有其他任何变化(除非我的记忆力很差!)。

此外,我遇到了一些关于使用“打印”的类似问题的旧建议。我尝试使用:

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

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


更新:我找到了 Mac 的 GhostScript 并按照 Chris 的建议安装了它。不幸的是,那没有用。我在一个论坛上读到,目前许多 Mac 用户在使用 MATLAB 绘图时遇到问题,可能与 java 有关。上周有一个操作系统更新(到 OS X 10.6.8),这就是问题开始的时候。我的代码在那之前工作过。

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

4

2 回答 2

2

我认为这里的问题是 GhostScript 死了,而不是 Matlab。那个 GS 错误的谷歌出现了很多这样页面。这完全适用吗?如果您在 Matlab 之外使用 GS,它会起作用吗?

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

于 2011-07-10T23:39:08.117 回答
1

我可以提出的一个建议是使用 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. 请注意,此方法无法从 和 的字体缩放功能中saveas受益print。这种方法假设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')

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

于 2011-07-15T03:46:50.853 回答