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