join - Combine strings - MATLAB (original) (raw)

Syntax

Description

[newStr](#btxr5ml-1-newStr) = join([str](#btxr5ml-1-str)) combines the text in str by joining consecutive elements of the input array, placing a space character between them. str can be a string array or a cell array of character vectors. newStr has the same data type as str.

For a string or cell array of any size, join concatenates elements along the last dimension of str with a size that does not equal 1.

example

[newStr](#btxr5ml-1-newStr) = join([str](#btxr5ml-1-str),[delimiter](#btxr5ml-1-delimiter)) combines the text in str and places the elements ofdelimiter between the elements of str instead of a space character.

If the delimiter argument is an array of different delimiters, and str has N elements along the dimension that is joined, then delimiter must have N–1 elements along the same dimension. The other dimensions ofdelimiter must have either a size of 1 or the same size as the corresponding dimension of str.

example

[newStr](#btxr5ml-1-newStr) = join([str](#btxr5ml-1-str),[dim](#btxr5ml-1-dim)) combines the elements in str along the dimensiondim.

example

[newStr](#btxr5ml-1-newStr) = join([str](#btxr5ml-1-str),[delimiter](#btxr5ml-1-delimiter),[dim](#btxr5ml-1-dim)) combines the elements in str along the dimensiondim and places the elements of delimiter between the elements of str.

Examples

collapse all

Create a string array. You can create strings using double quotes.

str = ["Carlos","Sada"; "Ella","Olsen"; "Diana","Lee"]

str = 3×2 string "Carlos" "Sada" "Ella" "Olsen" "Diana" "Lee"

Combine the strings using the join function. join concatenates the strings from str and places a space character between them. join concatenates along the second dimension, because it is the last dimension with a size that does not equal 1.

newStr = 3×1 string "Carlos Sada" "Ella Olsen" "Diana Lee"

Combine elements in a string array. Instead of spaces, insert different pieces of text between the strings in str.

Create a string array.

str = ["x","y","z"; "a","b","c"]

str = 2×3 string "x" "y" "z" "a" "b" "c"

Concatenate the strings with dashes between them.

newStr = 2×1 string "x-y-z" "a-b-c"

Concatenate the strings with symbols that make the output strings represent equations. The delimiters argument must be a 2-by-2 array because str is a 2-by-3 array.

delimiters = [" + "," = "; " - "," = "]; newStr = join(str,delimiters)

newStr = 2×1 string "x + y = z" "a - b = c"

Create a string array.

str = ["Carlos","Sada"; "Ella","Olsen"; "Diana","Lee"]

str = 3×2 string "Carlos" "Sada" "Ella" "Olsen" "Diana" "Lee"

Combine the strings in str along the first dimension. By default, the join function combines strings along the last dimension with a size that does not equal 1. To combine the strings along the first dimension, specify it as an additional input argument.

newStr = 1×2 string "Carlos Ella Diana" "Sada Olsen Lee"

Input Arguments

collapse all

Input text, specified as a string array or a cell array of character vectors.

Delimiting characters for joining strings, specified as a character vector, a cell array of character vectors, or a string array.join forms the output string array by combining string elements with delimiters between them.

join inserts all characters indelimiter as literal text, including escaped character sequences.

Dimension along which to join strings, specified as a positive integer. Ifdim is not specified, then the default is the last dimension with a size that does not equal 1.

Output Arguments

collapse all

Output text, returned as a string array or a cell array of character vectors. newStr has the same data type as the input text and has a size of 1 along the dimension being joined.

Extended Capabilities

expand all

Thejoin function fully supports tall arrays. For more information, see Tall Arrays.

Version History

Introduced in R2016b