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

Syntax

Description

str = append([str1,...,strN](#mw%5F00ce9125-6d45-494b-8ff9-92717c065c6c)) combines the text from str1,...,strN. Each input argument can be a string array, a character vector, or a cell array of character vectors.

Unlike the strcat function, append preserves trailing whitespace characters from input arguments of all data types.

example

Examples

collapse all

Create two strings.

str1 = "Good"; str2 = "Morning";

Combine them using the append function.

To add a space between the input strings, specify a space character as another input argument.

str = append(str1,' ',str2)

As an alternative, you can use the plus operator to combine strings.

However, the best practice is to use append when you do not know whether the input arguments are strings, character vectors, or cell arrays of character vectors.

Create two character vectors, with the first character vector having a trailing whitespace character.

chr1 = 'Hello '; chr2 = 'World';

Combine them into one character vector.

The append function always preserves trailing whitespace characters, unlike the strcat function. (strcat removes trailing whitespace characters from character vectors.)

You can combine string arrays or cell arrays of character vectors, element by element. Also, you can append a single piece of text to the elements of an input array.

Create an array of file names.

names = ["data" "report" "slides"]

names = 1×3 string "data" "report" "slides"

Create an array of file extension names, with a different extension for each element of names.

ext = [".xlsx" ".docx" ".pptx"]

ext = 1×3 string ".xlsx" ".docx" ".pptx"

Combine the file names and extensions.

str1 = 1×3 string "data.xlsx" "report.docx" "slides.pptx"

To append the same extension to each name, use a character vector or a string scalar.

str2 = append(names,'.mat')

str2 = 1×3 string "data.mat" "report.mat" "slides.mat"

The append function supports implicit expansion of arrays. For example, you can combine strings from a column vector and a row vector to form a two-dimensional string array.

Create a column vector of strings. Then create a row vector.

str1 = 3×1 string "A" "B" "C"

str2 = 1×4 string "1" "2" "3" "4"

Combine str1 and str2.

str = 3×4 string "A1" "A2" "A3" "A4" "B1" "B2" "B3" "B4" "C1" "C2" "C3" "C4"

Input Arguments

collapse all

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

The append function supports input arguments that have_compatible sizes_.

String arrays and cell arrays of character vectors have compatible sizes if, for each dimension, one of these conditions is true:

Character vectors are always compatible with all other input arguments. You can always append a character vector to another character vector, or to the elements of a string array or cell array of character vectors.

For more information on combining arrays with compatible sizes, see Compatible Array Sizes for Basic Operations.

Extended Capabilities

expand all

Usage notes and limitations:

Version History

Introduced in R2019a