Jonathan Dreyer




Creating Matlab Plots for Thesis

The following instructions assume you have set the LyX font to Latin Modern Roman. This can be done by going to Settings > Fonts and selecting Roman: Latin Modern Roman.

The matching system font, Computer Modern Unicode, can be downloaded from http://sourceforge.net/projects/cm-unicode/ or as a local download here. Note that although the Computer Modern Roman font in LyX looks the same as Latin Modern Roman, the later is a vector font and appears clearer in pdfs.

To install these fonts unpack the compressed file (cm-unicode-0.6.3a.zip) into the system fonts directory. On a Window system these are located at \Windows\Fonts\

Example 1

The following example script generates a png file that can be directly imported into LyX without any scaling or modification. Although it appears large in the LyX preview mode, in the generated pdf it will appear at the proper size.

%Example plotting script for generating high resolution Matlab plots
%suitable for a thesis or paper.
close all
clear all

%Parameters for saved images
ImageDPI=500;
ImageSizeX=6;
ImageSizeY=4;
ImageFontSize=9;
FileLabel='WithFormatting';
FontName='Garamond';
AxisFontName='CMU Serif';

a=[0:0.1:100];
b=0.005*(a);
c=sin(a).*exp(-a/5);
d=exp(-a/100);

figure2 = figure(2);

axes1 = axes('FontSize',ImageFontSize,'FontName',AxisFontName);
xlim(axes1,[min(a) max(a)]);
ylim(axes1,[-0.4 1]);
box(axes1,'on');
hold(axes1,'all');

h=plot(a,b,'k',a,c,'k',a,d,'--k');

set(h(1), 'LineWidth',2)
set(gca,'FontName',AxisFontName,'FontSize',ImageFontSize)
%Legend entries
%IMPORTANT NOTE
%Matlab has problems drawing the box around the legend if the font has been
%changed. The only way that I have found to get around this is to force
%blank spaces at the end of the longest legend entry. In this case
%the 'Another Signal{ }' has three blank spaces after it.
m2=legend('Signal','Total Signal','Another Signal{ }',...
'Location','NorthWest');

xlabel('Frequency (Hz)','fontsize',ImageFontSize)
ylabel('Current Noise (A/\surdHz)','fontsize',ImageFontSize)

%====================
%Set the image size and save to FileLabel.png where FileLabel is set at line 9.
set(gcf,'PaperUnits','inches','PaperPosition',[0 0 ImageSizeX ImageSizeY])
print('-dpng', strcat(FileLabel, '.png') , strcat('-r',num2str(ImageDPI)))

 

A comparison of the image generated from the default Matlab settings and the modified settings is shown below.

Original Matlab Image
Modified Matlab Image

Notes: These images are extremely high DPI; however, anything lower and I have found the images to be blurry. Yes, you could save the images as EPS files and import them into LyX; however, trying to embed the fonts into the EPS is not trivial and I have neither the time nor interest to figure it out. If you know of a way to easily do this in Matlab let me know.

Example 2

You will notice that when saving images at higher resolutions Matlab does not scale the dashed lines properly. At high DPI dashed lines become blurry and can appear as solid lines. The following example uses the scripts written by P. Kabal in the Department of Electrical & Computer Engineering at McGill University. This toolkit also adds support for variable dashed-dotted lines. The original material can be found online at: http://www-mmsp.ece.mcgill.ca/Documents/Reports/index.html. I had to modify two functions in this toolkit, the modified version is available here. If you would prefer the original Matlab code it can be downloaded here and the documentation for the m-files is available here (from the McGill web site).

Unpack the MatlabPlot.zip (from this site) or Matlab-Plot-v1r3.tar.gz (from the McGill site) and add it to your Matlab path using the Path Tool GUI. This can be accessed by typing 'pathtool' at the Matlab command window.

The following example uses these functions to create a plot with a variety of dashed and dotted lines that properly scale with increasing image DPI. Additional examples can be found in the Matlab-Plot-v1r3 documentation.

%Example plotting script for generating high resolution Matlab plots
%suitable for a thesis or paper.
close all
clear all

%Parameters for saved images
ImageDPI=500;
ImageSizeX=6;
ImageSizeY=6;
ImageFontSize=9;
FileLabel='MatlabPlotv1r3';
FontName='Garamond';
AxisFontName='CMU Serif';

%===============
%Generate data to plot
t=linspace(-pi, pi, 201);
signal(1,:)=sin(t);
signal(2,:)=sin(t-1);
signal(3,:)=sin(t-2);
signal(4,:)=sin(t-3);
signal(5,:)=sin(t-4);
signal(6,:)=sin(t-5);
%===============

figure2=figure(2);
%Make dash/dot line styles
Pattern1{1} = []; % Line
Pattern1{2} = [5 -4]; % 5 unit dash, 4 unit blank
Pattern1{3} = [2 -2]; % 2 unit dash, 2 unit blank
Pattern1{4} = [5 -2 1 -2]; % 5 unit dash, 2 unit blank, 1 unit dash, 2 unit blank
Pattern1{5} = [5 -2 1 -2 1 -2]; % 5 unit dash, 2 unit blank, 1 unit dash, 2 unit blank, 1 unit dash, 2 unit blank
Pattern1{6} = [5 -2 2 -2]; % 5 unit dash, 2 unit blank, 2 unit dash, 2 unit blank

%Set the axis limits prior to calling DashLine
axes1=axes('Parent',figure2,...
'Position',[0.131785714285714 0.363963963963964 0.775 0.586800931366149],...
'FontSize',ImageFontSize,...
'FontName',AxisFontName);

axis([min(t) max(t) -1 1]);
axdata=axis;

%Combine data for plot into two arrays
%Number of data sets in the plot
Ngraph=6;
for (i=1:Ngraph)
X1(:,i)=t;
end
Y1(:,1)=signal(1,:);
Y1(:,2)=signal(2,:);
Y1(:,3)=signal(3,:);
Y1(:,4)=signal(4,:);
Y1(:,5)=signal(5,:);
Y1(:,6)=signal(6,:);

%Create the dashed lines
for i=1:Ngraph
[X1d{i} Y1d{i}] = DashLine(X1(:,i), Y1(:,i), Pattern1{i});
end

%Plot the data
h=plot(X1d{1},Y1d{1},'k',...
X1d{2},Y1d{2},'k',...
X1d{3},Y1d{3},'k',...
X1d{4},Y1d{4},'k',...
X1d{5},Y1d{5},'k',...
X1d{6},Y1d{6},'k');
axis(axdata);
set(h(4), 'LineWidth',2)
set(gca,'FontName',AxisFontName,'FontSize',ImageFontSize)

%Legend entries
%IMPORTANT NOTE
%Matlab has problems drawing the box around the legend if the font has been
%changed. The only way that I have found to get around this is to force
%blank spaces at the end of the longest legend entry. In this case
%the 'Another Signal{ }' has three blank spaces after it.
m1=legend('Signal','Total Signal','Another Signal',...
'Noise', 'More Noise', 'Unknown Noise{ }');
set(m1,'FontName',FontName,'FontSize',ImageFontSize, 'Location','SouthOutside',...
'Position',[0.326807142857143 0.0216096096096096 0.383885714285714 0.246870870870871])
SetLegendProp(m1, 'Pattern', Pattern1);

%Set Axis Font and Size
xlabel('Frequency (Hz)','fontsize',ImageFontSize)
ylabel('Current Noise (A/\surdHz)','fontsize',ImageFontSize)
xlhand=get(gca,'xlabel');
ylhand=get(gca,'ylabel');
set(xlhand,'FontName',FontName);
set(ylhand,'FontName',FontName);

%====================
%Set the image size and save to [FileLabel].png where FileLabel is set at line 11.
set(gcf,'PaperUnits','inches','PaperPosition',[0 0 ImageSizeX ImageSizeY])
print('-dpng', strcat(FileLabel, '.png') , strcat('-r',num2str(ImageDPI)))

Output Image

 

 

 

(c) jdreyer 2012