convert Matlab array to Latex

76 views (last 30 days)
Del
Del on 18 Jun 2013
Answered: Daksh on 2 Feb 2023
I have an array of cells which I want to convert into a latex table.
I know for a simple array I can use the following command:
A = [1 2 3; 4 5 6; 7 8 9];
latex_table = latex(sym(A))
Which will create
latex_table =
\left(\begin{array}{ccc} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end{array}\right)
However, if I have something like this
B = ['Hello World' '2' '3'; 'yes' 'a' '6'; 'yes' '8' 'john']
How can I create a latex table from the matrix B?

Answers (1)

Daksh
Daksh on 2 Feb 2023
You can use the MATLAB latex() method to convert the MATLAB Array into latex table as follows:
B=["Hello" "2" "3"; "yes" "5" "6"; "Sam" "8" "John"];
latex_table = latex(sym(B))
latex_table = '\left(\begin{array}{ccc} \mathrm{Hello} & 2 & 3\\ \mathrm{yes} & 5 & 6\\ \mathrm{Sam} & 8 & \mathrm{John} \end{array}\right)'
If you intend to store "Hello World" inside the 1st element of the MATLAB array, it will yield an error while creating Latex table as it doesn't allow the same. For that, you can use the str2sym() method as follows:
B=[str2sym("Hello world") "2" "3"; "yes" "5" "6"; "Sam" "8" "John"];
latex_table = latex(sym(B))
latex_table = '\left(\begin{array}{ccc} \mathrm{Helloworld} & 2 & 3\\ \mathrm{yes} & 5 & 6\\ \mathrm{Sam} & 8 & \mathrm{John} \end{array}\right)'
Hope this helps!

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!