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