Code covered by the BSD License  

Highlights from
Converting MATLAB data to LaTex table

5.0
5.0 | 14 ratings Rate this file 183 Downloads (last 30 days) File Size: 4.78 KB File ID: #44274 Version: 1.11
image thumbnail

Converting MATLAB data to LaTex table

by

 

11 Nov 2013 (Updated )

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

| Watch this File

File Information
Description

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

Acknowledgements

This file inspired Print Table.

Required Products MATLAB
MATLAB release MATLAB 8.2 (R2013b)
MATLAB Search Path
/
/latexTable
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 (33)
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};
end
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});
end
if j==1
rowStr = dataValue;
else
rowStr = [rowStr,' & ',dataValue];
end
end
latex(end+1) = {[rowStr,' \\']};
end

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.data=M;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 input.data = 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(input.data,'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 http://www.mathworks.com/matlabcentral/answers/892-engineering-notation-printed-into-files

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);
fid=fopen('laTexFile.tex','w');
for i=1:size(latex,1)
fprintf(fid, '%s\n', latex{i});
end
fclose(fid);

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

@Jan
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

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

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

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,:});
end
fclose(fid);

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

Hello,

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

useful

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!

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

Contact us