Method Reference: string.string

string: str = string ()
string: str = string (in)
string: str = string (calendarDuration, 'Format', FMT)
string: str = string (duration, 'Format', FMT)
string: str = string ({in1, …, inN})

Create a new string array.

str = string () creates a scalar string array, whose element contains an empty character vector.

str = string (in) creates a string array of the same size as in, which is converted to string according to the following options:

  • character arrays are converted so that each row becomes a string element, with any trailing whitespace preserved; cell arrays of character vectors are stored as-is.
  • numeric arrays are converted via the code num2str function.
  • logical arrays are converted to either false or true character sequences.
  • categorical arrays are converted via their cellstr method.
  • datetime arrays are converted via their dispstrings method.
  • calendarDuration arrays and duration arrays are converted via their respective cellstr methods, in which case an extra pair argument is supported to allow parsing to the respective method the appropriate display format. See calendarDuration and duration for valid formats parsed through FMT to each class method. Extra input arguments to the string constructor except for this case are ignored.
  • missing arrays are converted to a string array of missing elements.

str = string ({in1, …, inN}) creates a string array from a cell array, which may contain any combination of the aforementioned data types, provided that each cell element is compatible to a string scalar. When using this syntax, calendarDuration arrays and duration arrays are converted via their dispstrings method, hence no extra format argument is meaningful.

See also: calendarDuration, categorical, datetime, duration, missing

Source Code: string

Example: 1

A string is MATLAB's string-array type. There is a crucial Octave difference to keep in mind: in core Octave BOTH 'text' and "text" produce ordinary character vectors (class char) — unlike MATLAB, the double quote does not make a string. Only the string constructor produces a string array.

 class ('text')             % single quotes -> char
ans = char
 class ("text")             % double quotes -> ALSO char in Octave
ans = char
 class (string ('text'))    % the constructor -> string
ans = string

Example: 2

Build a string array from a cell array of character vectors — one element per cell. A string array prints its elements in double quotes.

 string ({'apple'; 'banana'; 'cherry'})
ans =
  3x1 string array

    "apple"     
    "banana"    
    "cherry"

Example: 3

Numbers convert to their text form.

 string ([3.14, 42, -1])
ans =
  1x3 string array

    "3.14"    "42"    "-1"

Example: 4

The special <missing> value is the string counterpart of NaN, and is distinct from an empty string "". Assign string (missing) to introduce one.

 s = string ({'a'; 'b'; 'c'})
s =
  3x1 string array

    "a"    
    "b"    
    "c"
 s(2) = string (missing)
s =
  3x1 string array

    "a"          
    <missing>    
    "c"
 ismissing (s)
ans =

  0
  1
  0