Code covered by the BSD License  

Highlights from
Converting MATLAB data to LaTex table


5.0 | 7 ratings Rate this file 243 Downloads (last 30 days) File Size: 3.86 KB File ID: #44274
image thumbnail

Converting MATLAB data to LaTex table



11 Nov 2013 (Updated )

Converts matrices or tables to LaTex table code. Features data formats,pivoting,column/row headers.

| Watch this File

File Information

An easy to use function that generates code for a LaTex table from a given numeric MATLAB array. It is also possible to use the new MATLAB datatype 'table' as data input. Optional parameters are available to control the data format column- or row-wise, pivot the table and several other table properties. The LaTeX code is printed into the command window for quick copy&paste and also given back as a cell array for further processing in MATLAB. Enjoy :)


This file inspired Print Table.

Required Products MATLAB
MATLAB release MATLAB 8.2 (R2013b)
Other requirements If you want to use a MATLAB table as data input, your 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 (19)
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

Thanks for posting this code. Very useful!

24 Aug 2014 Pablo  
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.

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.

08 Apr 2014 Manuel

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

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.

08 Apr 2014 Manuel

And is it possible to support utf8 signs ?

07 Apr 2014 Manuel  
07 Apr 2014 Manuel

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 ?

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,:});

07 Apr 2014 Manuel

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

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.

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.

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.

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

05 Dec 2013 Timo

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

It does the job quick and simple.Great!

21 Nov 2013

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

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

15 Feb 2014

Changes in comment section

18 Feb 2014

Updated comments in example script

24 Feb 2014

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

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

Some changes in the documentation.

07 Aug 2014

Changed file descriptions.

07 Aug 2014

Improved documentation.

07 Aug 2014

changed title

Contact us