This is a static copy of a profile reportHome
num2str (6 calls, 0.016 sec)
Generated 15-Mar-2007 12:01:49 using real time.
M-function in file C:\Program Files\MATLAB71\toolbox\matlab\strfun\num2str.m
[Copy to new window for comparing multiple runs]
Parents (calling functions)
Lines where the most time was spent
Line Number | Code | Calls | Total Time | % Time | Time Plot |
51 | s = int2str(x); | 6 | 0.016 s | 100.0% |  |
41 | maxDigitsOfPrecision = 256; | 6 | 0.000 s | 0.0% |  |
45 | floatWidthOffset = 4; | 6 | 0.000 s | 0.0% |  |
43 | intFieldExtra = 2; | 6 | 0.000 s | 0.0% |  |
44 | maxFieldWidth = 12; | 6 | 0.000 s | 0.0% |  |
Other lines & overhead | | | 0 s | 0% |  |
Totals | | | 0.016 s | 100% | |
Children (called functions)
Function Name | Function Type | Calls | Total Time | % Time | Time Plot |
int2str | M-function | 6 | 0 s | 0% |  |
Self time (built-ins, overhead, etc.) | | | 0.016 s | 100.0% |  |
Totals | | | 0.016 s | 100% | |
M-Lint results
Line number | Message |
161 | Constructing a cell array is faster than using STRVCAT. |
Coverage results
[ Show coverage for parent directory ]
Total lines in file | 173 |
Non-code lines (comments, blank lines) | 69 |
Code lines (lines that can run) | 104 |
Code lines that did run | 12 |
Code lines that did not run | 92 |
Coverage (did run/can run) | 11.54 % |
Function listing
time calls line
1 function s = num2str(x, f)
2 %NUM2STR Convert numbers to a string.
3 % T = NUM2STR(X) converts the matrix X into a string representation T
4 % with about 4 digits and an exponent if required. This is useful for
5 % labeling plots with the TITLE, XLABEL, YLABEL, and TEXT commands.
6 %
7 % T = NUM2STR(X,N) converts the matrix X into a string representation
8 % with a maximum N digits of precision. The default number of digits is
9 % based on the magnitude of the elements of X.
10 %
11 % T = NUM2STR(X,FORMAT) uses the format string FORMAT (see SPRINTF for
12 % details).
13 %
14 % Example:
15 % num2str(randn(2,2),3) produces the string matrix
16 %
17 % '-0.433 0.125'
18 % ' -1.67 0.288'
19 %
20 % See also INT2STR, SPRINTF, FPRINTF, MAT2STR.
21
22 % Copyright 1984-2005 The MathWorks, Inc.
23 % $Revision: 5.32.4.9 $ $Date: 2005/06/21 19:40:38 $
24
25 %------------------------------------------------------------------------------
26 % if input does not exist or is empty, throw an exception
6 27 if nargin<1
28 error('MATLAB:num2str:NumericArrayUnspecified',...
29 'Numeric array is unspecified')
30 end
31 % If input is a string, return this string.
6 32 if ischar(x)
33 s = x;
34 return
35 end
6 36 if isempty(x)
37 s = '';
38 return
39 end
40
< 0.01 6 41 maxDigitsOfPrecision = 256;
< 0.01 6 42 floatFieldExtra = 7;
< 0.01 6 43 intFieldExtra = 2;
< 0.01 6 44 maxFieldWidth = 12;
< 0.01 6 45 floatWidthOffset = 4;
46
47 % Compose sprintf format string of numeric array.
6 48 if nargin < 2 && ~isempty(x) && all(x(:)==fix(x(:)))
6 49 if isreal(x)
50 % The precision is unspecified; the numeric array contains whole numbers.
0.02 6 51 s = int2str(x);
6 52 return;
53 else
54 %Complex case
55 % maximum field width is 12 digits
56 xmax = double(max(abs(x(:))));
57 if xmax == 0
58 d = 1;
59 else
60 d = min(maxFieldWidth, floor(log10(xmax)) + 1);
61 end
62
63 % Create ANSI C print format string.
64 f = ['%' sprintf('%d',d+intFieldExtra) 'd']; % real numbers
65 fi = ['%-' sprintf('%d',d+intFieldExtra) 's']; % imaginary numbers
66 end
67 elseif nargin < 2
68 % The precision is unspecified; the numeric array contains floating point
69 % numbers.
70 xmax = double(max(abs(x(:))));
71 if xmax == 0
72 d = 1;
73 else
74 d = min(maxFieldWidth, max(1, floor(log10(xmax))+1))+floatWidthOffset;
75 end
76
77 % Create ANSI C print format string.
78 % real numbers
79 f = ['%' sprintf('%.0f',d+floatFieldExtra) '.' sprintf('%.0f',d) 'g'];
80 % imaginary numbers
81 fi = ['%-' sprintf('%.0f',d+floatFieldExtra) 's'];
82
83 elseif ~ischar(f)
84 % Precision is specified, not as ANSI C format string, but as a number.
85 % Windows gets a segmentation fault at around 512 digits of precision,
86 % as if it had an internal buffer that cannot handle more than 512 digits
87 % to the RIGHT of the decimal point. Thus, allow half of the windows buffer
88 % of digits of precision, as it should be enough for most computations.
89 % Large numbers of digits to the LEFT of the decimal point seem to be allowed.
90 if f > maxDigitsOfPrecision
91 error('MATLAB:num2str:exceededMaxDigitsOfPrecision', ...
92 'Exceeded maximum %d digits of precision.',maxDigitsOfPrecision);
93 end
94
95 % Create ANSI C print format string
96 fi = ['%-' sprintf('%.0f',f+floatFieldExtra) 's'];
97 f = ['%' sprintf('%.0f',f+floatFieldExtra) '.' int2str(f) 'g'];
98 else
99 % Precistion is specified as an ANSI C print format string.
100 % Validate format string
101 k = strfind(f,'%');
102 if isempty(k), error('MATLAB:num2str:fmtInvalid', ...
103 '''%s'' is an invalid format.',f);
104 end
105 % If digits of precision to the right of the decimal point are specified,
106 % make sure it will not cause a segmentation fault under Windows.
107 dotPositions = strfind(f,'.');
108 if ~isempty(dotPositions)
109 decimalPosition = find(dotPositions > k(1)); % dot to the right of '%'
110 if ~isempty(decimalPosition)
111 digitsOfPrecision = sscanf(f(dotPositions(decimalPosition(1))+1:end),'%d');
112 if digitsOfPrecision > maxDigitsOfPrecision
113 error('MATLAB:num2str:exceededMaxDigitsOfPrecision', ...
114 'Exceeded maximum %d digits of precision.',maxDigitsOfPrecision);
115 end
116 end
117 end
118 d = sscanf(f(k(1)+1:end),'%f');
119 fi = ['%-' int2str(d) 's'];
120 end
121 %-------------------------------------------------------------------------------
122 % Print numeric array as a string image of itself.
123 [m,n] = size(x);
124 scell = cell(1,m);
125 xIsReal = isreal(x);
126 for i = 1:m,
127 t = [];
128 if xIsReal && (min(x(i,:)) >= 0) && (max(x(i,:)) < 2^31-1)
129 t = sprintf(f,x(i,:));
130 else
131
132 for j = 1:n,
133 u0 = sprintf(f, real(x(i,j)));
134 % we add a space infront of the negative sign
135 % because Win32 version of sprintf does not.
136 if (j>1) && u0(1)=='-'
137 u = [' ' u0];
138 else
139 u = u0;
140 end
141 % If we are printing integers and have overflowed, then
142 % add in an extra space.
143 if (real(x(i,j)) > 2^31-1) && (~isempty(findstr(f,'d')))
144 u = [' ' u];
145 end
146 if ~xIsReal
147 if imag(x(i,j)) == 0,
148 u = [u '+' formatimag(f,fi,0)];
149 elseif imag(x(i,j)) > 0
150 u = [u '+' formatimag(f,fi,imag(x(i,j)))];
151 elseif imag(x(i,j)) < 0
152 u = [u '-' formatimag(f,fi,-imag(x(i,j)))];
153 end
154 end
155 t = [t u];
156 end
157 end
158 scell{i} = t;
159 end
160 if m > 1
161 s = strvcat(scell{:});
162 else
163 s = t;
164 end
165 % Remove leading blanks
166 % If it's a scalar remove the trailing blanks too.
167 if length(x)==1,
168 s = strrep(s,' ','');
169 else
170 s = lefttrim(s);
171 end
172
173 %------------------------------------------------------------------------------
Other subfunctions in this file are not included in this listing.