comparison scripts/strings/mat2str.m @ 5838:376e02b2ce70

[project @ 2006-06-01 20:23:53 by jwe]
author jwe
date Thu, 01 Jun 2006 20:23:54 +0000
parents 55404f3b0da1
children 4b433225128d
comparison
equal deleted inserted replaced
5837:55404f3b0da1 5838:376e02b2ce70
47 ## @end example 47 ## @end example
48 ## 48 ##
49 ## @seealso{sprintf, int2str} 49 ## @seealso{sprintf, int2str}
50 ## @end deftypefn 50 ## @end deftypefn
51 51
52 function s = mat2str(x,n,cls) 52 function s = mat2str (x, n, cls)
53 53
54 if (nargin < 2 || isempty(n)) 54 if (nargin < 2 || isempty (n))
55 n = 17; # default precision 55 ## Default precision
56 n = 17;
56 endif 57 endif
57 58
58 if (nargin < 3) 59 if (nargin < 3)
59 if (ischar(n)) 60 if (ischar (n))
60 cls = n; 61 cls = n;
61 n = 17; 62 n = 17;
62 else 63 else
63 cls = ''; 64 cls = "";
64 endif 65 endif
65 endif 66 endif
66 67
67 if (nargin < 1 || nargin > 3 || ischar(x) || isstruct(x) || 68 if (nargin < 1 || nargin > 3 || ! isnumeric (x))
68 ischar(n) || isstruct(n) || isstruct(cls)) 69 print_usage ();
69 usage ("mat2str");
70 endif 70 endif
71 71
72 if (ndims (x) > 2) 72 if (ndims (x) > 2)
73 error ("mat2str: x must be two dimensional"); 73 error ("mat2str: X must be two dimensional");
74 endif 74 endif
75 75
76 if (!(COMPLEX = is_complex(x))) 76 x_is_complex = is_complex (x);
77 FMT = sprintf("%%.%dg", n(1)); 77
78 if (! x_is_complex)
79 fmt = sprintf ("%%.%dg", n(1));
78 else 80 else
79 if (length(n) == 1 ) 81 if (length (n) == 1 )
80 n = [n, n]; 82 n = [n, n];
81 endif 83 endif
82 FMT = sprintf("%%.%dg%%+.%dgi", n(1), n(2)); 84 fmt = sprintf ("%%.%dg%%+.%dgi", n(1), n(2));
83 endif 85 endif
84 86
85 [nr, nc] = size(x); 87 nel = numel (x);
86 88
87 if (nr*nc == 0) # empty .. only print brackets 89 if (nel == 0)
90 ## Empty, only print brackets
88 s = "[]"; 91 s = "[]";
89 elseif (nr*nc == 1) # scalar x .. don't print brackets 92 elseif (nel == 1)
90 if (!COMPLEX) 93 ## Scalar X, don't print brackets
91 s = sprintf( FMT, x ); 94 if (! x_is_complex)
95 s = sprintf (fmt, x);
92 else 96 else
93 s = sprintf( FMT, real(x), imag(x) ); 97 s = sprintf (fmt, real (x), imag (x));
94 endif 98 endif
95 else # non-scalar x .. print brackets 99 else
96 FMT = [FMT, ',']; 100 ## Non-scalar X, print brackets
97 if (!COMPLEX) 101 fmt = [fmt, ","];
98 s = sprintf( FMT, x.' ); 102 if (! x_is_complex)
103 s = sprintf (fmt, x.');
99 else 104 else
100 x = x.'; 105 x = x.';
101 s = sprintf( FMT, [ real(x(:))'; imag(x(:))' ] ); 106 s = sprintf (fmt, [real(x(:))'; imag(x(:))']);
102 endif 107 endif
103 108
104 s = ["[", s]; 109 s = ["[", s];
105 s (length(s)) = "]"; 110 s(end) = "]";
106 IND = find(s == ","); 111 ind = find (s == ",");
107 s (IND(nc:nc:length(IND)) ) = ";"; 112 s(ind(nc:nc:end)) = ";";
108 endif 113 endif
109 114
110 if (strcmp ("class", cls)) 115 if (strcmp ("class", cls))
111 s = [class(x), "(", s, ")"] 116 s = [class(x), "(", s, ")"]
112 endif 117 endif