Difference between revisions of "MATLAB:LaTeX Table Writer"
Line 24: | Line 24: | ||
<source lang="matlab"> | <source lang="matlab"> | ||
for k=1:length(TC) | for k=1:length(TC) | ||
− | fprintf('%8.2f & %8.2f & %8.2f & %8.2f \\\\\n', MainMat(k,:)) | + | fprintf('%8.2f & %8.2f & %8.2f & %8.2f \\\\ \n', MainMat(k,:)) |
end | end | ||
</source> | </source> | ||
Line 34: | Line 34: | ||
100.00 & 373.15 & 212.00 & 671.67 \\ | 100.00 & 373.15 & 212.00 & 671.67 \\ | ||
</source> | </source> | ||
+ | |||
+ | === Generalized === | ||
+ | If every entry in a row will have the same precision, you can also use | ||
+ | <source lang="matlab"> | ||
+ | for ROW=1:size(MainMat, 1) | ||
+ | for COL=1:size(MainMat,2)-1 | ||
+ | fprintf('%8.2f & ', MainMat(ROW,COL)); | ||
+ | end | ||
+ | fprintf('%8.2f \\\\ \n', MainMat(ROW,end)); | ||
+ | end | ||
+ | </source> | ||
+ | which will automatically determine the appropriate number of rows and columns for the matrix. | ||
== Extra Code == | == Extra Code == | ||
Line 40: | Line 52: | ||
<source lang="matlab"> | <source lang="matlab"> | ||
for k=1:length(TC) | for k=1:length(TC) | ||
− | fprintf('%8.2f & %8.2f & %8.2f & %8.2f \\\\ \\hline\n', MainMat(k,:)) | + | fprintf('%8.2f & %8.2f & %8.2f & %8.2f \\\\ \\hline \n', MainMat(k,:)) |
end | end | ||
</source> | </source> |
Revision as of 18:02, 22 February 2009
Sometimes you will need to take data from a MATLAB script or function and present it using a LaTeX table. Rather than hand-code all the numbers, you may want to write a MATLAB loop to take care of producing the LaTeX code. This can be done in two parts:
- Create a single matrix with all the information in it
- Run a
for
loop to generate the LaTeX code
Contents
Create the Matrix
Probably the easiest way to create the matrix is to make sure all the information you want is stored in column vectors. Then you can just create one large matrix by listing all those vectors between square brackets. For example, if you want to create a table for temperatures in different units, you might have the following code:
TC = [-273.15 -40 0 100]';
TK = TC + 273.15;
TF = (TC+40)*9/5-40;
TR = TF + 459.67;
You could then generate a single matrix will all this information:
MainMat = [TC TK TF TR]
Generate LaTeX Code
Keep in mind for LaTeX tables, the entries are separated by & and the rows are terminated with \\. In MATLAB, to print a \, you must actually use the backslash command, which is \\. To get MATLAB to go to the next line, you need to use the \n command.
The following loop might be used to create the main parts of the LaTeX table for the matrix above:
for k=1:length(TC)
fprintf('%8.2f & %8.2f & %8.2f & %8.2f \\\\ \n', MainMat(k,:))
end
The output from this code is:
-273.15 & 0.00 & -459.67 & 0.00 \\
-40.00 & 233.15 & -40.00 & 419.67 \\
0.00 & 273.15 & 32.00 & 491.67 \\
100.00 & 373.15 & 212.00 & 671.67 \\
Generalized
If every entry in a row will have the same precision, you can also use
for ROW=1:size(MainMat, 1)
for COL=1:size(MainMat,2)-1
fprintf('%8.2f & ', MainMat(ROW,COL));
end
fprintf('%8.2f \\\\ \n', MainMat(ROW,end));
end
which will automatically determine the appropriate number of rows and columns for the matrix.
Extra Code
Horizontal Lines
If you want horizontal lines between rows, LaTeX needs the \hline command after the double-slashes. Keeping in mind that MATLAB needs to \ two produce a \, you can write
for k=1:length(TC)
fprintf('%8.2f & %8.2f & %8.2f & %8.2f \\\\ \\hline \n', MainMat(k,:))
end
to get:
-273.15 & 0.00 & -459.67 & 0.00 \\ \hline
-40.00 & 233.15 & -40.00 & 419.67 \\ \hline
0.00 & 273.15 & 32.00 & 491.67 \\ \hline
100.00 & 373.15 & 212.00 & 671.67 \\ \hline
Questions
Post your questions by editing the discussion page of this article. Edit the page, then scroll to the bottom and add a question by putting in the characters *{{Q}}, followed by your question and finally your signature (with four tildes, i.e. ~~~~). Using the {{Q}} will automatically put the page in the category of pages with questions - other editors hoping to help out can then go to that category page to see where the questions are. See the page for Template:Q for details and examples.