Skip to content

Instantly share code, notes, and snippets.

@davidwessman
Created November 16, 2015 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidwessman/5abaa5bb1f9023edbfcc to your computer and use it in GitHub Desktop.
Save davidwessman/5abaa5bb1f9023edbfcc to your computer and use it in GitHub Desktop.
Export Matlab figure as PDF
% This needs to be set to be able to print
fig = figure;
% If you want multiple plots in the figure
hold on;
I = 0:1:9;
blue = [0, 0.048, 0.143, 0.129, 0.236, 0.333, 0.479, 0.512, 0.582, 0.678];
red = [0, 0.095, 0.106, 0.171, 0.156, 0.227, 0.270, 0.300, 0.366, 0.384];
plot(I, blue,'+b')
plot(I, red,'xr')
xlabel('strˆm / A');
ylabel('skillnad i vÂgtal / cm^{-1}');
grid on;
%=============================PRINT========================================
% This gives the current path without any file format at the end
fullpath = mfilename('fullpath');
% This adds '_plot.pdf' on the path.
savepath = strcat(fullpath, '_plot.pdf');
% This prints the current plot (when hold is on, everything will be on it)
ti = get(gca,'TightInset');
set(gca,'Position',[ti(1) ti(2) 1-ti(3)-ti(1) 1-ti(4)-ti(2)]);
set(gca,'units','centimeters')
pos = get(gca,'Position');
ti = get(gca,'TightInset');
set(gcf, 'PaperUnits','centimeters');
set(gcf, 'PaperSize', [pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperPosition',[0 0 pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
print(savepath,'-dpdf')
% If you change the filename, or moves the file, it will print your file in
% the same folder as the .m file, with the same name + _plot.pdf
%==========================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment