View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

4.7 | 37 ratings Rate this file 128 Downloads (last 30 days) File Size: 2.5 KB File ID: #4894 Version: 1.0



Moritz Koehler (view profile)

  • 1 file
  • 4.66667


10 May 2004 (Updated )

MATLAB array to LaTeX table converter.

| Watch this File

File Information

This function converts an MATLAB 2-dimensional array (matrix) which can be numerical or a cell array, into proper formated LaTeX code. The result will be a file that can be included into any LaTeX document, and will display the data through the 'tabular' environment.

% Usage:
% matrix2late(matrix, filename, varargs)
% where
% - matrix is a 2 dimensional numerical or cell array
% - filename is a valid filename, in which the resulting latex code will
% be stored
% - varargs is one ore more of the following (denominator, value) combinations
% + 'rowLabels', array -> Can be used to label the rows of the
% resulting latex table
% + 'columnLabels', array -> Can be used to label the columns of the
% resulting latex table
% + 'alignment', 'value' -> Can be used to specify the alginment of
% the table within the latex document. Valid arguments are: 'l', 'c',
% and 'r' for left, center, and right, respectively
% + 'format', 'value' -> Can be used to format the input data. 'value'
% has to be a valid format string, similar to the ones used in
% fprintf('format', value);
% + 'size', 'value' -> One of latex' recognized font-sizes, e.g. tiny,
% HUGE, Large, large, LARGE, etc.
% Example input:
% matrix = [1.5 1.764; 3.523 0.2];
% rowLabels = {'row 1', 'row 2'};
% columnLabels = {'col 1', 'col 2'};
% matrix2latex(matrix, 'out.tex', 'rowLabels', rowLabels, 'columnLabels', columnLabels, 'alignment', 'c', 'format', '%-6.2f', 'size', 'tiny');
% The resulting latex file can be included into any latex document by:
% /input{out.tex}
% Enjoy life!!!


This file inspired Modified Matrix2latex., Matrix2latex Gui, Matrix To Latex Matrix, and Matrix2lyx.

MATLAB release MATLAB 6.5 (R13)
Other requirements none
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (39)
30 May 2016 DZ

DZ (view profile)

I would much appreciate if there was option that allows the output to be in `array` environment. (ie. no \hline) And it would be nice if the output allowed for appending to file as oppose to overwrite as well. Regardless, thank you!

20 Oct 2015 Amir

Amir (view profile)

27 Aug 2015 Sha

Sha (view profile)

26 Jun 2015 Farid Salazar Wong

Could this possibly work if my table contains symbols instead of numbers.

matrix=[x1, x2; x3, x4]

08 May 2015 St Sakkas

06 Oct 2014 Mikel Larrea

It worked like a charm. Thank you very much!

08 Jul 2014 Ole

Ole (view profile)

24 Apr 2014 Richard Mertens

Great work, thanks!

29 Mar 2014 Monika

Monika (view profile)

THANK YOU! You saved me weeks!!

25 Mar 2014 Sturla Kvamsdal

24 Sep 2013 David

David (view profile)

Simple and effective.

18 Apr 2013 ss4johnny Hall

This is great, but I did notice that Lyx would not compile when I had a % in either the row or column names. Also, I believe a simple adjustment can be made to lines 103-111 so that you can index the format variable so that it need not be the same for every part of the table.

06 Nov 2012 Gajendra jung Katuwal

Just what I need. Thanks a lot.

16 Oct 2012 Jonathan

This just saved me a lot of twiddling around, thank you so much.

Comment only
16 Oct 2012 Jonathan

18 Jul 2012 Tyler R

This is a great function. However, it appears that formatting does not work if the array is a string.

16 Mar 2012 Stefan

Stefan (view profile)

Excellent function, although I seem to have a problem with extra invalid characters that latex complains about in the place of empty cells. Has anybody else seen this?

04 Aug 2011 Reza Farrahi Moghaddam

I may suggest following changes to add 'p{}' alignment:

Lines 73-87:
case 3 % alignment
alignment = lower(pval);
if (strcmpi(alignment, 'right'))
alignment = 'r';
if (strcmpi(alignment, 'left'))
alignment = 'l';
if (strcmpi(alignment, 'center'))
alignment = 'c';
if (strcmpi(alignment, 'l')) && (strcmpi(alignment, 'r')) && (strcmpi(alignment, 'c')) && ~(strfind(alignment, 'p{'))
alignment = 'l';
warning('matrix2latex: ', 'Unkown alignment. (Set it to \''left\''.)');

and Line 124:
fprintf(fid, '%s|', alignment);

03 Aug 2011 Reza Farrahi Moghaddam

17 Jul 2011 xy

xy (view profile)


25 Sep 2010 Ruben Gaitan

thanks a lot! makes life easier!!

Comment only
03 Jun 2010 Tristan Fletcher

This is fantastic. Thank you.

26 Apr 2010 Jesse Blocher

18 Jan 2010 Tina Hegarty

This is one of the handiest little .m files I've come across. My sincere thanks. :)

08 Oct 2008 Marco Capasso

17 Sep 2008 John Russell

Works perfect!

03 Sep 2008 Ryan Krook

It works but I don't think the formatting works. Could just be me.

11 Jun 2008 Christian Bohne

really useful! saves time!

01 Feb 2008 gatsby great

Great m.file! Best in years! Thanks a lot!

31 Jan 2008 Nicolas Bercher

Just great !
I'm working on a new version that supports many extra features: custom format strings, enable/disable borders, extra title/footnotes, custom alignment, enable/disable \hline for data lines, support for NaN values, etc.
Coming soon...

25 Jan 2008 Gangadhar Garipelli

thanks.... a simple one... and useful! saves time!

05 Apr 2007 J D

Really helpful, thanks

18 Jan 2007 Jens Rasmussen

What a timesaver, I was just about to make one myself.

... Enjoy life? - I will now!

26 Sep 2006 vicente Torres

Excellet. very useful, My students have to know about this script

22 Feb 2006 weierand Switzerland

It is a very good and usefull script.
Easy to use.

09 Dec 2005 Chris Granade

Found a problem... throws an error if äalignmentä is set to äleftä. Need to use strcmp instead of ´´. Also, thereäs a cool trick for aligning LaTeX tables to the decimal point, which is to split each number into two columns, and set the column alignment to är").=lä, which doesnät seem to be allowed bz zour script.

27 Jul 2005 Tim Janssen

Very effective little script to get away from having to type latex tables manually. I am grateful!

05 May 2005 Hani HAMDAN

07 Sep 2004 Rene Mayrhofer

Contact us