time | Calls | line |
---|
| | 1 | function s = compose(fmt, varargin)
|
| | 2 | %COMPOSE Converts data into formatted string arrays
|
| | 3 | % STR = COMPOSE(TXT) translates the escape-character sequences in the
|
| | 4 | % input text TXT and leaves all other characters unchanged.
|
| | 5 | % Escape-character sequences represent nonprinting characters such as
|
| | 6 | % newlines or tabs.
|
| | 7 | %
|
| | 8 | % STR = COMPOSE(FORMAT, A1, ..., AN) formats values from A1, ..., AN
|
| | 9 | % using formatting operators specified by FORMAT and returns the
|
| | 10 | % resulting text in STR. COMPOSE formats the values from A1, ..., AN in
|
| | 11 | % column order.
|
| | 12 | %
|
| | 13 | % FORMAT can be a string array, character vector, or cell array of
|
| | 14 | % character vectors. If FORMAT is a string array then STR is a string
|
| | 15 | % array. Otherwise, STR is a cell array of character vectors.
|
| | 16 | %
|
| | 17 | % * If A1, ..., AN have multiple rows, COMPOSE repeats FORMAT in each
|
| | 18 | % row of STR, with formatted values from the corresponding row of
|
| | 19 | % A1, ..., AN.
|
| | 20 | % * If the number of columns in A1, ..., AN exceed the number of
|
| | 21 | % operators in FORMAT, COMPOSE repeats FORMAT as an additional column
|
| | 22 | % of STR. The extra columns of A1, ..., AN contribute formatted values
|
| | 23 | % to the new column of strings in STR.
|
| | 24 | % * If the number of columns in A1, ..., AN are less than the number of
|
| | 25 | % operators in FORMAT, then COMPOSE does not format values using those
|
| | 26 | % operators. Instead, COMPOSE leaves formatting operators in STR.
|
| | 27 | %
|
| | 28 | % Conversion operators with the processing-order identifier (n$ syntax)
|
| | 29 | % and * field width are not supported.
|
| | 30 | %
|
| | 31 | % Example:
|
| | 32 | % TXT = "The quick brown fox jumps";
|
| | 33 | % TXT = TXT + '\n' + 'over the lazy dog.';
|
| | 34 | % compose(TXT)
|
| | 35 | %
|
| | 36 | % returns
|
| | 37 | %
|
| | 38 | % "The quick brown fox jumps
|
| | 39 | % over the lazy dog."
|
| | 40 | %
|
| | 41 | % Example:
|
| | 42 | % F = "pi = %.5f";
|
| | 43 | % compose(F, pi)
|
| | 44 | %
|
| | 45 | % returns
|
| | 46 | %
|
| | 47 | % "pi = 3.14159"
|
| | 48 | %
|
| | 49 | % Example:
|
| | 50 | % F = "pi = %.2f, e = %.5f";
|
| | 51 | % compose(F, [pi,exp(1)])
|
| | 52 | %
|
| | 53 | % returns
|
| | 54 | %
|
| | 55 | % "pi = 3.14, e = 2.71828"
|
| | 56 | %
|
| | 57 | % Example:
|
| | 58 | % F = "real = %3.2f, imag = %3.2f";
|
| | 59 | % A = [4+6i;2.3+5.7i;6.1+2i;0.5+7i];
|
| | 60 | % compose(F, [real(A),imag(A)])
|
| | 61 | %
|
| | 62 | % returns
|
| | 63 | %
|
| | 64 | % "real = 4.00, imag = 6.00"
|
| | 65 | % "real = 2.30, imag = 5.70"
|
| | 66 | % "real = 6.10, imag = 2.00"
|
| | 67 | % "real = 0.50, imag = 7.00"
|
| | 68 | %
|
| | 69 | % Example:
|
| | 70 | % F = 'The time is %d:%d';
|
| | 71 | % A = [8,15,9,30;12,23,11,46];
|
| | 72 | % compose(F,A)
|
| | 73 | %
|
| | 74 | % returns
|
| | 75 | %
|
| | 76 | % 'The time is 8:15' 'The time is 9:30'
|
| | 77 | % 'The time is 12:23' 'The time is 11:46'
|
| | 78 | %
|
| | 79 | % See also SPRINTF, NEWLINE, STRING, STRING/PLUS
|
| | 80 |
|
| | 81 | % Copyright 2015-2017 The MathWorks, Inc.
|
| | 82 |
|
< 0.001 | 24 | 83 | narginchk(1, Inf);
|
< 0.001 | 24 | 84 | if ~isTextStrict(fmt)
|
| | 85 | firstInput = getString(message('MATLAB:string:FirstInput'));
|
| | 86 | error(message('MATLAB:string:MustBeCharCellArrayOrString', firstInput));
|
< 0.001 | 24 | 87 | end
|
| | 88 |
|
< 0.001 | 24 | 89 | try
|
< 0.001 | 24 | 90 | s = string(fmt);
|
< 0.001 | 24 | 91 | s = s.compose(varargin{:});
|
| | 92 |
|
< 0.001 | 24 | 93 | if ~isstring(fmt)
|
< 0.001 | 24 | 94 | s = cellstr(s);
|
< 0.001 | 24 | 95 | end
|
| | 96 |
|
| | 97 | catch E
|
| | 98 | throw(E);
|
< 0.001 | 24 | 99 | end
|
< 0.001 | 24 | 100 | end
|
Other subfunctions in this file are not included in this listing.