Main Content

Code Generation for Strings

Code generation supports 1-by-1 MATLAB® string arrays. Code generation does not support string arrays that have more than one element.

A 1-by-1 string array, called a string scalar, contains one piece of text, represented as a 1-by-n character vector. An example of a string scalar is "Hello, world". For more information about strings, see Text in String and Character Arrays.


For string scalars, code generation does not support:

  • Global variables

  • Indexing with curly braces {}

  • Missing values

  • Defining input types programmatically by using preconditioning with assert statements

  • Their use with coder.varsize, when generating code by using the codegen command, the fiaccel (Fixed-Point Designer) command, or the MATLAB Coder™ app

  • Their use as Simulink® signals, parameters, or data store memory

For code generation, limitations that apply to classes apply to strings. See MATLAB Classes Definition for Code Generation.

Differences Between Generated Code and MATLAB Code

  • Converting a string that contains multiple unary operators to double can produce different results between MATLAB and the generated code. Consider this function:

    function out = foo(op)
    out = double(op + 1);

    For an input value "--", the function converts the string "--1" to double. In MATLAB, the answer is NaN. In the generated code, the answer is 1.

  • Double conversion for a string with misplaced commas (commas that are not used as thousands separators) can produce different results from MATLAB.

Related Topics