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.002 | 114 | 35 | if nargin > 0
|
< 0.001 | 114 | 36 | x = convertStringsToChars(x);
|
< 0.001 | 114 | 37 | end
|
| | 38 |
|
< 0.001 | 114 | 39 | if nargin > 1
|
< 0.001 | 56 | 40 | f = convertStringsToChars(f);
|
< 0.001 | 114 | 41 | end
|
| | 42 |
|
< 0.001 | 114 | 43 | narginchk(1,2);
|
< 0.001 | 114 | 44 | if ischar(x)
|
| | 45 | s = x;
|
| | 46 | return;
|
< 0.001 | 114 | 47 | end
|
< 0.001 | 114 | 48 | if isempty(x)
|
| | 49 | s = '';
|
| | 50 | return
|
< 0.001 | 114 | 51 | end
|
< 0.001 | 114 | 52 | if ~isnumeric(x) && ~islogical(x)
|
| | 53 | error(message('MATLAB:num2str:nonNumericInput') );
|
< 0.001 | 114 | 54 | end
|
0.001 | 114 | 55 | if isfloat(x)
|
< 0.001 | 114 | 56 | x = 0+x; % Remove negative zero
|
< 0.001 | 114 | 57 | end
|
< 0.001 | 114 | 58 | if issparse(x)
|
| | 59 | x = full(x);
|
< 0.001 | 114 | 60 | end
|
< 0.001 | 114 | 61 | intFieldExtra = 1;
|
< 0.001 | 114 | 62 | maxFieldWidth = 12;
|
< 0.001 | 114 | 63 | floatWidthOffset = 4;
|
< 0.001 | 114 | 64 | forceWidth = 0;
|
< 0.001 | 114 | 65 | padColumnsWithSpace = true;
|
| | 66 | % Compose sprintf format string of numeric array.
|
| | 67 |
|
< 0.001 | 114 | 68 | if nargin < 2 || (isinteger(x) && isnumeric(f))
|
| | 69 |
|
| | 70 | % To get the width of the elements in the output string
|
< 0.001 | 58 | 71 | widthCopy = x;
|
| | 72 | % replace Inf and NaN with a number of equivalent length (3 digits) for width
|
| | 73 | % calcultion
|
< 0.001 | 58 | 74 | if isfloat(x)
|
< 0.001 | 58 | 75 | widthCopy(~isfinite(widthCopy)) = 314; %This could be any 3 digit number
|
< 0.001 | 58 | 76 | end
|
< 0.001 | 58 | 77 | xmax = double(max(abs(widthCopy(:))));
|
0.001 | 58 | 78 | if isequaln(x, fix(x)) && (isinteger(x) || eps(xmax) <= 1)
|
| | 79 | if isreal(x)
|
| | 80 | s = int2str(x); % Enhance the performance
|
| | 81 | return;
|
| | 82 | end
|
| | 83 |
|
| | 84 | d = min(maxFieldWidth, floor(log10(xmax)) + 1);
|
| | 85 | forceWidth = d+intFieldExtra;
|
| | 86 | f = '%d';
|
< 0.001 | 58 | 87 | else
|
| | 88 | % The precision is unspecified; the numeric array contains floating point
|
| | 89 | % numbers.
|
< 0.001 | 58 | 90 | if xmax == 0
|
| | 91 | d = 1;
|
< 0.001 | 58 | 92 | else
|
< 0.001 | 58 | 93 | d = min(maxFieldWidth, max(1, floor(log10(xmax))+1))+floatWidthOffset;
|
< 0.001 | 58 | 94 | end
|
| | 95 |
|
0.013 | 58 | 96 | [s, forceWidth, f] = handleNumericPrecision(x, d);
|
| | 97 |
|
< 0.001 | 58 | 98 | if ~isempty(s)
|
< 0.001 | 58 | 99 | return;
|
| | 100 | end
|
| | 101 | end
|
< 0.001 | 56 | 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
|
< 0.001 | 56 | 110 | elseif ischar(f)
|
| | 111 | % Precision is specified as an ANSI C print format string.
|
| | 112 |
|
| | 113 | % Explicit format strings should be explicitly padded
|
< 0.001 | 56 | 114 | padColumnsWithSpace = false;
|
| | 115 |
|
| | 116 | % Validate format string
|
< 0.001 | 56 | 117 | k = strfind(f,'%');
|
< 0.001 | 56 | 118 | if isempty(k)
|
| | 119 | error(message('MATLAB:num2str:fmtInvalid', f));
|
< 0.001 | 56 | 120 | end
|
| | 121 | else
|
| | 122 | error(message('MATLAB:num2str:invalidSecondArgument'))
|
< 0.001 | 56 | 123 | end
|
| | 124 |
|
| | 125 | %-------------------------------------------------------------------------------
|
| | 126 | % Print numeric array as a string image of itself.
|
| | 127 |
|
< 0.001 | 56 | 128 | if isreal(x)
|
0.008 | 56 | 129 | [raw, isLeft] = cellPrintf(f, x, false);
|
< 0.001 | 56 | 130 | [m,n] = size(raw);
|
< 0.001 | 56 | 131 | cols = cell(1,n);
|
< 0.001 | 56 | 132 | widths = zeros(1,n);
|
< 0.001 | 56 | 133 | for j = 1:n
|
< 0.001 | 56 | 134 | if isLeft
|
| | 135 | cols{j} = char(raw(:,j));
|
< 0.001 | 56 | 136 | else
|
0.013 | 56 | 137 | cols{j} = strvrcat(raw(:,j));
|
< 0.001 | 56 | 138 | end
|
< 0.001 | 56 | 139 | widths(j) = size(cols{j}, 2);
|
< 0.001 | 56 | 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
|
< 0.001 | 56 | 152 | end
|
| | 153 |
|
< 0.001 | 56 | 154 | maxWidth = max([widths forceWidth]);
|
< 0.001 | 56 | 155 | padWidths = maxWidth - widths;
|
< 0.001 | 56 | 156 | padIndex = find(padWidths, 1);
|
< 0.001 | 56 | 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 |
|
< 0.001 | 56 | 166 | if padColumnsWithSpace
|
| | 167 | spaceCols = char(ones(m,1)*' ');
|
| | 168 | cols = strcat(cols, {spaceCols});
|
< 0.001 | 56 | 169 | end
|
| | 170 |
|
< 0.001 | 56 | 171 | s = strtrim([cols{:}]);
|
< 0.001 | 56 | 172 | end
|
Other subfunctions in this file are not included in this listing.