convert Matlab array to Latex

31 次查看(过去 30 天)
Del
Del 2013-6-18
回答: Daksh 2023-2-2
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?

回答(1 个)

Daksh
Daksh 2023-2-2
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!

标签

Community Treasure Hunt

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

Start Hunting!

Translated by