# Including a Matlab table in document

Using Matlab I got a table. Now I want to integrate in a LaTeX document. I assume it works like including an image in a document.

A = [1 2 3; 4 5 6; 7 8 9];
latex_table = latex(sym(A))

latex_table =

\left(\begin{array}{ccc} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end{array}\right)

\begin{table}
\begin{tabular}{...}
***...latex_table ...***
\end{tabular}
\end{table}

-
Could you specify the code a bit more. Right now you have mixed matlab and LaTeX code in your example. It would be useful to see what you do in matlab and LaTeX separately. –  Peter Jansson Dec 19 '12 at 15:36

## 2 Answers

I think the easiest way of doing this is to format the data in Matlab and use fprintf to save it to a file. You can then include that file in your Latex-document.

A = [1 2 3; 4 5 6; 7 8 9];
latex_table = latex(sym(A));
file = fopen('/path/to/output.tex','w');
fprintf(file,'%s',latex_table);
fclose(file);


Or you can write a Matlab function which output the latex-code as a macro.

function [ ] = savetable(varargin)
% SAVETABLE(handle, matrix, file)

handle = char(varargin{1});
latex_table = latex(sym(varargin{2}));

file = fopen(varargin{3}, 'a');
fprintf(file,'\\newcommand{\\%s}{\n\t',handle);
fprintf(file,'\t%s\n}',latex_table);
fclose(file);


Example
Use can use this to write several different equations in the same file as shown below.

A = [1 2 3; 4 5 6; 7 8 9];
B = [10 11 12;13 14 15;16 17 18];
savetable('eqA',A,'equations.tex')
savetable('eqB',B,'equations.tex')


A Latex-document might look something like this:

\documentclass{article}
\input{equations}

\begin{document}
Here we have a nice matrix:
$\eqA{}$
And here is another one:
$\eqB{}$
\end{document}

-
thanx. This is exactly what was my first thought. But, how can i in latex open many equations.tex files. Infact, I want a latex file with 129 such detailed tables at specified positions. –  user31177 Dec 19 '12 at 18:33
I added an example to my answer. The 'a' flag in fopen tells Matlab to "Create and open a new file or open an existing file for writing, appending to the end of the file.", so you can run savetable 129 times and output it all in one file, which then will contain all your equations. The rest is just a matter of calling the macros at the right place in the Latex code. –  Ahlqvist Dec 19 '12 at 19:08
@ Ahlqvist I copy paste your latex code but I cann't use your latex code because I am getting a lot of errors. What to do? –  user31177 Dec 20 '12 at 8:28
Can you post any of the error messages? The code works for me. Does the Matlab script work? My equations.tex looks like this: \newcommand{\eqA}{\left(\begin{array}{ccc} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end{array}\right)} (and more line which looks similar). You need to have all equations (eqA, eqB, et al.) defined in that file or it won't work. –  Ahlqvist Dec 20 '12 at 23:57

The following is how you would use it:

\documentclass{article}
\begin{document}
\begin{table}
$\left(\begin{array}{ccc} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end{array}\right)$
\caption{Here is my table.}
\end{table}

This is a table generated in Matlab:
$\left(\begin{array}{ccc} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end{array}\right)$
\end{document}


Note that you're inserting math content in a table (which is perfectly fine), but not necessary, even though intuitively a "table" (or two-dimensional structure) should be put inside a table or tabular environment. The second option ("inline" display math) is preferred.

-
i don't want to do it with mouse. I wish to insert it (all tables in a specified place )automatically. –  user31177 Dec 19 '12 at 16:10
@user31177: I don't understand. You want to type something like A = [1 2 3; 4 5 6; 7 8 9]; latex_table = latex(sym(A)) in Matlab and have it automatically wrap it in a table environment? That would be something that has to be done on the Matlab side of things... not from within LaTeX. –  Werner Dec 19 '12 at 17:07
Is my answer at How to create a random math problem in LaTeX? closer to what you want? Granted, it was originally written for making sets of homework assignments, but at its core, it's a way to make MATLAB insert content into a LaTeX document. –  Mike Renfro Dec 19 '12 at 18:03