NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/util/latexmat.m

Revision 495 (checked in by cbc, 12 years ago)

Initial import of Stark code.

Line 
1 function str = latexmat(mat, format)
2 %LATEXMAT Generate LaTeX code for a matrix.
3 %
4 %   STR = LATEX(MAT, FORMAT) return the LaTeX code for the matrix MAT,
5 %   with the given FORMAT.
6 %
7 %   See HELP SPRINTF for more details about the FORMAT parameter.
8
9 %   Author:      Peter J. Acklam
10 %   Time-stamp:  2000-02-29 23:27:59
11 %   E-mail:      jacklam@math.uio.no
12 %   WWW URL:     http://www.math.uio.no/~jacklam
13
14    error(nargchk(2, 2, nargin));
15
16    if ischar(mat)
17       error('First argument can not be a string.');
18    end
19
20    if ~ischar(format)
21       error('Second argument must be a string.');
22    end
23
24    [ r, c ] = size(mat);
25
26    newline = sprintf('\n');
27
28    str = [ '\left[ \begin{array}{' ...
29                char(abs('c')*ones(1,c)) '}' newline ];
30
31    for i = 1:r
32       str = [ str ' ' ];
33       for j = 1:c
34          t = sprintf(format, real(mat(i,j)));
35          if (imag(mat(i,j)) > 0)
36             t = [ t '+' sprintf(format, imag(mat(i,j))) 'i' ];
37          elseif (imag(mat(i,j)) < 0)
38             t = [ t '-' sprintf(format, -imag(mat(i,j))) 'i' ];
39          end
40          str = [ str ' ' t ];
41          if j < c
42             str = [ str ' &' ];
43          else
44             if i < r
45                str = [ str ' \\' newline ];
46             else
47                str = [ str newline ];
48             end
49          end
50       end
51    end
52    str = [ str '\end{array} \right]' newline ];
Note: See TracBrowser for help on using the browser.