time | Calls | line |
---|
| | 1 | function s = int2str(x)
|
| | 2 | %INT2STR Convert integer to string.
|
| | 3 | % S = INT2STR(X) rounds the elements of the matrix X to integers and
|
| | 4 | % converts the result into a string matrix.
|
| | 5 | % Return NaN and Inf elements as strings 'NaN' and 'Inf', respectively.
|
| | 6 | %
|
| | 7 | % See also NUM2STR, SPRINTF, FPRINTF, MAT2STR.
|
| | 8 |
|
| | 9 | % Copyright 1984-2010 The MathWorks, Inc.
|
| | 10 |
|
| | 11 | % only work with real portion of x
|
< 0.01 | 7 | 12 | x = real(x);
|
| | 13 |
|
| | 14 | % create a copy of x to use to calculate maximum width in digits
|
< 0.01 | 7 | 15 | widthCopy = x;
|
< 0.01 | 7 | 16 | if isfloat(x)
|
< 0.01 | 7 | 17 | x = 0+round(x); %remove negative zero
|
| | 18 | % replace Inf and NaN with a number of equivalent length for width
|
| | 19 | % calcultion
|
< 0.01 | 7 | 20 | widthCopy(~isfinite(widthCopy)) = 314;
|
| 7 | 21 | formatConversion = '.0f';
|
| | 22 | elseif isa(x, 'uint64')
|
| | 23 | formatConversion = 'lu';
|
| | 24 | else
|
| | 25 | formatConversion = 'ld';
|
| | 26 | end
|
| | 27 |
|
< 0.01 | 7 | 28 | if isempty(x)
|
| | 29 | s = '';
|
< 0.01 | 7 | 30 | elseif isscalar(x)
|
< 0.01 | 7 | 31 | s = sprintf(['%', formatConversion], x);
|
| | 32 | else
|
| | 33 | % determine the variable text field width quantity
|
| | 34 | widthMax = double(max(abs(widthCopy(:))));
|
| | 35 | if widthMax == 0
|
| | 36 | width = 3;
|
| | 37 | else
|
| | 38 | width = floor(log10(widthMax)) + 3;
|
| | 39 | end
|
| | 40 |
|
| | 41 | format = sprintf('%%%d%s', width, formatConversion);
|
| | 42 |
|
| | 43 | [rows, cols] = size(x);
|
| | 44 | s = char(zeros(rows, width*cols));
|
| | 45 | for row = 1:rows
|
| | 46 | % use vectorized version of sprintf for each row
|
| | 47 | s(row,:) = sprintf(format, x(row,:));
|
| | 48 | end
|
| | 49 |
|
| | 50 | % trim leading spaces from string array within constraints of rectangularity.
|
| | 51 | s = strtrim(s);
|
| | 52 | end
|
Other subfunctions in this file are not included in this listing.