Export Matlab plots for presentations and publications

Export Matlab plots for presentations and publications

When it comes to presentations and report/paper writing (either in Word/Powerpoint or in LaTeX/Beamer), the question of how to generate good looking Matlab figures is very often neglected by many students but unfortunately also by many researchers and academics. In this short post I show how I’m used to export my graphs.

To take a simple example, the following Matlab script generates a graph with two impedance curves of an electrodynamic loudspeaker for two different mechanical stiffnesses $K_{ms}$.

f1 = 10; f2 = 20e3;
freq = logspace(log10(f1),log10(f2),500);
jw = 1i*2*pi*freq;
Ze1 = 8 + jw*2e-4 + 4^2./(jw*1e-3 + 0.7 + 500./jw);
Ze2 = 8 + jw*2e-4 + 4^2./(jw*1e-3 + 0.7 + 1000./jw);

%% PLOT 1
figure;
semilogx(freq,abs(Ze1)); hold on;
semilogx(freq,abs(Ze2)); hold off;
xlim([f1 f2]);
xlabel('Frequency [Hz]');
ylabel('Impedance (abs) [ohm]');
l = legend('Kms = 500 N/m','Kms = 1000 N/m');

The most common and intuitive way of exporting the figure to an image is to go the File menu of the Figure, click to Save As and save it as a jpeg. What we get is a simple figure that is hard to view and read.

The following Matlab code shows how to export it in a more adequate way so all the axes and legends are clear and easy to read.

figure;
semilogx(freq,abs(Ze1),'linewidth',2); hold on;
semilogx(freq,abs(Ze2),'linewidth',2); hold off;
xlim([f1 f2]);
xlabel('Frequency [Hz]');
ylabel('Impedance (abs) [\Omega]');
l = legend('K_{ms} = 500 N/m','K_{ms} = 1000 N/m');

box on; grid on;
set(gcf,'Color',[1 1 1],'PaperUnits', 'centimeters', 'PaperPosition', [0 0 16 10]);
set(gca,'FontSize',12,'FontName','Helvetica');
set(l,'FontSize',12,'FontName','Helvetica');

%% save to png
saveas(gcf, 'export02.png', 'png');

The most important part is the line 10 of the previous code, in which the last two numbers 16 10 indicates an imaginary size of the paper on which the graph is printed. Modifying these numbers we can play with the total size (compared to the font size), as well as with the ratio, as shown in the following code:

set(gcf,'Color',[1 1 1],'PaperUnits', 'centimeters', 'PaperPosition', [0 0 25 10]);
saveas(gcf, 'export03.png', 'png');

For those who prefer to work with LaTeX, the following code can be used to export in the vectorial eps and, if you have LaTeX installed with the epstopdf package, to the pdf. Note, that for the last command the path of the epstopdf should be included in the Matlab PATH.

%% export to latex eps and/or pdf
saveas(gcf, 'export02.eps', 'epsc'); 
system('epstopdf export02.eps');
Comments are closed.