MATLAB function to save plots as pdf
figtopdf.m |
Download the above file (OR) save the following MATLAB code as "figtopdf.m"
% Function to Export the MATLAB figure to PDF
function figtopdf = figtopdf(savepath,fig_number)
% Transparent background
set(gcf, 'Color', 'white');
% crop the figure
fig=figure(fig_number);
set(fig,'Units','Inches');
pos = get(fig,'Position');
set(fig,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
print(fig,savepath,'-dpdf','-r600')
end
% Function to Export the MATLAB figure to PDF
function figtopdf = figtopdf(savepath,fig_number)
% Transparent background
set(gcf, 'Color', 'white');
% crop the figure
fig=figure(fig_number);
set(fig,'Units','Inches');
pos = get(fig,'Position');
set(fig,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
print(fig,savepath,'-dpdf','-r600')
end
How to use it?
Change the size of the figure window to the desired size and execute the following.
savepath=['C:\User\File_location\File_name']; % Type the directory path of the pdf file "File_name"
savepath(strfind(savepath,'\'))='/';
fig_number = 1; % Figure window number
figtopdf(savepath,fig_number)
savepath=['C:\User\File_location\File_name']; % Type the directory path of the pdf file "File_name"
savepath(strfind(savepath,'\'))='/';
fig_number = 1; % Figure window number
figtopdf(savepath,fig_number)