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