Include a MATLAB Plot in a LaTeX Figure

Plots created in an external tool such a MATLAB can be easily incorporated in a LaTeX document. The basic steps are

  1. Create the plot in MATLAB
  2. Export the plot to an EPS file
  3. Use \includegraphics to insert the plot into your LaTeX document

An alternative approach is to use the matlab2tikz tool by Nico Schlömer.

Create a Plot in MATLAB

The details of creating a plot in MATLAB will not be addressed here. Instead, we illustrate the process with a simple plot of sin(x)

x = linspace(0,2*pi);
y = sin(x);
plot(x,y,'o');   xlabel('x');   ylabel('sin(x)')

Export the plot to an EPS file

Use the MATLAB print command to create an EPS version of the plot in the current directory

print -depsc myfig.eps

Note that if you have multiple figure windows open, the "print" command is applied to the top-most plot. Also note that the -depsc option produces a color EPS file, whereas -deps option produces a black and white EPS file.

Insert the plot into your LaTeX document

There is more than one way to do it. I like the \includegraphics command from the graphicx package. Here is a short LaTeX source file that shows how it is done

\documentclass{article}
\usepackage{graphicx}    %  in the preamble

\begin{document}

\begin{center}
   \includegraphics[scale=0.6]{myfig.eps}
\end{center}
\end{document}

The plot appears centered on the page and scaled at 60 percent of its original size. Because the plot is stored as EPS, there is no loss in quality. If the plot is scaled too drastically the axis font and plot symbols may become hard to read.

Insert the plot into LaTeX (floating) figure

LaTeX floats are automatically placed at the top or bottom of nearby pages. (Finer control over float placement is possible, but will not be described here.) While we're at it, let's define the caption and a tag for the figure. The caption will appear below the plot and will automatically be numbered.

\documentclass{article}
\usepackage{graphicx}    %  in the preamble

\begin{document}

\begin{figure}
  \centerline{\includegraphics[scale=0.6]{myfig.eps}
  \label{fig:theFig}
  \caption{My plot of $\sin(x)$.}
\end{figure}

\end{document}

The \label command creates a tag that can then be used in the text of the document like this:

Figure~\ref{fig:theFig} shows the sine function ...

Storing EPS files in subdirectories

If you have a document with many figures, it is sometimes an advantage to store them in a subdirectory of the directory that holds the .tex source. In that case, the \graphicspath command of the graphicx package is useful. Suppose then that the EPS files are stored in the "eps" subdirectory. In the preamable add \graphicspath{{./eps/}} then

\includegraphics[scale=0.6]{myfig.eps}

will insert myfig.eps if it stored in the current directory (same as the source .tex file) or if myfig.eps is stored in the eps subdirectory. Multiple graphics diirectories can be specified. For example \graphicspath{{./eps/}{../../archive/eps/}}