4.64516

4.6 | 32 ratings Rate this file 265 Downloads (last 30 days) File Size: 2.5 KB File ID: #4894

matrix2latex

by

 

10 May 2004 (Updated )

MATLAB array to LaTeX table converter.

| Watch this File

File Information
Description

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!!!

Acknowledgements

This file inspired Matrix2latex Gui, Modified Matrix2latex., 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 (34)
06 Oct 2014 Mikel Larrea

It worked like a charm. Thank you very much!

08 Jul 2014 Ole  
24 Apr 2014 Richard Mertens

Great work, thanks!

29 Mar 2014 Monika

THANK YOU! You saved me weeks!!

25 Mar 2014 Sturla Kvamsdal  
24 Sep 2013 David

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.

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

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';
end
if (strcmpi(alignment, 'left'))
alignment = 'l';
end
if (strcmpi(alignment, 'center'))
alignment = 'c';
end
if (strcmpi(alignment, 'l')) && (strcmpi(alignment, 'r')) && (strcmpi(alignment, 'c')) && ~(strfind(alignment, 'p{'))
alignment = 'l';
warning('matrix2latex: ', 'Unkown alignment. (Set it to \''left\''.)');
end

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

03 Aug 2011 Reza Farrahi Moghaddam  
17 Jul 2011 xy

Thanks

25 Sep 2010 Ruben Gaitan

thanks a lot! makes life easier!!

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.
Thanks

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