# Get from

### Highlights from latexTable

• 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

Join the 15-year community celebration.

Play games and win prizes!

5.0
5.0 | 24 ratings Rate this file 268 Downloads (last 30 days) File Size: 17.6 KB File ID: #44274 Version: 1.21

# latexTable

### Eli Duenisch (view profile)

11 Nov 2013 (Updated )

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

File Information
Description

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
- easy including in your code for automation
- easy direct saving as a LaTex document (see example 2 in runExamples.m)
Optional features are:
- table positioning
- 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:
input.data=[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

Acknowledgements
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.
27 Sep 2016 defrunze

### defrunze (view profile)

Thanks, Eli! That definitely helped a lot.

The copy-paste is no longer a problem.

25 Sep 2016 Eli Duenisch

### Eli Duenisch (view profile)

Hi defrunze,

You could achieve floating-point formatting by setting a well-suited formatSpec in input.dataFormat. For example if you use '%.9g' there will print out nine significant digits ignoring the position of the decimal point:

disp(num2str(123.456789,'%.9g'));
disp(num2str(1.23456789,'%.9g'));

The copy&paste problem in yout LaTex editor:
If this is still a problem - could you provide a short version of the LaTex code that reproduces the error (via email)?

I hope that helps.

Comment only
21 Sep 2016 defrunze

### defrunze (view profile)

Hi Eli,

Whenever I convert an array to a latexTable, I get numbers in fixed point format. Is there any way to get them in floating point format?

Comment only
20 Sep 2016 defrunze

### defrunze (view profile)

Hi Eli,
when i copy-paste the output into TeX editor and typeset, it gives me an error that has something to do with \hline commands and \centering.

Comment only
31 Aug 2016 Eli Duenisch

### Eli Duenisch (view profile)

Hi Benjamin,
if you want to use the table placement option (current version of latexTable),
just set up the input struct like in this little example:

input.tablePlacement='H'; % table placement parameter
input.data = magic(3); % get some data
latex = latexTable(input);

This should add the placement parameter in brackets [H] to the resulting LaTex code:

\begin{table}[H]
\centering
\begin{tabular}{|c|c|c|}
...

One thing to remember is that depending on your LaTex compiler settings, included packages etc. positioning might behave differently.
I'm not sure if that answers your question. If not just let me know.

Comment only
31 Aug 2016 Benjamin Leiva

### Benjamin Leiva (view profile)

Thanks Eli. I got the first part, but I could not figure out how to incorporate the table position command. I have the current version, and replaced everywhere in the following command with 'H' as you specified, but it did not work.

% Placement of the table in LaTex document
if isfield(input,'tablePlacement') && (length(input.tablePlacement)>0)
input.tablePlacement = ['[',input.tablePlacement,']'];
else
input.tablePlacement = '';
end

AN H just appears besides the table.

Thanks so much for your help

Comment only
25 Aug 2016 Eli Duenisch

### Eli Duenisch (view profile)

Hi Benjamin,
setting column and/or row-labels should actually be ok. Could you please provide
an example that reproduces the error? Here is some code that should work:

clc;clear input; % clear previous stuff
input.data = magic(3); % get some data
input.tableColLabels = {'col1','col2','col3'}; % col labels
input.tableRowLabels = {'row1','row2','row4'}; % row labels
latex = latexTable(input);

Having an option to include the table position is a good point - I've included
it now in the current version. For example:
...
input.tablePlacement = 'H';
...
If you want to stick with the old version you may use the following workaround:
% set up the input struct and run the function as usual
input.data = magic(3);
latex = latexTable(input);
% then ...
clc; % clear command window for convenience
latex{1}='\begin{table}[H]'; % overwrite first line
disp(char(latex)); % print altered code

Comment only
24 Aug 2016 Benjamin Leiva

### Benjamin Leiva (view profile)

Hi Eli, I have two questions:

The first, is that the input.tableColLabels does not work for me, whereas the input.tableRowLabels does (and I am doing the same thing for both). Might there be a mistake with the first?

The second follows Chocolateee: when exporting to latex, I get that the table is pasted at the top of the page. This could manually be solved by copying and pasting the code and forcing the location with [H], but that attempts against the automation just achieved. Is there a way to manage this issue?

Thank you! Your package is great!

Comment only
01 Aug 2016 Eli Duenisch

### Eli Duenisch (view profile)

Hi Chocolateee,
here is an example how to save your code as a .tex file:

% the data
input.data = [1.12345 NaN 3.12345; ...
4.12345 5.12345 6.12345; ...
7.12345 8.12345 9.12345; ...
10.12345 11.12345 12.12345];
% we want a complete LaTex document
input.makeCompleteLatexDocument = 1;
% generate LaTex code
latex = latexTable(input);
% save LaTex code as file
fid=fopen('MyLatex.tex','w');
[nrows,ncols] = size(latex);
for row = 1:nrows
fprintf(fid,'%s\n',latex{row,:});
end
fclose(fid);

I think I will add this to the example code. Thank you for your feedback.

Comment only
31 Jul 2016 Chocolateee

### Chocolateee (view profile)

Hi Eli, this function works beautifully! One question, what is the most convenient way to save the cell output into a tex file that can be included into my main tex file? I tried with fprintf and save but had little success. You may consider include this feature in the future.

31 Jul 2016 Chocolateee

### Chocolateee (view profile)

20 Jun 2016 Eli Duenisch

### Eli Duenisch (view profile)

Hi ExtremeOPS,
thank you for your feedback and the feature proposals. It's great that you are offering to contribute to the code - for the individual features please open pull requests (a link to the github repository can be found below the download button on this page). Of course you will be mentioned in the author section afterwards.
Here are my two cents about the proposed features:
- file-saving should be handeled by the embedding script because this function should be kept as simple/reduced as possible
- individual column alignment should be a new feature
- printing the generated LaTex code should be optional, enabled by default

Again thank you for your time and valuable feedback.
Best

Comment only
20 Jun 2016 ExtremOPS

### ExtremOPS (view profile)

Hi Eli,

I really like your function! It does all the basic jobs you want to have.

I just added for myself the following stuff:
- function for saving the file in a specific folder; I needed to add for this input.savename and input.savefolder
- changed the function so not all columns have the same alignment type but you can choose manually; I did it the same way how you did it with the input.format
- and also the option of not showing the output in the command window; default setting is showing

I can add this to your code if you like you just need to tell me how to this and I will.

In the meantime I thank you very much for your work.

31 May 2016 Eli Duenisch

### Eli Duenisch (view profile)

@Anders thank you for the hint - helps to make this little tool better. It is fixed now.

Comment only
27 May 2016 Troels Landbo

### Troels Landbo (view profile)

Doing all the data processing in relation with the Bachelor project at this moment. This meant that the raw data was appended in the PDF report and not only in a zip file. Thanks

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

### Egle Jakucionyte (view profile)

04 Apr 2016 robertosangi

### robertosangi (view profile)

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

### Eli Duenisch (view profile)

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

### robertosangi (view profile)

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

I wrote:

myTable=T;
input.data=myTable;
latexTable(input)

Comment only
17 Mar 2016 Eli Duenisch

### Eli Duenisch (view profile)

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
myTable=table(magic(5));

% store table in input struct
input.data=myTable;

% call function
latexTable(input)

If it doesn't work, let me know.

Comment only
17 Mar 2016 robertosangi

### robertosangi (view profile)

The function give me a message:
I call my table
latexTable
I write
input.data=latexTable
At least i write
latex=latexTable(input)

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

### Eli Duenisch (view profile)

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

### Luc Le Magoarou (view profile)

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,

Luc

19 Nov 2015 Eli Duenisch

### Eli Duenisch (view profile)

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

### Eli Duenisch (view profile)

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

### Joseph Cullen (view profile)

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

### Eli Duenisch (view profile)

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

### Joseph Cullen (view profile)

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

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

### Joseph Cullen (view profile)

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

### Eli Duenisch (view profile)

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)
C(1,1+rowLabelsExist:end)=input.tableColLabels;

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

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

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
C(1,1+rowLabelsExist:end)=input.tableColLabels;
end

the error occurs since input.tableColLabels = { }

Comment only
25 Aug 2015 Sargondjani

### Sargondjani (view profile)

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

21 Aug 2015 JMP Phillips

### JMP Phillips (view profile)

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

### Eli Duenisch (view profile)

I forgot to mention the following workflow:
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

### Eli Duenisch (view profile)

@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

### David Stevens (view profile)

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

### Eli Duenisch (view profile)

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

### Sebastian Stoelen (view profile)

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

### Eli Duenisch (view profile)

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)

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.

14 Mar 2015 Eli Duenisch

### Eli Duenisch (view profile)

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

### Dimitar Atanasov (view profile)

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

Should be like this

29 Dec 2014 Andreas Trier Poulsen

### Andreas Trier Poulsen (view profile)

09 Dec 2014 Jan Kubanek

### Jan Kubanek (view profile)

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

### Eli Duenisch (view profile)

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

### Eli Duenisch (view profile)

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

### Eli Duenisch (view profile)

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

### Eli Duenisch (view profile)

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

### Eli Duenisch (view profile)

It is now possible to use the new MATLAB datatype 'table' as data input.

Comment only
15 Feb 2014 Eli Duenisch

### Eli Duenisch (view profile)

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

### Mehmet Niyazi ÇANKAYA (view profile)

Hello,

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

Comment only
09 Dec 2013 Pi Ting

### Pi Ting (view profile)

useful

05 Dec 2013 Eli Duenisch

### Eli Duenisch (view profile)

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

24 Feb 2014 1.5

It is now possible to use the new MATLAB datatype 'table' as data 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

20 Nov 2015 1.17

updated summary

03 Jan 2016 1.18

updated description

05 Jan 2016 1.181

minor changes

21 Jun 2016 1.19