File Exchange

image thumbnail

matrix2latex

version 1.0 (2.5 KB) by

MATLAB array to LaTeX table converter.

4.67568
39 Ratings

66 Downloads

Updated

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

Comments and Ratings (42)

Mahdi Aziz

Thanks, man, save me several hours!

M M

M M (view profile)

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!

Amir

Amir (view profile)

Sha

Sha (view profile)

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!

Ole

Ole (view profile)

Great work, thanks!

Monika

Monika (view profile)

THANK YOU! You saved me weeks!!

David

David (view profile)

Simple and effective.

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.

Jonathan

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

Jonathan

Tyler R

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

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?

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

xy

xy (view profile)

Thanks

Ruben Gaitan

thanks a lot! makes life easier!!

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!

J D

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

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!

Hani HAMDAN

Rene Mayrhofer

MATLAB Release
MATLAB 6.5 (R13)

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

» Watch video