Get from Ico-github-logo

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

» Watch video

Highlights from

  • latexTable(input)
    An easy to use function that generates a LaTeX table from a given MATLAB
  • runExamples.m
    This script runs two simple examples of latexTable.m
  • View all files
5.0 | 19 ratings Rate this file 266 Downloads (last 30 days) File Size: 13.7 KB File ID: #44274 Version: 1.19
image thumbnail




11 Nov 2013 (Updated )

Converts matrices, cells and MATLAB tables to LaTex table code. Allows format,pivot,col/row headers.

| Watch this File

File Information

Converts matrices, cells or MATLAB tables to LaTeX table code. Easy to use and very flexible:
- takes MATLAB matrices, cells or tables as input
- gives code for a LaTeX table containing your data as output
- handles numeric and text/string data
Optional features are:
- column/row labels
- column/row-wise formatting (like setting the number of digits etc.)
- pivoting the table (90 degree rotation making columns rows and vice versa)
- setting the default symbol for NaN values
- LaTeX column alignment
- LaTeX table borders, caption, label
- output a complete LaTeX document instead of a plain table
A little example:[1,3;4,2]; % set up data
  latexTable(input) % obtain LaTeX code

For further details and examples can be found in latexTable.m and runExamples.m


This file inspired Print Table and Online Portfolio Selection With Transaction Costs Including Market Impact Costs.

Required Products MATLAB
MATLAB release MATLAB 8.2 (R2013b)
Other requirements If you want to use a MATLAB table as data input, MATLAB version needs to be r2013b or newer.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (51)
19 May 2016 Anders

Anders (view profile)

Very nice function.
A little bug with the representation of NaN. The field you check for is not the one the user should input:

% Define what should happen with NaN values in input.tableData:
if ~isfield(input,'nanString'),input.dataNanString = '-';end

06 May 2016 Egle Jakucionyte  
04 Apr 2016 robertosangi

You're the best. I never use a function and for thar reason it doesn't play. Thanks for support and for all!

03 Apr 2016 Eli Duenisch

This looks to me like the function is not in your current MATLAB search path. Copy latexTable.m to your current working directory. If your MATLAB setup is ok the code provided in runExamples.m should run without errors.
Feel free to send me some part of your code as a PM or Pastebin link. This should be fixed easily.

Comment only
03 Apr 2016 robertosangi

Nothing to do... Now Matlab give me this message:
Undefined function or variable 'latexTable'

I wrote:


Comment only
17 Mar 2016 Eli Duenisch

Hi Roberto,
I guess the error comes from giving your table the name 'latexTable' which will shadow the function with the same name. Just choose a different name for the table:

% some MATLAB table

% store table in input struct;

% call function

If it doesn't work, let me know.

Comment only
17 Mar 2016 robertosangi

The function give me a message:
I call my table
I write
At least i write

And Matlab give me:
You cannot subscript a tablr using only one subscript. Table subscripting requires both row and variable subscripts.

Can you help me? Can you give me all meticulous passages to generate my latex table? Thanks

Comment only
15 Mar 2016 Eli Duenisch

Hi Luc, as far as I know all files published here are licensed according to BSD (not sure whether other licenses are allowed). To make it more obvious I will include the advertising clause in the code. Best, Eli

Comment only
14 Mar 2016 Luc Le Magoarou

Hi ELi,

under which license do you distribute your code ? If it is not specified, redistribution under any form is not allowed, that is why I ask this question.

Thanks for the great code anyway,


19 Nov 2015 Eli Duenisch

Hi Seeda, the code runs for string data as well. Please make sure to put LaTex code that is included in your data in $...$ environments. There was a little bug for handling MATLAB tables labels which should be fixed now. I also included a new example in runExamples.m using strings including LaTex code. Have fun :)

Comment only
18 Nov 2015 Seeda

Seeda (view profile)

Has someone figured out how to make latexTable.m to work with string entries? I took the suggestions I saw in the comments but did not work for me.

13 Nov 2015 Eli Duenisch

I'm sorry but I cannot reproduce the issues. Best thing would be that you send me an example and the latexTable.m that you are using. Which version of MATLAB are you using? input.tableRowLabels should work fine, try runing the examples given in runExamples.m. If you don't need row labels just do not set the field.

Comment only
11 Nov 2015 Joseph Cullen

The updates to the program fixed the problems. There are a couple more issues that popped up.
1. input.tableRowLabels doesn't seem to do anything. input.tableColLabels works as it would be expected
2. I am getting strange results from input.dataFormat where all of the numbers in the row correctly formatted except for the first one.

Comment only
25 Oct 2015 Eli Duenisch

Hi Joseph, it seems that you are using an older or modified version of the code (line 87 in the current version just contains a comment). In order to run the function properly you should stick to the field names given in the examples/comment section. If you use different names the function will inevitably throw errors. I hope that helps.

Comment only
15 Oct 2015 Joseph Cullen

get the following error when running the examples.
need to change to using input.tableData rather than

Reference to non-existent field 'tableData'.

Error in latexTable (line 87)
if strcmp(class(input.tableData),'table')

Error in runExamples (line 57)
latex = latexTable(input);

Comment only
15 Oct 2015 Joseph Cullen

This is outputing a latex table that is a transpose of the table I am providing. Setting the input.transposeTable = 0 or to 1 does not seem to make a difference.

Comment only
12 Sep 2015 Eli Duenisch

Hi vivi, thank you for your feedback. You are right, the code asumed that row and column names are given when using a MATLAB table. I fixed that now by setting the corresponding input fields only if names are given. Hope that helps.

Comment only
12 Sep 2015 vivi

vivi (view profile)

I have found what I think is a bug. This happens only when using the matlab table( ) command and if row names are not assigned in the table( ) command, even though they are assigned via input.tableColLabels. This is the error I get:

Subscripted assignment dimension mismatch.

Error in latexTable (line 116)

Error in plots (line 62)
latex = latexTable(input);

This is happening because this part of the code
% process table datatype
if isa(,'table')
input.tableColLabels =';
input.tableRowLabels ='; = table2array(';

creates the field input.tableColLabels, and then the test

colLabelsExist = isfield(input,'tableColLabels');

returns a 1, even though there are no tableColLabels created originally. Then when the script tries to write the row names in the table via this statement

if colLabelsExist

the error occurs since input.tableColLabels = { }

Comment only
25 Aug 2015 Sargondjani

Works like a charm!!! :D ... this seriously reduces the time I waste on editing :p

21 Aug 2015 JMP Phillips

Hello, the file in its current version only supports numeric tables, and so isnan(dataValue) will throw an error if dataValue is a string.

For this reason I propose the following simple modification to latexTable to support tables of strings or text rather than numbers:

% generate table
for i=1:size(C,1)
if input.tableBorders
latex(end+1) = {hLine};
rowStr = '';
for j=1:size(C,2)
dataValue = C{i,j};
if iscell(dataValue)
dataValue = dataValue{:};
elseif isnan(dataValue)
dataValue = input.dataNanString;
elseif isnumeric(dataValue)
dataValue = num2str(dataValue,dataFormatArray{i,j});
if j==1
rowStr = dataValue;
rowStr = [rowStr,' & ',dataValue];
latex(end+1) = {[rowStr,' \\']};

Comment only
29 Jul 2015 Eli Duenisch

I forgot to mention the following workflow:
1. load mat-file in workspace: load('blabla.mat')
2. make a struct containing data (and additional parameters if you want to):;input.tableColLabels=...;...
3. call function with the struct: latexTable(input)

Comment only
29 Jul 2015 Eli Duenisch

@David Please make sure that the data you want to plot is given as a struct. The struct needs to contain a field named 'data' which contains the data values you want to put in the LaTex table. For example lets assume your data is stored as a matrix M in the MATLAB workspace. Then use = M to obtain the struct. After that call latexTable(input). You may also add other parameters to the struct to modify some properties of the LaTex table. For details please have a look at the header section of the function or runExamples.m. Let me know if you have any further questions.

Comment only
29 Jul 2015 David Stevens

Hi, Thanks you very much for this great input!
I would like to convert a '*.mat' into a LaTex file. Unfortunately while running this function I just get the error message
"Reference to non-existent field 'data'.
Error in latexTable (line 96)
if isa(,'table')".
Do you have any idea how can I convert this "*.mat" into a LaTex file ussing this function? Thanks!

Comment only
04 May 2015 Eli Duenisch

Hi, as far as I know MATLAB does not offer a built-in functionality to convert decimal numbers to engineering notation strings.
However you there is a function here at the file exchange called 'num2eng'. Another approach can be found here

Comment only
14 Apr 2015 Sebastian Stoelen

Works perfectly! However, my table entries are really small values, is there any way to specify that the entries should appear in engineering notation rather than with a fixed number of digits after the decimal point?

03 Apr 2015 Eli Duenisch

Hi Adithyan,
the function gives a cell array as output, so you may use the following code to avoid copy&paste:

latex = latexTable(input);
for i=1:size(latex,1)
fprintf(fid, '%s\n', latex{i});

I'll think about including the code in runExamples.m or in the function itself.

Comment only
27 Mar 2015 Manuel

Manuel (view profile)

27 Mar 2015 ADITHYAN

Wonderful. Works like a charm.
Could you also maybe add a functionality where I can save this table in .tex file at user defined location.
This way I could easily import my code into latex using \input{.tex}, rather than copying and pasting every time.

27 Mar 2015 ADITHYAN  
14 Mar 2015 Eli Duenisch

I just fixed the output when no row or column labels are set. Thank you Dimitar :)

Thank you for mentioning this. Maybe I will implement your approach using a loop and repmat for the formatting in the next update.

Comment only
06 Feb 2015 Dimitar Atanasov

cellSize = [numberDataRows+colLabelsExist,numberDataCols+rowLabelsExist];

Should be like this

29 Dec 2014 Andreas Trier Poulsen  
09 Dec 2014 Jan Kubanek

I needed a more flexible formatting of the columns or rows, like this:
input.dataFormat = {'%.1f', [1 3 5], '%.3f', [2 4 6]};
This sets different formats for particular columns (alternating in this example).

To do so, simply change the loop starting with
for i=1:2:lengthDataFormat

for i=1:2:lengthDataFormat
dataFormatList(input.dataFormat{i+1},1) = repmat(input.dataFormat(i),length(input.dataFormat{i+1}),1);

24 Aug 2014 Pablo

Pablo (view profile)

Thanks for posting this code. Very useful!

24 Aug 2014 Pablo

Pablo (view profile)

27 Jul 2014 Eli Duenisch

This update brings the option to pivot/transpose your table. It is now possible to define the data format (how your data is shown in LaTex) column- or row-wise.

Comment only
10 Apr 2014 Eli Duenisch

The function works row-wise so the format for each data row is the same. You could tweak the code in section '% generate row-format cell array based on input.tableDataFormat' and then apply the format in section '% make table rows:'. I'm planning to implement switching between row-wise and cloumn-wise tables in my next update.

Comment only
08 Apr 2014 Manuel

Manuel (view profile)

Is it possible to change the format of each single column differently like changing the format of each row ?

Comment only
08 Apr 2014 Eli Duenisch

By now the function only supports multi-row headers. I'm planning to rewrite the code in a way that the whole table can be pivoted between row-based and cloumn-based. This would also cover multi-line and multi-row headers.

Comment only
08 Apr 2014 Manuel

Manuel (view profile)

And is it possible to support utf8 signs ?

Comment only
07 Apr 2014 Manuel

Manuel (view profile)

07 Apr 2014 Manuel

Manuel (view profile)

Thank you very much...It works very well. I expected this way to be the solution but didn't find the right way to code it. Is it also possible to have multicolumn headers ?

Comment only
07 Apr 2014 Eli Duenisch

Hi Manuel, yes it is possible. The function gives back a cell array containing the lines of the LaTex code. You can loop through this cell array and print the lines to a text document. Try something like this:

myCellArray = latexTable(input);
fid = fopen('myLatexFile.tex', 'wt+')
for i = 1 : size(myCellArray,1)
fprintf(fid, '%d%d%s\t\n', myCellArray{i,:});

Comment only
07 Apr 2014 Manuel

Manuel (view profile)

Is it possible to output the code direktly to a .tex document from the command window ?? If not, how can I realize ?

Comment only
22 Feb 2014 Eli Duenisch

It is now possible to use the new MATLAB datatype 'table' as data input.
Column headers are added as an optional input.

Comment only
15 Feb 2014 Eli Duenisch

Hi Mehmet, it seems to me that your code is not providing the field input.tableData which contains the data that should be placed in a LaTex table. I just uploaded a script runExample.m that runs an example of how to use latexTable.m. Maybe you try to run this on your system and compare it with your code. Please let me know if the problem persists.

Comment only
15 Feb 2014 Mehmet Niyazi ÇANKAYA


I received error message

Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release Notes, Assigning Nonstructure Variables As Structures
Displays Warning, for details.
> In latexTable at 65
Reference to non-existent field 'tableData'.

Please help how I can solve it.

Comment only
09 Dec 2013 Pi Ting


05 Dec 2013 Eli Duenisch

Hi Timo, thank you for your feedback. I have been thinking about the support of MATLAB tables and I will implement it in the update (in a week or so).

Comment only
05 Dec 2013 Timo

Timo (view profile)

Fantastic submission, Eli. A true time saver. Thanks! I am just getting to know the 2013b table function which so far looks really useful. Would it be possible to have latexTable accept a matlab table as an input? That would be amazing.

22 Nov 2013 Chris

Chris (view profile)

It does the job quick and simple.Great!

21 Nov 2013 1.1

Now you can set a replacement value for NaNs in your MATLAB data, e.g. use an empty string for a blank space or '-' for a minus sign.

15 Feb 2014 1.2

Added a script to run an simple example of latexTable.m

15 Feb 2014 1.3

Changes in comment section

18 Feb 2014 1.4

Updated comments in example script

24 Feb 2014 1.5

It is now possible to use the new MATLAB datatype 'table' as data input.
Column headers are added as an optional input.

28 Jul 2014 1.6

This update enables pivoting of the table (=transpose data and labels). The data format definition can now be applied column- or row-wise.

07 Aug 2014 1.7

Some changes in the documentation.

07 Aug 2014 1.8

Changed file descriptions.

07 Aug 2014 1.9

Improved documentation.

07 Aug 2014 1.10

changed title

14 Mar 2015 1.11

Fixed output when no row and/or column labels are set. Thank you Dimitar Atanasov for mentioning!

12 Sep 2015 1.12

it is now possible to use MATLAB tables without row and/or column names

12 Sep 2015 1.13

function can now process data values given as strings (code proposed by JMP Phillips)

12 Sep 2015 1.14

updated version date, switched back from github to regular upload

12 Sep 2015 1.15

using github repo next trial

12 Sep 2015 1.16

fixed git repo upload

20 Nov 2015 1.17

updated summary

03 Jan 2016 1.18

updated description

05 Jan 2016 1.181

minor changes

20 Apr 2016 1.19

added for github, added BSD 2 license comment (the default license at MathWorks File Exchange)

Contact us