Next: , Up: Creating Strings   [Contents][Index]


5.3.1 Concatenating Strings

Strings can be concatenated using matrix notation (see Strings, Character Arrays) which is often the most natural method. For example:

fullname = [fname ".txt"];
email = ["<" user "@" domain ">"];

In each case it is easy to see what the final string will look like. This method is also the most efficient. When using matrix concatenation the parser immediately begins joining the strings without having to process the overhead of a function call and the input validation of the associated function.

Nevertheless, there are several other functions for concatenating string objects which can be useful in specific circumstances: char, strvcat, strcat, and cstrcat. Finally, the general purpose concatenation functions can be used: see cat, horzcat, and vertcat.

: char (x)
: char (x, …)
: char (s1, s2, …)
: char (cell_array)

Create a string array from one or more numeric matrices, character matrices, or cell arrays.

Arguments are concatenated vertically. The returned values are padded with blanks as needed to make each row of the string array have the same length. Empty input strings are significant and will concatenated in the output.

For numerical input, each element is converted to the corresponding ASCII character. A range error results if an input is outside the ASCII range (0-255).

For cell arrays, each element is concatenated separately. Cell arrays converted through char can mostly be converted back with cellstr. For example:

char ([97, 98, 99], "", {"98", "99", 100}, "str1", ["ha", "lf"])
   ⇒ ["abc    "
       "       "
       "98     "
       "99     "
       "d      "
       "str1   "
       "half   "]

See also: strvcat, cellstr.

: strvcat (x)
: strvcat (x, …)
: strvcat (s1, s2, …)
: strvcat (cell_array)

Create a character array from one or more numeric matrices, character matrices, or cell arrays.

Arguments are concatenated vertically. The returned values are padded with blanks as needed to make each row of the string array have the same length. Unlike char, empty strings are removed and will not appear in the output.

For numerical input, each element is converted to the corresponding ASCII character. A range error results if an input is outside the ASCII range (0-255).

For cell arrays, each element is concatenated separately. Cell arrays converted through strvcat can mostly be converted back with cellstr. For example:

strvcat ([97, 98, 99], "", {"98", "99", 100}, "str1", ["ha", "lf"])
      ⇒ ["abc    "
          "98     "
          "99     "
          "d      "
          "str1   "
          "half   "]

See also: char, strcat, cstrcat.

: strcat (s1, s2, …)

Return a string containing all the arguments concatenated horizontally.

If the arguments are cell strings, strcat returns a cell string with the individual cells concatenated. For numerical input, each element is converted to the corresponding ASCII character. Trailing white space for any character string input is eliminated before the strings are concatenated. Note that cell string values do not have whitespace trimmed.

For example:

strcat ("|", " leading space is preserved", "|")
    ⇒ | leading space is preserved|
strcat ("|", "trailing space is eliminated ", "|")
    ⇒ |trailing space is eliminated|
strcat ("homogeneous space |", "  ", "| is also eliminated")
    ⇒ homogeneous space || is also eliminated
s = [ "ab"; "cde" ];
strcat (s, s, s)
    ⇒
        "ababab   "
        "cdecdecde"
s = { "ab"; "cd " };
strcat (s, s, s)
    ⇒
        {
          [1,1] = ababab
          [2,1] = cd cd cd
        }

See also: cstrcat, char, strvcat.

: cstrcat (s1, s2, …)

Return a string containing all the arguments concatenated horizontally with trailing white space preserved.

For example:

cstrcat ("ab   ", "cd")
      ⇒ "ab   cd"
s = [ "ab"; "cde" ];
cstrcat (s, s, s)
      ⇒ "ab ab ab "
         "cdecdecde"

See also: strcat, char, strvcat.


Next: , Up: Creating Strings   [Contents][Index]