MATLAB Answers

Spikes when saving matlab figure as pdf

2 views (last 30 days)
Sies Depoorter
Sies Depoorter on 13 Feb 2018
Answered: Ted Shultz on 2 Nov 2018
Hello
When I plot my figure to a pdf-file, then I get spikes on the figures in the pdf-file which are not present in the original matlab figure. Does anyone know how to get rid of these spikes?
Below you can see the original matlab figure followed by the pdf-file. The spikes are best visible in the right top.
For saving the figure as pdf-file I used the code below:
h=gcf;
set(h,'PaperOrientation','landscape');
set(h,'PaperPosition', [0 0 plotheight plotwidth]);
print(gcf,'-dpdf',title));
  2 Comments
Sies Depoorter
Sies Depoorter on 13 Feb 2018
I used plot(), and position the figures on the pdf-file with axes().
The data I use are the samples of a deformed sine wave and plot the FFT.
The matlab figure is the correct plot.

Sign in to comment.

Answers (1)

Ted Shultz
Ted Shultz on 2 Nov 2018
This is a MATLAB bug, and has been happening for a few years. It drives me crazy. For some discussion on this, and a workaround, see: https://github.com/altmany/export_fig/issues/6.
More or less, you save your figure as an eps (print -epsc2 filename.ep) and then run this function on it to fix it:
function fixeps(inname,outname,fixmode)
if nargin==2
fixmode = 'LJ';
end
fi = fopen(inname,'r');
fo = fopen(outname,'w');
tline = fgets(fi);
while ischar(tline)
if (strcmp(tline,['10.0 ML' 10])) % Replace 10.0 miterlimit
switch (fixmode)
case 'LJ'
fwrite(fo,['1 LJ' 10]); % With round linejoin
case 'ML'
fwrite(fo,['2.5 ML' 10]); % With smaller miterlimit
end
else
fwrite(fo,tline);
end
tline = fgets(fi);
end
fclose(fo);
fclose(fi);

Community Treasure Hunt

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

Start Hunting!

Translated by