User Tools

Site Tools


makematlabeps

Create EPS and PDF directly from MATLAB script using Makefile

It is possible to run MATLAB from the command line and have it produce eps files from an existing MATLAB script (m file).

Prerequisites

You need a MATLAB version that can be run from the command line, and its location must be in your $PATH. On Linux/Unix systems this is normally the case if MATLAB is installed. On Mac OS X, you need to add the directory

/Applications/MATLAB72/bin

to your $PATH (for MATLAB 7.2).

To verify that MATLAB is indeed accessible from the command line, enter the following in your terminal.

$ matlab -nodisplay -nojvm

If MATLAB and the $PATH are properly configured, you should be greeted with the following prompt:

Warning: 
  MATLAB is starting without a display, using internal event queue.
  You will not be able to display graphics on the screen.


                              < M A T L A B >
                   Copyright 1984-2006 The MathWorks, Inc.
                         Version 7.2.0.283 (R2006a)
                              January 27, 2006 

  To get started, type one of these: helpwin, helpdesk, or demo.
  For product information, visit www.mathworks.com.
 
>>

Doing the Job

Suppose we want to include in a LaTeX file a plot of a sinusoidal curve. One way of doing this is to start MATLAB, write a script myfig.m to plot the curve, and use the menu option “File/Save…” to save the figure as an eps file myfig.eps. If we use pdflatex, we might in addition want to convert the eps file to pdf using epstopdf.

Wouldn't it be much simpler to define a Makefile that can create myfig.eps and myfig.pdf automatically, and update them whenever myfig.m has changed? Consider the following example file myfig.m:

d = 1;
fc = 10;
fs = 1000; 

t = linspace(0, d, d*fs);
s = cos(2*pi*fc*t);

plot(t, s);
xlabel('t');
ylabel('s(t)');
title('Sinusoidal signal');

If we run this script in MATLAB (in graphical mode, not on the command line), we will get a nice figure window that we can save to an eps file as described before. As a first automatization step, we can get MATLAB to automatically save the figure to eps by adding the following line.

print(gcf, '-depsc2', 'myfig.eps');

This tells MATLAB to write the current figure to a file called myfig.eps in the eps format. (See the MATLAB help on print for the exact syntax.) If the size or dimensions of the figure in the eps file is not what you want, you can set the PaperPosition property of the figure. This property specifies the position and size of the figure in the file:

>> get(gcf, 'PaperPosition')

  ans = [0.2500 0.2500 8.0000 6.0000]

These values correspond to defaults; the figure will be 8 inches wide and 6 inches high. If you want to reduce e.g. the height, use the command

set(gcf, 'PaperPosition', [0.25 0.25 8 3]);

to change the height from 6 to 3 inches.

Now what we want is to run this script from the command line. There are two ways to do this. The first is pass the script to MATLAB through STDIN, as follows

$ matlab -nodisplay -nojvm < myfig.m

This will start MATLAB and then perform as if we had entered the commands in myfig.m one by one. Upon encountering the EOF (end-of-file) character in myfig.m, MATLAB will automatically exit.

The second method is using the -r parameter of the command line version of MATLAB, which tells it to execute the given commands. For our example:

$ matlab -nodisplay -nojvm -r 'myfig; exit'

The exit command is necessary because otherwise MATLAB would wait for input after executing myfig. Perhaps the advantage of the first method is that even if an error occurred in the script, MATLAB would eventually terminate once it encounters the end of the file. With the second method, an error in your script would cause MATLAB to abort and never execute the exit command.

Update: It appears that in MATLAB version 7.4 you need to put the following line at the end of your script, otherwise MATLAB won't terminate after execution:

exit

The Makefile

The following Makefile puts everything discussed above together:

all: myfig.pdf

%.eps: %.m
       matlab -nodisplay -nojvm < $< > /dev/null

%.pdf: %.eps
       epstopdf $<

Here we added > /dev/null to suppress the output of MATLAB. (This however will hide potential error messages MATLAB produces.)

Remark: You could of course put a rule in your Makefile that makes your tex file dependent on myfig.pdf or myfig.eps.

Problems: While this method works fine, it would be nice if MATLAB would return with a non-zero return code to let make know that there was an error.

makematlabeps.txt · Last modified: 2008/04/04 09:38 by kleiner