Compose String
Compose output string signal based on specified format and input signals
Libraries:
Simulink /
String
Description
The Compose String block composes output string signal based on the
format specifier listed in the Format parameter. The
Format parameter determines the number of input signals. If
there are multiple inputs, the block constructs the string by combining these multiple
inputs in order, and applying the associated format specifier, one format specifier for
each input. Each format specifier starts with a percent sign, %
,
followed by the conversion character. For example, %f
formats the
input as a floating point output. To supplement the string output, you can also add a
character to the format specification. Use this block to compose and format an output
string signal from a multiple inputs.
For example, if the Format parameter contains "%s is
%f"
, the block expects two inputs, a string signal and a single or double
signal. If the first input is the string "Pi"
and the second input is
a double value 3.14
, the output is "Pi is
3.14"
.
When a MinGW® compiler compiles code generated from the block, running the compiled code
may produce nonstandard results for floating-point inputs. For example, a numeric input
of 501.987
returns the string "5.019870e+002"
instead of the expected string "5.019870e+02"
.
Ports
Input
d — Data for first part of string
scalar
Data for the first part of string, specified as a scalar. The
Format parameter determines the port label and
the format of the input data. For example, if the first item in the
Format parameter is %d
, the
port label is d.
The data type of the input signal must be compatible with the format specifier in the Format parameter. For more information, see the Format parameter.
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
| Boolean
f — Data for second part of string
scalar
Data for the second part of string, specified as a scalar. The
Format parameter determines the port label and
the format of the input data. For example, if the first item in the
Format parameter is %f
, the
port label is f.
The data type of the input signal must be compatible with the format specifier specified in the Format parameter. For more information, see the Format parameter.
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
| Boolean
| fixed point
| enumerated
| bus
Port_N — Data for N parts of string
scalar (default)
Data for N
parts of string, specified as a
scalar. The Format parameter determines the port
label and the format of the input data. For example, if the
corresponding item in the Format parameter is
%f
, the port label is f.
The data type of the input signal must be compatible with the format specifier in the Format parameter. For more information, see the Format parameter.
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
| Boolean
| fixed point
| enumerated
| bus
Output
str — Output string
scalar
Output string composed of inputs, specified as a scalar.
Data Types: string
Parameters
Format — Format input data
"%d %f" (default) | format string | character vector
Format of input data, specified as a format string.
For more information about acceptable format specifiers, see Algorithms.
Programmatic Use
Block Parameter:
Format |
Type: character vector |
Values:
'<filename>' |
Default:
'"%d %f"' |
Output data type — Output data type
string
(default) | <data type expression>
Output data type, specified using the string data type to specify a string with no maximum length.
To specify a string data type with a maximum length, specify
stringtype(
. For
example, N
)stringtype(31)
creates a string data type with a
maximum length of 31 characters.
Click the Show data type assistant button to display the Data Type Assistant, which helps you set the data type attributes. See Specify Data Types Using Data Type Assistant for more information.
Programmatic Use
Block Parameter:
OutDataTypeStr |
Type: character vector |
Values:
'string' | <data type
expression> |
Default:
'string' |
Mode — Category of data
string
(default) | scalar
Use the stringtype
function, for example,
stringtype(255)
.
Dependency
Clicking the Show data type assistant button enables this parameter.
Block Characteristics
Data Types |
|
Direct Feedthrough |
|
Multidimensional Signals |
|
Variable-Size Signals |
|
Zero-Crossing Detection |
|
Algorithms
A formatting specifier starts with a percent sign, %
, and ends with
a conversion character. The conversion character is required. Optionally, you can
specify identifier, flags, field width, precision, and subtype specifiers between
%
and the conversion character. (Spaces are invalid between
specifiers and are shown here only for readability).
The Compose String block uses this format specifier prototype:
%[flags][width][.precision][length]specifier
Conversion Character
This table shows conversion characters to format numeric and character data as text.
Value Type | Conversion | Details |
---|---|---|
Integer, signed |
| Base 10 |
Integer, unsigned |
| Base 10 |
| Base 8 (octal) | |
| Base 16 (hexadecimal), lowercase letters
| |
| Same as | |
Floating-point number |
| Floating-point notation (Use a precision operator to specify the number of digits after the decimal point.) |
| Exponential notation, such as
| |
| Same as | |
| The more compact of | |
| The more compact of | |
String |
| The type of the output text is the same as the type of Format. |
Optional Operators
The optional identifier, flags, field width, precision, and operators further define the format of the output text.
Flags
'–'
Left-justify. Works with all specifiers.
Example:%-5.2f
Example:%-10s
'+'
Always print a sign character (+ or –) for any numeric value. Works with all specifiers except
u
,o
,x
,X
, ands
.
Example:%+5.2f
Right-justify text.
Example:%+10s
' '
Insert a space before the value. Works with all specifiers except
u
,o
,x
,X
, ands
.Example:
% 5.2f
'0'
Pad to field width with zeros before the value. Works with all specifiers except
s
.
Example:%05.2f
'#'
Modify selected numeric conversions:
For
%o
,%x
, or%X
, print0
,0x
, or0X
prefix.For
%f
,%e
, or%E
, print decimal point even when precision is 0.For
%g
or%G
, do not remove trailing zeros or decimal point.
Works with all specifiers except
d
,i
,u
, ands
.Example:
%#5.0f
Field Width
Minimum number of characters to print.
The function pads to field width with spaces before the value unless otherwise specified by flags.
Precision
For
%f
,%e
, or%E
Number of digits to the right of the decimal point
Example:'%.4f'
printspi
as'3.1416'
d, i, o, u, x, X
Minimum number of digits to be written. Outputs shorter than the specified precision are padded with leading zeros.
Example:"%.4d"
prints 5 as'0005'
For
%g
or%G
Number of significant digits
Example:'%.4g'
printspi
as'3.142'
s
Maximum number of characters to be written to the output.
Example:"%.2s"
prints"Hello!"
as"He"
Note
If you specify a precision operator for floating-point values that exceeds the precision of the input numeric data type, the results might not match the input values to the precision you specified. The result depends on your computer hardware and operating system.
Text Before or After Formatting Operators
Special Character | Representation |
---|---|
Single quotation mark |
|
Percent character |
|
Backslash |
|
Alarm |
|
Backspace |
|
Form feed |
|
New line |
|
Carriage return |
|
Horizontal tab |
|
Vertical tab |
|
Character whose Unicode® numeric value can be represented by the
hexadecimal number, |
Example:
|
Character whose Unicode numeric value can be represented by the octal
number, |
Example:
|
Format can also include additional text before a percent
sign, %
, or after a conversion character. The text can be:
Ordinary text to print.
Special characters that you cannot enter as ordinary text. This table shows how to represent special characters in
formatSpec
.
Length Specifiers
The Format String block supports the h
and
l
length subspecifiers. These specifiers can change according
to the Configuration Parameters > Hardware Implementation > Number of bits settings
Length | d i | u o x X | f e E g G | s |
---|---|---|---|---|
No length specifier | int | unsigned int | double (default),
single | string |
h | short | unsigned short | — | — |
l | long | unsigned long | — | — |
Note for Specifiers That Specify Integer Data Types (d
, i
, u
, o
,
x
, X
)
Target int
, long
, and
short
type sizes are controlled by settings in the Configuration Parameters > Hardware Implementation pane. For example, if the target int
is 32 bits and
the specifier is %u
, then the expected input type will be
uint32
. However, the input port accepts any built-in integer
type of that size or smaller with the %u
specifier
Notes for Specifiers That Specify Floating Point Data Types (f
, e
, E
,
g
, F
)
Do not use
l
andh
with these specifiers. Do not use the length subspecifier (for example,%f
is allowed, but%hf
and%lf
are not allowed).Ports that correspond with these specifiers accept both
single
anddouble
data types.
Note for Specifiers That Specify the String Data Type (s)
The
s
specifier does not work with thel
orh
subspecifiers, and only accepts a string input data type.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Version History
Introduced in R2018a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)