matlab2tikz

This is the web page of matlab2tikz, a MATLAB script to convert MATLAB into TikZ figures for easy and consistent inclusion into LaTeX.

As of now, matlab2tikz does not implement the conversion of all possible MATLAB figures. In particular, 3D plots are not yet supported; however, with the advent of Pgfplot 1.3, this goal came into reach. If you have a nice 2D plot, matlab2tikz might just do what you want it to. Among the supported features right now are:

  • most axes properties (including logarithmic scaling),
  • grids,
  • line plots (including markers line styles),
  • contour(f) plots,
  • bar plots (stacked, grouped, histgrams),
  • error bars,
  • stem plots,
  • stairs plots,
  • quiver (arrow) plots,
  • colorbars,
  • legends,
  • images,
  • subplots (with exact alignments).
  • zplane plots
  • frequency response plots

Moreover, there is basic support for

  • rose plots,
  • compass plots,
  • polar plots.

The workflow is as follows.

    1. Place the matlab2tikz script in a directory where MATLAB can find it. (the current directory, for example).
    2. Make sure that your LaTeX installation includes recent versions of the packages TikZ and pgfplots.
  1. Generate your plot in MATLAB.
    • Invoke matlab2tikz by
      >> matlab2tikz( "myfile.tikz" );
    • As of version 0.0.4, matlab2tikz supports several options given on the command line. For example
      >> matlab2tikz( "myfile.tikz", "height", "4cm", "width", "62mm" );

      sets the (LaTeX) width and height of the picture. This can also be used to set the dimension from within LaTeX; simply set

      >> matlab2tikz( "myfile.tikz", "height", "\fheight", "width", "\fwidth" );

      and then include the file into LaTeX by

      \newlength\fheight \newlength\fwidth \setlength\fheight{4cm} \setlength\fwidth{6cm} \input{myfile.tikz}
  2. Add the contents of myfile.tikz
    into your LaTeX source code; a convenient way of doing so is to use \input{/path/to/myfile.tikz}. Also make sure that at the header of your document the packages TikZ and pgfplots are included:
    \documentclass{...} [...] \usepackage{tikz} \usepackage{pgfplots} % recommended as of Pgfplots 1.3: \pgfplotsset{compat=newest} [...] \begin{document} [...]

If you experience bugs or have feature requests, please do not hesitate to contact the maintainer, or visit the matlab2tikz forum. You can follow the development at GitHub interface and check out the latest and greatest yourself via
$ git clone git@github.com:nschloe/matlab2tikz.git

Comments

nico, thanks for the nice script, now i can finally have really nice graphs in my documents. works like a charm if one does not forget to add \usepackage{pgfplots} to the preamble :)

Hi.

Great script. Thank you very much. Now, even matlab-created graphics look nice in my latex-documents :)

Hi, would you state the checkout URL of the SVN repository? I can’t tell from the WebSVN what the URL is.

UPDATE: Direct subversion access can be gained through svn://macos.cmi.ua.ac.be:/home/wins/toegw/nschloe/svnrepos/matlab2tikz hi tobias, i'm sorry say that our university's svn server is not directly accessible from outside the university, hence publishing the url would prolly be pretty useless. if you like to check out the latest revision, just poke me (nico.schloemer@ua.ac.be) and i'll send you a bleeding edge copy. hmm, i might consider moving the stuff to a more public place. anyone got experience with sourcefourge, gna, or similar? cheers, nico

Hi, would you state the checkout URL of the SVN repository? I can’t tell from the WebSVN what the URL is.

Great script. Thank you very much. Now, even matlab-created graphics look nice in my latex-documents :)

xrapid

Nice!

thanks for sharing

Hy Nico!

I am trying to Latex a Matlab's figure but I does not seem to work. matLab's output is a map (see M_Map Matlab Package). It is NOT a 3D image but despite this it does not work.
I have been using your package before (and it's nice!) but it does not work with M:Map figures.

I am doing something wrong or your package cannot swallow these kind of outputs?

Please mail me
Thanks

Kind regards
Marco

Thank you for this wonderful tool. It works great and I love what I get in my document. I have one little problem that I couldn't resolve even after consulting pgfplots manual.

Y axis ticks in one of my plots is {0,0.05, 0.1, 0.15, 0.2, 0.25}. All these numbers are rendered as written in the list, except for 0.05 which is rendered 5 . 10^{-2}.

I tried /disabledatascaling but it didn't work (I am not sure they way I have used it is correct way). I appreciate if you can give me a hint as how to fix this problem.

Best regards,
Hooman

hey hooman,

could you send me a matlab figure which displays the problem? i'll try to fix it, then.

cheers,
nico

xticklabel style={
/pgf/number format/fixed,
/pgf/number format/fixed zerofill}

I would strongly suggest that you remove 'clear all' from this function. I just wiped out all my work.

In fact, may I suggest that you refrain from using global variables at all. Instead, look into nested functions.

Removing clear all seems indeed a good idea.
The concept of global variables is something that I don't like so much either, and nested function would be a computationally clean solution for this. At the same time, I'm also aiming for readability of the code which might not automatically be given when nesting function properly.
Anyway, I put all this on the todo list.

Hi,

very nice tool. Now I can produce really nice plots from Matlab within Latex documents. But one question remains: How can I include the generated tikz file into the pictureenvironment in Latex?

Best wishes
Tobias

Hi Tobias,

I guess I don't understand that pretty well -- the pictureenvironment? When you just wanna include the end product of matlab2tikz into your document, you'd just go like

\input{myfile.tikz}

In case you have already a TikZ picture environment open, I guess you'd have to copy from myfile.tikz and paste into your file.

Hopefully you can fix the following at some point:

??? Error using ==> matlab2tikz>drawErrorBars at 1864
Upper and lower error deviations not equal (0.0242993 ~= 0.0532507); matlab2tikz can't deal with
that yet. Using upper deviations.

Is there any way to make the grid lines not being "on top" the actual plot?

I've put up an exmaple here: (removed link)

In the areas where the graph crosses/intersects the grid, it is evident that the grid is "on top", but I'd prefer it to be the other way around.

Hope someone can help :)

Best regards,

drgz

Yes, that bugged be too. I guess there are some reasons to do it like that but removing "axis on top," from the resulting *.tikz file works as expected.

@Nico: Thank you a lot, that script saved me a lot of time and grey hair ;)

Hi,
unfortunately there is no support for rectangle-handles.
Is it possible to use this script anyway?
Greetz

When either the xaxisposition is 'top' or yaxisposition is 'right' the lines representing the plot box end with small arrows. Using plotyy illustrates the effect.

I've not studied tikz much, but it looks to me like this is a *feature* of the commands ...

  • axis x line=top
  • axis y line=right

Is there a way to change the line/arrow style so that there is no arrow head?

p.s. I'd post an example, but the sites that allow that sort of thing are blocked by my company's "parental control" software :-(

Many thanks for this really useful package! A really nice job!
I'm a PhD student in energetics and this piece of work is going to help me much :-).

However, I'm a bit disappointed with the current version ;-) : Every graphs I use are scatters (X,Y marker plots with size and color variations according to 2 series of data)... Be sure that as soon as your package manages to convert them in TikZ, I will incorporate them!

Thanks again!

thank you for this post.
dizi izle Submitted by Anonymous (not verified) on Wed, 08/07/2009.

I would strongly suggest that you remove 'clear all' from this function. I just wiped out all my work.

In fact, may I suggest that you refrain from using global variables at all. Instead, look into nested functions.

Hi Hooman,

I had the exact same issue and solve it by including the following argument in the matlab2tikz call:

'extraAxisOptions', 'tick label style={/pgf/number format/fixed}'

Hope it helps.

Best,
Mat

Great script. Thank you very much. Now, even matlab-created graphics look nice in my latex-documents :)

I'm a bit disappointed with the current version ;-) : Every graphs I use are scatters (X,Y marker plots with size and color variations according to 2 series of data).health questions

I have the same problem.

A great script! A real relief for someone who uses Matlab to produce figures for LaTeX!

I have one problem though. Matlab2tikz seems to ignore text(x,y,...) entries in my graphs.
Is it (planned to be) implemented?

Use

axis x line*=top
axis y line*=right

to make axis lines without those pesky arrowheads

I'm a bit disappointed with the current version ;-) : Every graphs I use are scatters (X,Y marker plots with size and color variations according to 2 series of data

Hi,

Amazing tool !!! Thank you very much.

I have an annoying problem though. I have coordinates imported from as matlab serial dates, which I then re-label using xtick. Works fine except when pgf plots the graph there is an annoying x10^5 in the bottom right corner (which must originate from the matlab serial date values.)

i.e., the code I have is :

\begin{tikzpicture}

\begin{axis}[%
view={0}{90},
scale only axis,
width=\figurewidth,
height=\figureheight,
xmin=724642, xmax=733408,
ymin=0, ymax=3.5,
xtick={724642,725373,726103,726834,727564,728295,729025,729756,730486,731217,731947,732678,733408},
xticklabels={84,86,88,90,92,94,96,98,00,02,04,06,08},

Could somebody please help me with this?

Best regards
Paul

Hi all,

first I'd like to thank you, Nico, for this great script. However, I discovered a bug...

Consider a simple plot with just one point, plotted as marker
plot(0.1, 0.1, 'x')

Then the tikz file does not have any coordinate written into, it only gets the axis definition from matlab2tikz('test.tikz'):
\begin{tikzpicture}
\begin{axis}[%
view={0}{90},
scale only axis,
width=3.07417in,
height=1.793in,
xmin=-1, xmax=1.5,
ymin=-1, ymax=1.5,
axis on top]
\end{axis}
\end{tikzpicture}

I nailed that down to the function "segmentVisible" where the output has always n-1 = 0 size, with n as the number of data points. Unfortunately I could not find a solution for handling just one point, and maybe you get that fixed faster.

Thanks and best regards

Sebastian

Hi Sebastian,

thanks for spotting this! I created an issue at https://github.com/nschloe/matlab2tikz/issues/4 to keep track of it. I'll try to fix it soonish.

Cheers,
Nico

Thanks, that was really quick :-)

Thank you very much for this wonderful script. I had an issue with annotation arrow, though. It is really really big in the latex output.
May be you could fix that in future!

Submitted by amorua (not verified) on Mon, 05/04/2010.

A great script! A real relief for someone who uses Matlab to produce figures for LaTeX!

I have one problem though. Matlab2tikz seems to ignore text(x,y,...) entries in my graphs.
Is it (planned to be) implemented?

Hm not at the moment. If you'd really find it useful, I'd be happy to introduce you to the code to tell you what would need to be done to get it in. Ha! :)

For some reason when the y axis is placed on the right the ylabel is on the left... any idea on how to correct this?

@Craig: to avoid the arrow head when you use the the command
axis y line=right use the command y axis line style={-}

@Craig: to avoid the arrow head when you use the the command
axis y line=right use the command y axis line style={-}

A plot like this one

Z = peaks;
[C,h] = contour(Z,10);
clabel(C,h)

loses its descriptive text when converting. Why? What can I do?

/Lars

Well I guess matlab2tikz doesn't really support that yet. If there's an easy way in pgfplots to handle this kind of things, you could just edit your TikZ file. If that's indeed the case, be sure to let me know so I can include it in matlab2tikz!
Otherwise it'll be a bit difficult I'm afraid.

Hi,

I could probably manage doing that manually if I knew a way to create text alongside a curve like:

\begin{tikzpicture}
\draw (0,0) .. controls (6,1) and (9,1) ..
node[near start,sloped,above] {near start}
node {midway}
node[very near end,sloped,below] {very near end} (12,0);
\end{tikzpicture}

(code stolen from the PGF manual)

However, the code representing my contour plots from matlab uses another command for the curves, namely:

\addplot [draw=mycolor2] coordinates{ (0.0001,84.623) (4.81786,84.2105) (8.81812,83.9245) (15.0167,83.4811) (26.3159,82.6731) (52.6317,80.6091) (72.0674,78.9474) (78.9475,78.4545) (105.263,76.516) (131.579,74.3412) (139.256,73.6842) (157.895,72.3508) (163.015,71.9541) (169.174,71.4768) (184.211,70.3117) (206.329,68.4211) (210.526,68.1203) (236.842,66.2043) (263.158,64.0564) (273.659,63.1579) (289.474,62.0262) (315.79,60.0114) (317.112,59.8984) (323.215,59.3769) (340.559,57.8948) (342.105,57.784) (368.421,55.8901) (394.737,53.7685) (408.027,52.6316) (421.053,51.6992) (447.368,49.7084) (471.024,47.6977) (477.164,47.2059) (500,45.5733)};

And this "addplot" I cannot find in the PGF manual. The other command, "coordinates", combined with text I cannot find at all. I can find "coodinates" in combination with "mark = x" but that is not really text.

I have manual 2.0. Is there a manual for 2.1?

/Lars

One more question..

I succeed in converting meshed plots in 3d, command "mesh", from matlab to tikz. But when I try to convert "plot3" scatter plots, they are all projected down to z=0.

So if I plot a "plot3" separately, it becomes a normal 2d-graph. And if I plot a "plot3" together with a mesh, it will result in a two-dimensional plot in a 3d figure.

Is that a known bug?

/Lars

@Lars: matlab2tikz produces output in a format of a package that sits on *top* of TikZ actually, namely Pgfplots. This makes many things a lot easier. You'll find \addplot & friends in there.

@Lars: That's certainly something that hasn't been implemented yet, although it seems like a relatively easy thing to do.
Maybe you wanna look at it yourself? :)

Maybe I was a bit unclear - i read in the "TikZ & PGF Manual for Version 2.00".

/Lars

Maybe I was unclear too :). Check out Pgfplots.

=). No I am a lazy bastard.. or incompetent..

Hi, This is a beautiful software and very useful. I was wondering if the author of this code can include an option to add a menu for matlabtikz in the figure window. It will be useful for naive people like me. matfig2pgf has this option and it is very pretty. I request the author to kindly provide this option please.

Thanks for this usefull script! It saved me lots of time writing it myself ;)

Pages

Add new comment

Addthis