4.625

4.6 | 19 ratings Rate this file 296 Downloads (last 30 days) File Size: 54.29 KB File ID: #22022
image thumbnail

matlab2tikz

by Nico Schlömer

 

07 Nov 2008 (Updated 22 Nov 2011)

A script to convert MATLAB/Octave into TikZ figures for easy and consistent inclusion into LaTeX.

| Watch this File

File Information
Description

matlab2tikz supports the conversion of many MATLAB figures, including 3D plots. In particular, it supports a superset of plots compared to its ancestor matfig2pgf.

Among the supported features are:
    * most axes properties (including logarithmic scaling),
    * grids,
    * line plots (including markers line styles),
    * contour(f) plots,
    * bar plots (stacked, grouped, histograms),
    * stem plots,
    * stairs plots,
    * quiver (arrow) plots,
    * colorbars,
    * legends,
    * images,
    * subplots,
    * zplane plots,
    * frequency response plots,
    * scatter plots,
    * surf 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. Generate your plot in MATLAB.
   3. Invoke matlab2tikz by
              >> matlab2tikz( 'myfile.tikz' );

The resulting file can be included into any LaTeX document (by \input{myfile.tikz}). Don't forget to add

  \usepackage{tikz,pgfplots}

and optionally (as of Pgfplots 1.3)

  \pgfplotsset{compat=newest}
  \pgfplotsset{plot coordinates/math parser=false}

to the preamble of your LaTeX document.

To specify the dimension of the plot from within the LaTeX document, try

    >> matlab2tikz( 'myfile.tikz', 'height', '\figureheight', 'width', '\figurewidth' );

and in the LaTeX source

    \newlength\figureheight
    \newlength\figurewidth
    \setlength\figureheight{4cm}
    \setlength\figurewidth{6cm}
    \input{myfile.tikz}

If you would like to see more information, follow the development, submit a bug, or contribute in any other way, please visit matlab2tikz' web page at http://github.com/nicki/matlab2tikz .

There's also a matlab2tikz forum at http://win.ua.ac.be/~nschloe/forum/12/.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
Matfig2PGF

MATLAB release MATLAB 7.13 (2011b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (34)
10 Nov 2008 Jody Klymak

This would be much better if you explained that one needs to upload pgf and pgfplots latex packages and call \usepackage{pgfplots} in the preamble of your LaTeX document. After that it seems to work alright.

10 Mar 2009 David Haberthür

I've used this submission extensively to add plots to my LaTeX-Files and it works like intended. Thanks for that submission!

29 Jan 2010 Jean-Baptiste Carré

A really useful package. I hope it will support every plot types soon (particularly the scatter type ;-))!

09 Apr 2010 Craig

Excellent Tool, plots come out looking great!

02 Jun 2010 dm  
18 Jul 2010 leo nidas  
23 Jul 2010 Paul

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

20 Sep 2010 Ngo Huy

Hi everybody ! My name Ngo Huy . i am studient four year of Ho Chi Minh University of industry . I have a project graduated processor image (Product Classification in (closed chain stoppers)
help me . i use PLC s7200 , matlab 7.1

07 Oct 2010 Mukhtar Ullah

Wondeful tool. Today, I tried it and it could be no better than that. I faced that same problem ( that annoying x10^5), as mentioned by Paul, but that has nothing to do with malab2tikz.m but the way pgfplot processes the tick labels. I found a solution, though, which I would like to share and request Nico to include it in the readme file.
Add this to your latex document preamble
\pgfplotsset{x tick label style={/pgf/number format/fixed}}
\pgfplotsset{y tick label style={/pgf/number format/fixed}}
Of course, you do not need that if you are happy with the x10^5 notation.

29 Nov 2010 Igor

Works great!
Thanks!

14 Dec 2010 dm  
22 Dec 2010 Alexander Klein

Hi everybody!

first of all: Thank you Mr. Schlömer for matlab2tikz. This is a great piece of a software.

Unfortunately, I have had the following problems when using version 0.0.7 in Matlab 2009b on a 64bit Linux system:
1. Labels and titles could not be written by sprintf, since it where cells.
2. Barplots did not work, since there was a typo (e.g. m2t was missing)
3. I need to plot time signals with a very small difference. That is why the precision in the pgfplots coordinates were not sufficient.
4. When plotting data series with several thousand data points, it took minutes to write the file (due to strcat and a lopp for i=1:length(xData)

I have made changes to the file which takes care of this issues:
1. e.g:

  if iscell(title)
      title = title{:};
  end

2. for me replacing
barplotTotalNumber = barplotTotalNumber + 1;
with
m2t.barplotTotalNumber = m2t.barplotTotalNumber + 1;
helped
3. I added an option for the script where you can add a precision of the format string that is used when writing to the pgf file
4. I replaced the for loop with a repmat construction that reduced computation time a lot.

If anybody has encounters the same issues, I can send this file to Mr. Schlömer maybe (if he likes to add the changes). Or is there a better way to take care of that?

Kind regards

Alexander

27 Dec 2010 Petter

Looks good, but I am missing the functionality to keep the aspect ratio and change the width. E.g., \includegraphics[width=3cm]{image}

The help does not cover the optional arguments.

27 Dec 2010 Petter

Seems it was already supported, but not documented.

28 Dec 2010 Petter

Spaces in xlabel doesn't seem to work

29 Dec 2010 dm

@Petter: You have an example-code for which the spaces in xlabel doesn't work? In all my figures it works perfectly, so I assume it's something with your code.

Otherwise you don't use \includegraphics with TikZ figures (unless you pre-compile to PDF or something), but using the width/height commands already available will set the dimensions of the figure (these commands are also documented in the description here on FEX).

D.

02 Jan 2011 Petter

It was because matlab2tikz wrapped my x and ylabels with $..$. I removed that piece of code and got my expected results.

02 Feb 2011 Matt Bauman  
19 May 2011 Nicholaus Halecky

Thank you, Nico, the script is a work of art. I am an experienced LaTeX user (what I used to write my dissertation), and very comfortable in MatLab (minus any major plot generation). Thus, when I found your code, you can imagine how thrilled I was as I am building scripts for report generation at this time. This would be, easily, the best script I have ever encountered on this file exchange if I could find how to:
1) Remove the 10^5 in the x-axis label (I also use datetick and have added the suggestion by Mukhtar to the LaTeX preamble to no avail).
2) Predefine line width, image aspect ratio, and some sort of a scaling parameter, so that the output width and height parameters in the tikz, e.g.,

width=0.90\textwidth,
height=0.35\textheight

3) Some sort of understanding of how I can specify whether or not the title, axis labels, legends not be interpreted in LaTeX math mode (or do you suggest that I hard code this)?

Perhaps such features are supported (as mentioned by Petter), however a quick browse through the m file didn't give me much of a starting point.

@Alexander Klein, I am interested to know if your improvements were ever implemented by the author?

Thanks all for everything,
Cheers!

19 May 2011 Nico Schlömer

Well, the documentation may not be up-to-date all the time.
In general it's always worthwhile to take a peek at https://github.com/nicki/matlab2tikz/.
All the options are also listed in the source file itself of course, so you can always look at lines 125 ff. Should be pretty self-explainatory.

1) Not sure I understand what you mean.
2) You mean the image width? There are 'width' and 'height' arguments.
3) Option 'mathmode'.

Generally, if you found a bug or similar, it's easier for me to follow it up on GitHub.

07 Jun 2011 Nico Schlömer

@Alexander Klein: I implemented one or two things, you may wanna check out the latest release.
For future changes, it's best to submit a bug report at https://github.com/nschloe/matlab2tikz/.

17 Jun 2011 Mukhtar Ullah

I have emailed you my modification to the nested function drawText in order to deal with problems in text objects created using Matlab TEXT command. If you agree with that, you could update your code accordingly and save me from modifying everytime there is a new version. Thanks.
Mukhtar

02 Sep 2011 Michael Melzer  
02 Sep 2011 Michael Melzer

Hi Nico,
it might be better to use filename an path of uiputfile (line 206, 212, 218, ...), so files can be stored elsewhere than the current folder.

Thank you.

08 Sep 2011 Igor

Hi Nico,
Thanks again for the great work.

Few requests:
1. Is it possible to have a command line switch to put all Matlab legend entries just in {}, instead of $$ + \text{}? My legends are mostly non-formula, so I can format them much easier if they come through matlab2tikz in this shape.

2. I propose to put "defining custom colors" section before \begin{tikzpicture} because if you make legends in the fig. caption (by using \label{} and \ref{}), LaTeX will report an error without this, since \definecolor is only scoped to tikzpicture.

3. Related to 2: Any chance of automatic labeling of plots? For example \label{filename-p1}, \label{filename-p2},....

Thanks,
Igor

03 Oct 2011 THB  
14 Nov 2011 Francesco Montorsi

very nice script!! I've been using MATLAB EPS export and then editing with Inkscape and saving as PDF+LaTex solution so far, but this one is simpler and better as it solves in an elegant way the label positioning problems of my former solution!
Thanks!

29 Nov 2011 Sebastien Paris

Sounds great in a first try.

One thing, if my xlabel is xlabel('# K') in matlab, the produced file is buggy (manually changed xlabel={# K} to xlabel={\# K}) in the tikz file).

How to keep the aspect ratio for legend ?

29 Nov 2011 Nico Schlömer

Hi Sebastien,

the #-issue should be fixed now; thanks for the report.
At the moment, I don't think pgfplots has a way of controlling the legend box aspect ratio in any way, but you may want to skim through its manual. Let me know in case you find something!

27 Dec 2011 Eike

Three things:
1.: Awesome piece of code. Really saves a lot of time and work.
2.: I set the width an the hight via "\pgfplotset{}" in my preamble and just change it for a few plots. Matlab2tikz always outputs width and heigh parameters which in this case isn't very usefull.
(just found the Wiki which provides a good work around for that https://github.com/nschloe/matlab2tikz/wiki - "\newlength and \setlength")
3.: I added support vor the "Legend.Orientation" parameter as well as for "Loaction=SouthOutside" and "Location=NorthOutside", are you interested in the code? Where to should I send it?

27 Dec 2011 Nico Schlömer

There's always interest in patches to matlab2tikz! You could send a pull request on GitHub, or send the patch through by mail (use git format-patch to get properly credited) or create a pull request on GitHub. I don't mind either way.

16 Jan 2012 Tim Sønderby

Can you make so that the size of the figure always is the same, that would make it easier to control it for consistency in a report.

Thanks - great work!

16 Jan 2012 Nico Schlömer

Tim: Easy. Just use

 >> matlab2tikz( 'myfile.tikz', 'height', '\figureheight', 'width', '\figurewidth' );

for all of your figures and set \figureheight and \figurewidth once in your LaTeX document.
See the README.

25 Jan 2012 Dimitris K

Great work! Thanks

Please login to add a comment or rating.
Updates
08 Jan 2009

updated to v0.0.2

08 Mar 2009

updated to v0.0.3

09 Jun 2009

* Added support for error bars.
* Improvements in:
    - legends,
    - images,
    - quiver plots.
* Improved options handling.
* Allow for custom file encoding.
* Numerous bug fixes.

04 Jan 2010

updated to v0.0.5

06 May 2010

updated to v0.0.6

06 May 2010

update required MATLAB version

10 Sep 2010

update to 0.0.7

31 Jan 2011

update to 0.1.0

06 Apr 2011

update to 0.1.1

02 Jun 2011

update to 0.1.2

22 Aug 2011

update to 0.1.3

22 Nov 2011

update to 0.1.4

Tag Activity for this File
Tag Applied By Date/Time
3d Cristina McIntire 07 Nov 2008 10:15:40
2d Cristina McIntire 07 Nov 2008 10:15:48
image processing Nico Schlömer 07 Nov 2008 10:16:08
data export Nico Schlömer 07 Nov 2008 10:16:08
data import Cristina McIntire 07 Nov 2008 12:12:51
data export Cristina McIntire 10 Nov 2008 10:44:18
image processing Cristina McIntire 10 Nov 2008 10:44:22
matlab2latex Yunus Yunus 21 Mar 2009 11:19:57
latex Yunus Yunus 21 Mar 2009 11:19:57
3d dip 22 Mar 2009 22:23:46
matlab2latex Yongda Chen 08 Jun 2010 15:25:22

Contact us at files@mathworks.com