strings

String handling

Syntax

S = 'Any Characters'
S = [S1 S2 ...]
C = {S1 S2 ...}
S = strcat(S1, S2, ...)
S = char(S1, S2, ...)
S = char(X)
X = double(S)

Description

S = 'Any Characters' creates a character array, or string. The string is actually a vector that contains the numeric codes for the characters (codes 0 to 127 are ASCII). The length of S is the number of characters. A quotation within the string is indicated by two quotation marks.

S = [S1 S2 ...] concatenates character arrays S1, S2, etc. into a new character array, S.

C = {S1 S2 ...} creates a cell array of strings. Separate each row of the cell array with a semicolon (;).

S = strcat(S1, S2, ...) horizontally concatenates S1, S2, etc., which can be character arrays or cell arrays of strings. If the inputs are character arrays, strcat removes trailing white space. For more information, see the strcat reference page.

S = char(S1, S2, ...) vertically concatenates character arrays S1, S2, etc., padding each input string as needed so that each row contains the same number of characters.

S = char(X) converts an array that contains positive integers representing numeric codes into a MATLAB® character array.

X = double(S) converts the string to its equivalent integer numeric codes.

Examples

Create a simple string that includes a single quote.

msg = 'You''re right!'

msg =
You're right!

Create the string name using two methods of concatenation.

name = ['Thomas' ' R. ' 'Lee']
name = strcat('Thomas',' R.',' Lee')

Create a character array of strings.

C = char('Hello','Goodbye','Yes','No')

C =
Hello  
Goodbye
Yes    
No     

Create a cell array of strings.

S = {'Hello' 'Goodbye'; 'Yes' 'No'}

S = 
    'Hello'    'Goodbye'
    'Yes'      'No'

More About

expand all

Tips

  • To convert between character arrays and cell arrays of strings, use char and cellstr. Most string functions support both types.

  • To determine whether S is a character array or cell array, call ischar(S) or iscellstr(S).

Was this topic helpful?