4.8

4.8 | 21 ratings Rate this file 189 Downloads (last 30 days) File Size: 2.5 KB File ID: #4894

matrix2latex

by Moritz Koehler

 

10 May 2004 (Updated 17 May 2004)

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 submission has inspired the following:
matrix2latex GUI, Modified matrix2latex., Matrix to latex Matrix
MATLAB release MATLAB 6.5 (R13)
Other requirements none
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (22)
07 Sep 2004 Rene Mayrhofer  
05 May 2005 Hani HAMDAN  
27 Jul 2005 Tim Janssen

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

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.

22 Feb 2006 weierand Switzerland

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

26 Sep 2006 vicente Torres

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

18 Jan 2007 Jens Rasmussen

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

... Enjoy life? - I will now!

05 Apr 2007 J D

Really helpful, thanks

25 Jan 2008 Gangadhar Garipelli

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

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

01 Feb 2008 gatsby great

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

11 Jun 2008 Christian Bohne

really useful! saves time!

03 Sep 2008 Ryan Krook

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

17 Sep 2008 John Russell

Works perfect!

08 Oct 2008 Marco Capasso  
18 Jan 2010 Tina Hegarty

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

26 Apr 2010 Jesse Blocher  
03 Jun 2010 Tristan Fletcher

This is fantastic. Thank you.

25 Sep 2010 Ruben Gaitan

thanks a lot! makes life easier!!

17 Jul 2011 xy

Thanks

03 Aug 2011 Reza Farrahi Moghaddam  
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);

Please login to add a comment or rating.
Updates
17 May 2004

Update to v 1.1

-> Supports now LaTeX font sizes

Tag Activity for this File
Tag Applied By Date/Time
data import Moritz Koehler 22 Oct 2008 07:19:26
data export Moritz Koehler 22 Oct 2008 07:19:26
array Moritz Koehler 22 Oct 2008 07:19:26
matrix Moritz Koehler 22 Oct 2008 07:19:26
latex Moritz Koehler 22 Oct 2008 07:19:26
table Moritz Koehler 22 Oct 2008 07:19:26
tex Moritz Koehler 22 Oct 2008 07:19:26

Contact us at files@mathworks.com