File Exchange

image thumbnail


version (2.5 KB) by Moritz Koehler
MATLAB array to LaTeX table converter.


Updated 17 May 2004

View License

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

Cite As

Moritz Koehler (2021). matrix2latex (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (54)

Paolino Tona

Heng Guo

Gustav Axelsson

Md Rasheduzzaman

stefano crotti


Thanks man.

Antonio Demarcus

It works good for my 52*52 matrix input

matrix2latex( in , ' out.tex ' )

Patrick Russell

Oseze Esther Iyore

This does not work for me. I get the error:
Error using matrix2latex (line 49)
And when I check line 49 it says 'the format might not agree with argument count'.
Anyone have the same problem?

Buddhika Samarakoon

Diletta Rossini

Mahdi Aziz

Thanks, man, save me several hours!



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!



Farid Salazar Wong

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

matrix=[x1, x2; x3, x4]

St Sakkas

Mikel Larrea

It worked like a charm. Thank you very much!


Richard Mertens

Great work, thanks!


THANK YOU! You saved me weeks!!

Sturla Kvamsdal


Simple and effective.

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.

Just what I need. Thanks a lot.


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


Tyler R

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


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?

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);



Ruben Gaitan

thanks a lot! makes life easier!!

Tristan Fletcher

This is fantastic. Thank you.

Jesse Blocher

Tina Hegarty

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

Marco Capasso

John Russell

Works perfect!

Ryan Krook

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

Christian Bohne

really useful! saves time!

gatsby great

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

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

Gangadhar Garipelli

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


Really helpful, thanks

Jens Rasmussen

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

... Enjoy life? - I will now!

vicente Torres

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

weierand Switzerland

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

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.

Tim Janssen

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


Rene Mayrhofer

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!