How Pstoedit, Tgif, LaTeX and A2ps can get along with MATLAB figures
Marco De la CruzHeredia
[marco@atmosp.physics.utoronto.ca]
New! Contribute MATLAB [benchmarks]!
Before we start, remember: the most important MATLAB command is
" 
>> lookfor factorial all Readme file for MATLAB. GALLERY Higham test matrices. FACTORIAL Factorial function. GAMMA Gamma function. IPJFACT Hankel matrix with factorial elements.
Now you're cooking! (^^)
I will start by making a checkerboard plot. The commands:
PeaksCPlotHandle = axes('Position',[.1 .1 .75 .8],'Visible','off'); % Make the contour plot pcolor(peaks) shading flat colormap(1  0.7*hot) set(PeaksCPlotHandle,'XTickLabel',[]) set(PeaksCPlotHandle,'YTickLabel',[]) title({'Using cells','we have two rows!'},'Rotation',0) xlabel('x') ylabel('y','Rotation',0,'FontSize',16)
will create a set of axes (called " 
clabel
"Although the shading indicates regions of high and low values it isn't very quantitative. I'll fix this by adding contours:
hold on ContourHandle = contour(peaks,'b'); clabel(ContourHandle,'FontSize',15,'Color','r','Rotation',0)

Note that I must first " 

arrow
"I shall now add an arrow and tags to indicate an interesting region :)
AChibi = arrow([44 32],[45 25],'ends','start'); arrow(AChibi,'FaceColor','g','EdgeColor','k','tipangle',10,'width',1) text(43,23,'Chibi!','Color','k','FontName','utopia','FontSize',14')
As you can see the " 

Hmph! Most fonts are mapped to Courier when the figure is translated
and imported into Tgif, but that doesn't mean we can't make jpegs directly from
MATLAB (using " 

It would be nice to see what a crosssection of the plot looks like! It's a good idea, though, to first mark where I'm making the crosssection (two, in this case):
cutx = 20; cuty = 10; peakdata = peaks; plot(cutx*ones(1,length(peakdata)),[1:length(peakdata)],'y') plot([1:length(peakdata)],cuty*ones(1,length(peakdata)),'r')
OK, now it's time to make the plot. First we have to tell MATLAB where we want the new set of axes and the size of the area they must occupy:
RightAxesHandle = axes('Position',[.855 .1 .12 .8]);
Tip: if all your plots are of the same size you
can use the command " 
[RightAxesHandle, CutXHandle, CutYHandle] = ... plotyy(peakdata(:,cutx),[1:length(peakdata)], ... peakdata(cuty,:),[1:length(peakdata)]); set(CutXHandle,'Color','y') set(CutYHandle,'Color','r') set(RightAxesHandle(1),'YTick',[],'YTickLabel',[],'YColor','y') set(RightAxesHandle(2),'YTick',[10 30 40],'YTickLabel', ... ['a'; 'b'; 'c'],'YColor','r') set(RightAxesHandle(1),'XTick',[],'XTickLabel',[]) set(RightAxesHandle(2),'XTick',[],'XTickLabel',[]) title('\Gamma_0')

The " 

In order to obtain a full 3d view of the data I will make a third set of axes and plot a surface. By now you should know the drill! 
PeaksPlotHandle = axes('Position',[.1 .65 .25 .25],'Visible','off'); surf(peaks) shading flat set(PeaksPlotHandle,'Box','on') set(PeaksPlotHandle,'XTick',[]) set(PeaksPlotHandle,'XTickLabel',[]) set(PeaksPlotHandle,'YTick',[]) set(PeaksPlotHandle,'YTickLabel',[]) set(PeaksPlotHandle,'ZTick',[]) set(PeaksPlotHandle,'ZTickLabel',[])

Unfortunately you can't combine two colormaps in a single figure,
which is très uncool, because I wanted a surface plot with
a different colour scheme. In order to do that little
trick you need the Image Processing Toolbox and the
" 

Let's print the sucker!
print dpsc2 coolplot.psIf you don't need to do any more image processing you can skip down to Step 8 below. Otherwise, read on...

Now, you may have noticed that the arrow in the plot points inconspicuously to me! Where is cute Chibichan? Well, it turns out that my picture was made using Tgif, a really cool drawing program. Pasting the pic into the plot is one way of putting it in its place, but that would probably require making bitmap images of both the plot and Chibi Top. One would like to keep everything in a nice vector format so as not to lose resolution. One way of doing this is to convert the PS plot into something [William Cheng's] Tgif understands. In order to do this one needs two programs: Ghostscript (which usually comes with Linux), and Pstoedit, a very nifty utility that allows one to covert PS files into a variety of formats including, you guessed it, Tgif object files. For starters then, let's see what Pstoedit has to say: 

[snowflake]~/> pstoedit help pstoedit: version 3.10 (build Mar 20 1999) : Copyright (C) 1993  1999 Wolfgang Glunz usage: pstoedit [v] [help] [split] [page nn] [dt] [merge] [df fontname] [scale nn] [dis] [nomaptoisolatin1] [nq] [nb] [flat nn] [bo] [psarg string] [include file] f format [infile [outfile]] Default interpreter is /usr/bin/gs Available formats : gnuplot: gnuplot format idraw: Interviews draw format fig: .fig format for xfig xfig: .fig format for xfig tgif: Tgif .obj format (for tgif version >= 3) sample: sample driver: if you don't want to see this, uncomment the corresponding line in makefile and make again tk: tk and/or tk applet source code hpgl: HPGL code pic: PIC format for troff et.al. kil: .kil format for KIllustrator pdf: Adobe's Portable Document Format java: java applet source code dxf: CAD exchange format rpl: Real3D Programming Language Format rib: RenderMan Interface Bytestream lwo: LightWave 3D Object Format psf: Flattened PostScript (no curves) ps: PostScript debug: for test purposes dump: for test purposes (same as debug) gs: any device that GhostScript provides  use gs:format, e.g. gs:pdfwrite ps2ai: Adobe Illustrator via ps2ai.ps of GhostScript
Here is the result of running it:
[snowflake]~/> pstoedit f tgif coolplot.ps coolplot.obj pstoedit: version 3.10 (build Mar 20 1999) : Copyright (C) 1993  1999 Wolfgang Glunz Interpreter finished. Return status 0
Is the conversion perfect? Not always, but it's usually quite good. In particular I've noticed that fancy or rotated text doesn't translate well (if at all). In that case I'd suggest making all the MATLAB labels as simple as possible and let LaTeX do the fancy stuff (or if you use Wordperfect or something like that you can do quite a bit of text editing using Tgif, but it's not really the best tool). Dashed lines become solid, but again, Tgif can easily take care of that. 
One tricky thing sometimes is to get Pstoedit and Ghostscript working together well. It seems that every time one of the packages is upgraded they no longer like each other. The good news is that [Wolfgang Glunz], the author of Pstoedit, is a really cool guy, and he's very helpful when it comes to fixing the little booboos that pop up... 

OK, so now I can load coolplot.obj into Tgif! Just
type " 

After putting all together I suggest grouping the entire plot and saving it (as a Tgif object file, of course). You can now print it into an EPS file which LaTeX can read, just make sure that the menu bar at the top says LaTeX (EPS) (other formats are raw PostScript, X11 bitmap, ASCII, EPSI, PNG, HTML, PDF or the specified printer device). It is usually a good idea to select the object one wants to print and then choose PrintSelectedObjs from the File menu in order to avoid extra whitespace around the figure. IMPORTANT! Under the File menu make sure that you choose Print/ExportInColor ! Otherwise you'll get a black rectangle when you print the EPS file (this applies only to figures with colour). 

The final step now is to incorporate the figure into a LaTeX document. The following code readily does this: 

\documentclass[12pt,titlepage]{article} \usepackage{psfrag} \usepackage{graphicx} \usepackage{amsmath} \usepackage{amsfonts} \usepackage{amssymb} \begin{document} \begin{figure}[!htbp] \begin{center} \psfrag{x}[ct][][1][0]{$3 (1x)^2 e^{x^2  (y+1)^2}  10 (\frac{x}{5}  x^3  y^5) e^{x^2y^2}  \frac{1}{3} e^{(x+1)^2  y^2}$} \psfrag{Chibi!}[][][1][0]{$\mathfrak{Chibi!}$} \psfrag{y}[][][1][90]{y} \includegraphics[scale = 0.9]{ChibiTop_coolplot.eps} \end{center} \caption{The final plot! Note that the ``x'' label has been substituted for the expression of the ``peaks'' function, while ``y'' has been rotated and ``Chibi!'' is now written in a funky font...} \end{figure} \end{document}
Notice that using the " 

Of course, I can also scale the text or even the entire figure without any loss of resolution: 

\begin{figure} \begin{center} \includegraphics[scale = 0.6]{Small_Chibi.eps} \includegraphics[scale = 0.6, angle = 100]{Small_Chibi.eps} \includegraphics[scale = 0.6, angle = 220]{Small_Chibi.eps} \includegraphics[scale = 0.6, angle = 330]{Small_Chibi.eps} \includegraphics[scale = 0.6, angle = 145]{Small_Chibi.eps} \includegraphics[scale = 0.6, angle = 90]{Small_Chibi.eps} \caption{Wheeeeeeeeeeeeeeeeeeee!!!} \end{center} \end{figure}
Another program which MATLAB users may find useful is A2ps,
which converts plain ASCII text into PostScript, with the added bonus
that it can "pretty print" code which is written in certain languages.
I've written a couple of style sheets for it, but the one included in
the distribution is for MATLAB 4.X. [Below]
you will find a new style sheet for MATLAB 5.X. To use it, just copy
it into your style sheets directory and modify the appropriate entry
in " 
a2ps prologue=color Pfile coolplot.m
to get:
Below is a list of all the programs mentioned above. Except for MATLAB, they're all free! 
MATLAB
": [http://www.mathworks.com]
clabel:
": [clabel.m] (16.3 Kb)
arrow
": [arrow.m] (53.4 Kb)
Linux
": [http://www.linux.org]
Ghostscript
":
[http://www.cs.wisc.edu/~ghost/aladdin/index.html]
Pstoedit
":
[http://www.geocities.com/SiliconValley/Network/1958/pstoedit/].
Tgif
":
[http://bourbon.cs.umd.edu:8001/tgif/].
LaTeX2e and packages
":
[ftp://ftp.cdrom.com/pub/tex/ctan/CTAN.sites]
A2ps
":
[http://www.inf.enst.fr/~demaille/a2ps]
MATLAB 5.X style sheet
": [matlab5.ssh] (9.6 Kb)
MATLAB 4.X style sheet
": [matlab4.ssh] (7.0 Kb)
CTWM
": like my windows manager? You can get it from
[ftp://slhp1.epfl.ch/pub/]. It's
very cool! My personal configuration file is
[.ctwmrc] (17.2 Kb), give it a shot!
Chibi Top
": was made by me using Tgif. I'm an awful
artist, to see the really amazing stuff that can be done with Tgif check
out [Onizuka]'s
extremely cool drawings at
[http://www1.nisiq.net/~onizuka/galgaE.html]
There are other ways to get better mathematics in MATLAB plots
than what the default environment provides. A good example of
this is "stext
" by
[Douglas M. Schwarz]. It
carries MATLAB's TeX functionality one step further, allowing
one to perform some extra tricks. It's a bit of a tough call
sometimes to decide what's the better approach (do all
in MATLAB or let TeX take charge of all the text), but
choice is always good :)
I've developed a MATLAB benchmark which complements
"bench.m
". It is a much more grueling and,
hopefully, a better indicator of a machine's raw CPU
power (it doesn't test graphics). Briefly, it runs a
battery of 25 tests, including FFTs, mathematical
operations on "small" and "large" matrices, various
factorizations and eigenvalue calculations (both
full and sparse). Note that the benchmark is designed
for MATLAB 5.X.
Unfortunately it requires so much memory (256 Mb) that
it can only run on one of the machines I have access
to (SGI with dual R10Ks, takes 10 hours!) If you'd like
to contribute to this benchmark please download
[mbench.m] and email me
the results at
[marco@atmosp.physics.utoronto.ca]
(you will be credited). Aside from the resulting timings (which are stored
in the cell array "times
" if you do "times =
mbench;
", say), please send me also:
The information will be summarized on this page as follows:
A full table is also available.
Thank you in advance for any contributions!
The numbers have been added to the table. Note from Tommy:
In my version of Matlab I had to add SIGMA to the final eigs test. I used SIGMA=0.
Thanks for the submission!

Well folks, I hope that some of you find this document useful. If you have any questions, comments, suggestions and/or complaints you can email my agent at: [marco@atmosp.physics.utoronto.ca]. Visit too his way cool [Gunnm] web page! Have a good life, and always remember: you can tune a piano, but you cannot tuna fish! Bai! 

Return to the [index]