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 1:
|
| | 15 | % num2str(randn(2,2),3) produces a string matrix such as
|
| | 16 | %
|
| | 17 | % 1.44 -0.755
|
| | 18 | % 0.325 1.37
|
| | 19 | %
|
| | 20 | % Example 2:
|
| | 21 | % num2str(rand(2,3) * 9999, '%10.5e\n') produces a string matrix
|
| | 22 | % such as
|
| | 23 | %
|
| | 24 | % 8.14642e+03
|
| | 25 | % 1.26974e+03
|
| | 26 | % 6.32296e+03
|
| | 27 | % 9.05701e+03
|
| | 28 | % 9.13285e+03
|
| | 29 | % 9.75307e+02
|
| | 30 | %
|
| | 31 | % See also INT2STR, SPRINTF, FPRINTF, MAT2STR.
|
| | 32 |
|
| | 33 | % Copyright 1984-2012 The MathWorks, Inc.
|
| | 34 | %------------------------------------------------------------------------------
|
| 140 | 35 | narginchk(1,2);
|
| 140 | 36 | if ischar(x)
|
| | 37 | s = x;
|
| | 38 | return;
|
| | 39 | end
|
0.01 | 140 | 40 | if isempty(x)
|
| | 41 | s = '';
|
| | 42 | return
|
| | 43 | end
|
| 140 | 44 | if isfloat(x)
|
| 140 | 45 | x = 0+x; % Remove negative zero
|
| 140 | 46 | end
|
| 140 | 47 | if issparse(x)
|
| | 48 | x = full(x);
|
| | 49 | end
|
| 140 | 50 | intFieldExtra = 1;
|
| 140 | 51 | maxFieldWidth = 12;
|
| 140 | 52 | floatWidthOffset = 4;
|
| 140 | 53 | forceWidth = 0;
|
| 140 | 54 | padColumnsWithSpace = true;
|
| | 55 | % Compose sprintf format string of numeric array.
|
| 140 | 56 | if nargin < 2
|
| | 57 | % To get the width of the elements in the output string
|
| 20 | 58 | widthCopy = x;
|
| | 59 | % replace Inf and NaN with a number of equivalent length (3 digits) for width
|
| | 60 | % calcultion
|
| 20 | 61 | if isfloat(x)
|
| 20 | 62 | widthCopy(~isfinite(widthCopy)) = 314; %This could be any 3 digit number
|
| 20 | 63 | end
|
| 20 | 64 | xmax = double(max(abs(widthCopy(:))));
|
| 20 | 65 | if isequaln(x, fix(x)) && (isinteger(x) || eps(xmax) <= 1)
|
| | 66 | if isreal(x)
|
| | 67 | s = int2str(x); % Enhance the performance
|
| | 68 | return;
|
| | 69 | end
|
| | 70 |
|
| | 71 | d = min(maxFieldWidth, floor(log10(xmax)) + 1);
|
| | 72 | forceWidth = d+intFieldExtra;
|
| | 73 | f = '%d';
|
| 20 | 74 | else
|
| | 75 | % The precision is unspecified; the numeric array contains floating point
|
| | 76 | % numbers.
|
| 20 | 77 | if xmax == 0
|
| | 78 | d = 1;
|
| 20 | 79 | else
|
| 20 | 80 | d = min(maxFieldWidth, max(1, floor(log10(xmax))+1))+floatWidthOffset;
|
| 20 | 81 | end
|
| | 82 |
|
| 20 | 83 | [s, forceWidth, f] = handleNumericPrecision(x, d);
|
| | 84 |
|
| 20 | 85 | if ~isempty(s)
|
| 20 | 86 | return;
|
| | 87 | end
|
| | 88 | end
|
| 120 | 89 | elseif isnumeric(f)
|
| | 90 | f = round(real(f));
|
| | 91 |
|
| | 92 | [s, forceWidth, f] = handleNumericPrecision(x, f);
|
| | 93 |
|
| | 94 | if ~isempty(s)
|
| | 95 | return;
|
| | 96 | end
|
| 120 | 97 | elseif ischar(f)
|
| | 98 | % Precision is specified as an ANSI C print format string.
|
| | 99 |
|
| | 100 | % Explicit format strings should be explicitly padded
|
| 120 | 101 | padColumnsWithSpace = false;
|
| | 102 |
|
| | 103 | % Validate format string
|
| 120 | 104 | k = strfind(f,'%');
|
| 120 | 105 | if isempty(k)
|
| | 106 | error(message('MATLAB:num2str:fmtInvalid', f));
|
| | 107 | end
|
| | 108 | else
|
| | 109 | error(message('MATLAB:num2str:invalidSecondArgument'))
|
| | 110 | end
|
| | 111 |
|
| | 112 | %-------------------------------------------------------------------------------
|
| | 113 | % Print numeric array as a string image of itself.
|
| | 114 |
|
| 120 | 115 | if isreal(x)
|
0.02 | 120 | 116 | [raw, isLeft] = cellPrintf(f, x, false);
|
| 120 | 117 | [m,n] = size(raw);
|
< 0.01 | 120 | 118 | cols = cell(1,n);
|
| 120 | 119 | widths = zeros(1,n);
|
< 0.01 | 120 | 120 | for j = 1:n
|
| 120 | 121 | if isLeft
|
| | 122 | cols{j} = char(raw(:,j));
|
| 120 | 123 | else
|
0.02 | 120 | 124 | cols{j} = strvrcat(raw(:,j));
|
| 120 | 125 | end
|
| 120 | 126 | widths(j) = size(cols{j}, 2);
|
| 120 | 127 | end
|
| | 128 | else
|
| | 129 | forceWidth = 2*forceWidth + 2;
|
| | 130 | raw = cellPrintf(f, real(x), false);
|
| | 131 | imagRaw = cellPrintf(f, imag(x), true);
|
| | 132 | [m,n] = size(raw);
|
| | 133 | cols = cell(1,n);
|
| | 134 | widths = zeros(1,n);
|
| | 135 | for j = 1:n
|
| | 136 | cols{j} = [strvrcat(raw(:,j)) char(imagRaw(:,j))];
|
| | 137 | widths(j) = size(cols{j}, 2);
|
| | 138 | end
|
| | 139 | end
|
| | 140 |
|
| 120 | 141 | maxWidth = max([widths forceWidth]);
|
| 120 | 142 | padWidths = maxWidth - widths;
|
| 120 | 143 | padIndex = find(padWidths, 1);
|
| 120 | 144 | while ~isempty(padIndex)
|
| | 145 | padWidth = padWidths(padIndex);
|
| | 146 | padCols = (padWidths==padWidth);
|
| | 147 | padWidths(padCols) = 0;
|
| | 148 | spaceCols = char(ones(m,padWidth)*' ');
|
| | 149 | cols(padCols) = strcat({spaceCols}, cols(padCols));
|
| | 150 | padIndex = find(padWidths, 1);
|
| | 151 | end
|
| | 152 |
|
| 120 | 153 | if padColumnsWithSpace
|
| | 154 | spaceCols = char(ones(m,1)*' ');
|
| | 155 | cols = strcat(cols, {spaceCols});
|
| | 156 | end
|
| | 157 |
|
< 0.01 | 120 | 158 | s = strtrim([cols{:}]);
|
| 120 | 159 | end
|