Code covered by the BSD License  

Highlights from
matlabfrag

4.9

4.9 | 33 ratings Rate this file 141 Downloads (last 30 days) File Size: 732 KB File ID: #21286

matlabfrag

by

 

01 Sep 2008 (Updated )

A matlab figure to .eps and .tex file exporter, similar to LaPrint

| Watch this File

File Information
Description

matlabfrag is a function which exports a Matlab figure to .eps and .tex files for use in LaTeX/pdfLaTeX/LyX. It is inspired by LaPrint, but is intended to be more WYSIWYG, by respecting figure handles better.

Some of the advantages of matlabfrag compared to LaPrint are:
- Much more emphasis on WYSIWYG.
- Handles figures that need the OpenGL or Z-Buffer renderers.
- Text objects are sized correctly (such as with a legend).
- Tick labels are handled better.
- Reduced number of options (which could also be viewed as a disadvantage).
- A great user manual ;-)

To include the figures in LaTeX, pdfLaTeX and LyX I recommend the pstool package. More information can be found in the User Guide.

Please email any bug reports to me (see my account for more details). I'm unlikely to notice any bug reports posted in the comments.

This is version 0.6.16 of matlabfrag, uploaded 04-Apr-2010.
- Fixed a bug that occurred when a figure wasn't frontmost.

Acknowledgements

La Print inspired this file.

This file inspired Matlabfrag To Pdf, Convert Plot4 Publication, and Mat2fig Conversion From A Matrix To A Figure.

MATLAB release MATLAB 7.8 (R2009a)
Other requirements Matlab r2007a or later.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (59)
12 Aug 2014 Charlie Hogg

Does anyone have any ideas on this exponent problem?

My guess is that matlab includes the exponents as a different kind of child of the plot object than other text, which causes trouble for matlabfrag. The axis exponents are difficult to access within matlab too.

25 Jul 2014 Charlie Hogg

I have used this tool for ages and I think it's great.

I have one problem occasionally. When I try to plot a color bar which has an exponent (e.g. 10^-3), the exponent is not recognised in latex and the tag (e.g. 000) comes through.

Has anyone had this problem and found a work around? The tex file seems to be created ok, but the tag (000) in the eps file is not picked up.

My work around is to multiply the colour legend by the exponent and then add the exponent manually - not ideal.

Thanks,
Charlie

14 Jul 2014 V_oron  
10 Apr 2014 Marko Budisic

I've been using this great tool for years, so thank you for making it.

However, I've just spotted a (possible) bug. When colorbar is displayed where number of color levels is relatively small, e.g., 5, matlabfrag-generated EPS will have a smooth color interpolation between those values.

For example, an empty plot, with just a colorbar:

colormap(jet(5)); colorbar

in Matlab figure looks as expected (five discrete color levels from jet map), yet in EPS the figure has a much larger number of levels, looking more like

colormap(jet(32)); colorbar

Additionally, the end colors in EPS range from light blue to orange, instead of purplish-blue to deep red (as in Matlab).

Is there an easy fix for this?

14 Mar 2014 Ashley

Gah, I edited the code to get it to do what I want, but now the background boxes are too big, rather than too small.

14 Mar 2014 Ashley

Thanks for this, the tool produces some great looking figures!

I just have one problem, related to text with a background color. The text is replaced by key values (for psfrag) before output, so the background color is much smaller than the eventual text. Is there any way around this? Maybe padding key values with zeros so they are the same length (in characters) as the original text?

02 Mar 2014 Octopode

Hi. Nice tool, thanks.
I have trouble with multiple 3D axes, and the "OpenGL" renderer set in matlabfrag. With more than 1 axis, any other axes are ignored, while the text around it is accounted for.
See the sample :
http://s24.postimg.org/ogr9myeut/multi_axes_Open_Gl.png

There is not problem with the other renderer, all axes show up.
Any idea how to fix this ? I do need OpenGL renderer.

27 Jan 2014 Stepan Kucera

a patch for a bug - add/replace lines marked by % <<<<

VAlign = get(handle,'VerticalAlignment');
switch VAlign
case {'baseline'} % <<<<
valign = 'B'; % <<<< this case must be distinguished in accordance with psfrag rendering in latex
case {'bottom','base'} % <<<<
valign = 'b';
case {'top','cap'}
valign = 't';
case {'middle'}
valign = 'c';
otherwise
warning('matlabfrag:UnknownVertAlign',...
'Unknown text vertical alignment for "%s", defaulting to bottom',string);
valign = 'l';
end

29 Dec 2013 Charlie  
17 Sep 2013 Charlie

Hi Zebb,

Thanks very much for the quick response.

crop=preview did fix it for me. Some of the legends overshoot the figures, but I imagine this is why I should install pdfcrop. I'll summon the courage to install perl if I need to.

I was also making another mistake with my own figures which returned the same error. I was using the tex interpreter. Commands like \zeta were getting left in the labels and causing latex to return the errors.

Thanks again.

17 Sep 2013 Zebb Prime

Hi Charlie,

This sounds like a problem with pdfcrop. You should be able to fix it by changing:

crop=pdfcrop

to

crop=preview

in the latex file. Otherwise, to get pdfcrop working, you will need to make sure it is installed in Miktex (via the Package Manager), then make sure you have a working Perl installation (e.g. Activeperl).

Regards,
Zebb

16 Sep 2013 Charlie

This package looks great.

Unfortunately, I cannot get the package to run smoothly.

When I run the (very helpful) test package, I can only get the figures to appear if I run using the latex>ps route.

If I run pdflatex, only figures 7 and 8 are succesfully included in the document. The other figures have a red box which reads "An error occured processing graphic ...". The log file is long, but I can't find in it what the problem seems to be.

I've looked around the web for a couple of hours. There is an error in pstool reported here http://tex.stackexchange.com/questions/60084/pstool-pdflatex-not-getting-pstool-to-work-latex-error-file-not-found, but I have the corrected update of pstools. I've checked that I have the -shell-escape option working correctly. Also, I can get laprint to work with my pdflatex setup (although I have to alter the tex document slightly).

Does anyone have any suggestions of what I might be doing wrong?

Many thanks in advance.

I am using TexnicCenter 2.0 with Miktex 2.9, matlabfrag v0.6.16 and pstool v1.5a.

09 Jul 2013 Lishuai

I met a conflict when using \IEEEoverridecommandlockouts

Hi Zebb,

I have generated all the eps figures and .tex files from matlab. But when i import the figures to my paper, all the lables on the x-axises are shifted to the right. The reason is due to I included \IEEEoverridecommandlockouts

in the main .tex file to creat the paper. Why this conflict appear?

Best regards,
Lishuai

12 May 2013 kevin  
06 Mar 2013 Nick

Fantastic little gem of a script!

However I've had some issues with labels not being coded in the PostScript file properly.. The number key and label is in the .tex file.. But appears to be missing it's partner in the postscipt.. And hence appears as '001' etc when processed. Has any one overcome this issue?

Cheers,
Nick.

18 Feb 2013 Tobias

This is a great tool! Best way to prepare matlab figures for LaTeX. Thank you!

24 Oct 2012 Bernhard

Great work.

Only question is, why is it not naturally included in MATLAB?

05 Jul 2012 Peter

Thank you for an extremely useful tool.

To avoid warning messages, when supplying absolute paths, you might want to replace

if ~exist(['./',pathstr],'dir')

with

if ~exist(['./',pathstr],'dir') && ~exist(pathstr,'dir')

or something...

23 May 2012 Koen

Great script!

I found that a cause of axis scaling not being handled properly can be the use of axis() with either of the arguments inf or -inf. This messes up the placement of the replacement string. Replacing the values with a numerical value fixes the problem.

Hopefully someone will find this useful!
Koen

27 May 2011 Nicholaus Halecky

Hey Zebb,
I am a huge fan of LaTeX (used it to write my dissertation), and had I known of your function -- well, my life would have been a bit different. Very nice work.
Two comments:
1) The sortrows function in MatLab 2011a has been changed to perform a check on input array with ismatrix, and as such, throws an error within matlabfrag. I made a simple fix by changing the sortrows to sort as below (lines 262 to 282), however, I didn't check if this changes the sort logic:
% Sort by text size first
%[Y,I] = sortrows(cell2mat( PsfragCmds(:,4)))
[Y,I] = sort( cell2mat( PsfragCmds(:,4) ) ); %#ok<*ASGLU> Required for backward compatibility
PsfragCmds = PsfragCmds(I,:);
% Now sort by colour
[Y,I] = sortrows( cell2mat( PsfragCmds(:,5) ), [3 2 1] );
PsfragCmds = PsfragCmds(I,:);
% Now sort by font angle
[Y,I] = sort( cell2mat( PsfragCmds(:,6) ) );
PsfragCmds = PsfragCmds(I,:);
% Now sort by font weight
[Y,I] = sort( cell2mat( PsfragCmds(:,7) ) );
PsfragCmds = PsfragCmds(I,:);
% Now sort by whether it is 'fixed width'
[Y,I] = sort( cell2mat( PsfragCmds(:,8) ) );
PsfragCmds = PsfragCmds(I,:);
% Now sort by label type
[Y,I] = sort( PsfragCmds(:,9) );
PsfragCmds = PsfragCmds(I,:);
clear Y
2) How does matlabfrag handle background color in text annotations? I am trying to publish a few statistics and an equation on the plot, but haven't had much luck.

Thank you again, it's really an incredible function,
Cheers,
Nicholaus

11 Apr 2011 Benjamin  
05 Apr 2011 Frederick

Thanks for that Zebb!

04 Apr 2011 Zebb Prime

Herwig: The use of single dash or double dash with the --shell-escape option depends upon your LaTeX distribution. On my system, both variants work.

Fredrick: That is a pstool question, not a matlabfrag question. If you read the pstool documentation you'll see that you can specify \includegraphics options as options to \psfragfig, i.e.

\psfragfig[width=10cm]{...}

That said, the workflow I designed matlabfrag for is to resize the figure in matlab first, so as to prevent things like text running on top of tick labels, etc.

As for your other problem, I don't know what the problem it. Try updating your version of pstool.

04 Apr 2011 Frederick

How do you resize a figure in pdftex using the command \psfragfig{S10MeanPeakt111}? And for me I cannot specify a eps/tex combination that is not in the same directory for some reason.

28 Mar 2011 E.

In the above replace get(0,'ScreenSize') by get(gcf,'Position'). You need to have opened a figure of standard size.

03 Mar 2011 E.

Wonderful function!

However, had some problems using figures, generated with matlabfrag, as subfigures in LaTeX/pdfLaTeX. The resizing part messed up the label positions so that label ticks and labels itself overlapped. For me the trick was to resize the figures in MATLAB on beforehand. Here is howto:
First get screensize using:
get(0,'ScreenSize')
Size the latter two numbers corresponding to your scale and plot the required figures using:
figure('Position',[x y scaled1 scaled2])
where x and y equal the first two results of the screensize and the scaled1 and 2 represent the scaled values.
This did the trick for me.

17 Jan 2011 Stefan Stefan

Hi there,

matlabfrag helped a lot to write good looking papers. Now I have a problem with intersecting axes labels and label ticks. I sent you an email with the necessary files to reproduce the problem. Looking forward to hear from you.
Regards,
Stefan

04 Jan 2011 Herwig Peters

Hi, excellent tool!

I'd like to point out one thing (typo?) in the userguide.pdf, section 4.3. The userguide says you have to insert -shell-escape in order to make it work with pdflatex. But, I needed to insert --shell-escape, note the double -!

Cheers
Herwig

25 Aug 2010 Martin

Great work !!

I uploaded a small script to convert the tex and eps files into one pdf.

File ID is #28545.

22 Jun 2010 Allan Leal

This is a great work!

I am wondering how could I get the EPS file already with the <<final>> format, I mean, with the tags already converted? I don't want to work with the TEX file.

Thanks.
A.

22 Jun 2010 Allan Leal  
22 Jun 2010 Juan

I manage to solve it. Apparently my windows version of pdftex does not support the font I was using in the rest of the document.

22 Jun 2010 Juan

I am trying with a Contour plot.(pdflatex + pstool) Everything looks perfect except for the contour numbers. They don’t appear in the pdf files (actually the number tags are there in the eps). Does it happened to somebody else ?.

17 Jun 2010 Rory

I am testing this because I get ridiculously big files with LaPrint, which according to Laurin's comment above should not happen with this package. However, the eps files that this produces are still far too large (~75mb vs 6mb when I do a simple print command).
Thanks in advance, if I can get this working it will be a real lifesaver for my thesis!

26 May 2010 Zebb Prime

Hi Michael,

The way I would do it is to just set all fonts to the size corresponding to \small. Of course this won't change if you wish to change to document fontsize.

You could open the output .tex file, and edit the \matlabtextA, etc commands to use \small instead of \fontsize.

The reason I chose to use \fontsize is that the label positioning is dependent upon the chosen fontsize. This way, if it looks ok on the screen, it should look ok in the document too.

Regards,
Zebb

26 May 2010 Michael Friedlander

Great package!

Suppose that I want to have all text in final figure typeset using \small, i.e., all figure text should be slightly smaller than the default tex-document's font. Any suggestions on how to do this?

Thanks!
Michael

08 Apr 2010 Zebb Prime

Hi Benjamin,

The version checking bug should already be fixed in the latest version.

Regards,
Zebb

08 Apr 2010 Benjamin

So I decided to post a small solution.
Changes lines 48-53 from

v = version;
v = sscanf(v,'%i.%i');
v = eval( sprintf('%i.%i',v(1),v(2)) );
if v(1) < 7
error('matlabfrag:oldMatlab','Matlabfrag requires Matlab r2007a or newer to run');
end

to

v = version;
v = sscanf(v,'%i.%i');
if (v(1) < 7) || ((v(1) == 7) && (v(2) < 4))
error('matlabfrag:oldMatlab','Matlabfrag requires Matlab r2007a or newer to run');
end

08 Apr 2010 Benjamin

It's a really great script. I used it to create the plots of my masters thesis. At the moment there is a small error with Matlab 7.10 . The script needs Matlab 7.3 or newer, but in the script 7.10 is recognized as 7.10<7.3 .

24 Mar 2010 Zebb Prime

Hi all,

Please disregard my use of \include in my comments above. Please use \input instead.

Cheers,
Zebb

17 Mar 2010 Rob Campbell  
21 Feb 2010 Zebb Prime

Hi All,

Viton: That should be possible, but I don't have time to do implement anything like that at the moment.

Andrey: That should only be necessary if you aren't grouping the figures properly. They placing \begin{psfrag} \end{psfrag} around your \include and \includegraphics.

Vincent: There isn't a way to do this using the default axis labels in matlab. You could however convert the axis labels to text objects. There are several functions that do this in the FEX, one is http://www.mathworks.com/matlabcentral/fileexchange/23841-format-tick-labels-extended (disclaimer - I haven't used this file myself)

David: What version are you using? You haven't provided enough information for me to diagnose the problem. Make sure you are using the latest version, and email me an example of the problem.

Hans: I did a quick test, and it looked the same in the document as it did on the screen. The easiest way to fix this would be to manipulate the position handles of legend, and if necessary the axis.

I also want to remind everyone that the comment section isn't a very good medium for receiving support.

Cheers,
Zebb

19 Feb 2010 Hans Henrik Sievertsen

Hi,
Thanks, it is very great.
I have a problems with the location of my legend. It always seem to overlap with the xlabel when I use SouthOutside. Is there a way to avoid this?
Thanks in advance
Hans

02 Feb 2010 David

I have a lot of success with matlabfrag, but I find that the print command still resizes the legend box, it is very frustrating.

If I force the rendereer to be opengl or zbuffer the legend box is in the right place but the box lines on the legend and the figure are blurry.

Please please can somebody help me with a solution for this problem?

28 Jan 2010 vincent ssemaganda

Hi Andrey,

one way to solve your problem is by rescaling the eps plots to smaller sizes.
try
\includegraphics[scale = 'value < 1']{filename}

28 Jan 2010 vincent ssemaganda

This happens when I rescale the plots to be used in a sub plot

28 Jan 2010 vincent ssemaganda

Thanks alot! However in my tex plots axis labels seem to get closer to axes than they are in the actual matlab pics. Is there any way to space axis labels further away from axes?

26 Jan 2010 Andrey Sokolov

Is it possible to make matlabfrag output unique psfrag command numbers for every eps file? I'm including two eps files in one figure using the subfigure command, and the labels get all mixed up because some of the psfrag command numbers are the same for both files. I've tried to move the \input{*.tex} commands right before the relevant \subfigure and it worked but it had some undesirable side effect, ie the figures got stacked vertically instead of being side by side. So basically I want to include the *.tex files for both figures beforehand without messing the labels. I can change the numbers in the eps and tex files by hand of course but it's too ineffective.

17 Dec 2009 Viton Vitanis

Excellent piece of code! I'm wondering whether there is some automatic way to modify the size of the legend box during latex compilation so that it adapts to any font being used there.

02 Dec 2009 Paolo Bocchini

Very nice work!

19 Oct 2009 Laurin

I've been looking for exactly this program for about a year now. :)

Until now I used LaPrint for almost all of my Matlab -> Latex printing jobs. For me the biggest drawback of LaPrint is that it can only handle the Matlab "painters" renderer. In my diploma thesis I have many pcolor and isosurface plots (with nice phong lighing). With the "painters" renderer the first resulted in huge file sizes even when "downsampling" my data matrices to reasonable sizes while the latter where completely impossible to realize. With matlabfrag now even transparency works!

With matlabfrag I can now also put all my graphics in a subdirectory without manually modifying all of the tex-files like in LaPrint!

Keep up the good work!

16 Sep 2009 Tor

Extraordinary work! A lot better than LaPrint, I rarely need to fight this script. However, I do occasionally get a legend box that's too small for the legend text (the right hand side of the box cuts of a few letters). Usually solved by adding a "\," or two.

16 Jul 2009 Zebb Prime

Hi Matt,

I generally leave the font as the default (Helvetica). That said, matlabfrag should work with any font you choose in Matlab.

Cheers,
Zebb

14 Jul 2009 Matt

Hi, I'm finding this code (and pstool) to be very useful.

I would, however, like to know what font you use in matlab in order to get your legend sizing to work out in pdflatex.

07 Jul 2009 James Housley

Excellent! Found it much more useable than laprint, and now can cope with transparency (use the opengl renderer).

23 Jun 2009 Kamil Wojcicki

Great job, thanks!

23 Jun 2009 Will Robertson  
28 May 2009 Zebb Prime

Hi Rui,

The idea is that ['number'] gets written out to the eps file, and the tex file contains instructions (using the psfrag LaTeX package) to substitute it for the original label.

In LaTeX you can do this in two ways:
1. Load the psfrag package, and then when including the graphics, include the tex file:
\include{figure.tex}
\includegraphics{figure.eps}
-- This way only works with latex.

2. Load the psfrag and pstool packages, and use the command
\psfragfig{figure}
instead of the includegraphics command. This works for both latex (from pstool v1.2 on) and pdflatex.

Please contact me via email if you need more information.

Cheers,
Zebb

25 May 2009 Rui

hi.. great program. congratulations
but how can i avoid the axis text having this format: " [ 'number' ] "

Updates
10 Jun 2009

- Better handling of axis tick labels and changed the substitution text format.
- More examples.
- Improved User Guide.

21 Jun 2009

- Fixed a bug that occurred when the figure being exported had no text in it.

30 Jun 2009

- Preliminary support for the opengl and zbuffer renderers.

04 Jul 2009

Fixed support for rotated text when using the zbuffer and opengl renderers.

08 Jul 2009

Fixed some of the annoying resizing issues again.

09 Jul 2009

Improved tick label positioning.

15 Jul 2009

Improved alignment of negative xtick labels.
Legend callback should be suppressed during printing.

16 Jul 2009

Fixed the horribly broken compatibility for r2007a and r2007b.

23 Aug 2009

Fixed a bug that occurred if some (but not all) the tick labels are empty.

06 Oct 2009

- Text alignment tweaks (hopefully for the better). In particular, x-tick labels should be vertically aligned better.
- Bug fixes: z-tick label alignment and no-text detection.

13 Oct 2009

- Removed AutoTick test from axis scalings, and updated the user guide to reflect this.

26 Oct 2009

- Added a test for empty ticks, while ticklabels are not empty.
- Added sections for Legends and the 'Unable to interpret TeX string' warning in the userguide.

20 Nov 2009

Fixed a bug that occurred with [xyz]ticks set outside [xyz]lims.

11 Jan 2010

Changed default painters dpi to 3200, and fixed my broken usage of inputParser.

21 Feb 2010

Fixed a bug to do with blank strings, and a bug to do with baseline alignment.

15 Mar 2010

Fixed a version checking bug which caused it to break in r2010a, and a EPS integer overflow bug.

24 Mar 2010

Documentation update: for plain LaTeX, \input should be used instead of \include.

04 May 2010

Fixed a bug that occurred when a figure wasn't frontmost.

Contact us