compose
Formats values into text using a format with placeholders.
Category: Language Fundamentals > Data Types
Syntax
str = compose(formatSpec)
Processes escape sequences and returns a string.
str = compose(formatSpec, A1, ..., An)
Inserts the values using the format.
str = compose(formatSpec, A)
With a single array A and one placeholder, returns one string per element.
Description
compose is a vectorized sprintf: it formats once per element instead of concatenating everything into one line. compose('%.2f', [1.234; 5.6789]) returns a column of two strings.
The result is a string array, not a char vector, so it can be combined with + and used with the string functions.
Input arguments
formatSpec- Text with operators such as
%d,%f,%s, as insprintf. May be a string array. A1, ..., An- Values to insert.
Output arguments
str- String array.
Example
One string per element
compose('x=%d', [1 2])
compose('%.2f', [1.234; 5.6789])
compose("Value %d of %s", 3, "x")
Output
ans = 2×1 string array
"x=1"
"x=2"
ans = 2×1 string array
"1.23"
"5.68"
ans = "Value 3 of x"