Main Content

Manage Textual Information by Using Strings

To use textual data to control chart behavior and to manipulate text to create natural language output, use strings. String data is available only in Stateflow® charts that use C as the action language.

Example of String Data

In Stateflow, a string is a piece of text surrounded by quotes ("..." or '...'). For example, this chart takes string data as input. Based on that input, the chart produces a corresponding string output.

To specify a string symbol, set its Type field to string. Stateflow dynamically allocates memory space for this type of data.

Alternatively, you can create string data with a maximum number of characters. To specify a string symbol with a buffer size of n characters, set its Type field to stringtype(n). The text of the string can be shorter than the buffer, but if it exceeds the buffer size, then the text in the string is truncated. For instance, if the symbol output in the previous chart is specified as stringtype(10), then its value in the state On is truncated to "All system". Depending on the configuration parameter String truncation checking, you can halt simulation and diagnose truncation of string data.

String Truncation CheckingDescription
errorSimulation stops with an error.
warningString is truncated. Simulation continues with a warning.
noneString is truncated. Simulation continues with no error or warning.


Unlike C or C++, Stateflow interprets escape sequences as literal characters. For example, the string "\n" contains two characters, backslash and n, and not a newline character.

Computation with Strings

To manipulate string data in a Stateflow chart, use the operators listed in this table.



An alternative way to execute dest = src.

Assign string data to s1, s2, and s3.

strcpy(s1,'So long');

dest = src

Assigns string src to dest.

Assign string data to s4, s5, and s6.

s4 = 'Auf Wiedersehen';
s5 = "Adieu";
s6 = s4;

dest = strcat(s1,...,sN)

Concatenates strings s1,...,sN.

Concatenate strings to form "Stateflow".

s1 = "State";
s2 = "flow";
dest = strcat(s1,s2);


dest = substr(str,i,n)

Returns the substring of length n starting at the i-th character of string str. Use zero-based indexing.

Extract substring "Stateflow" from a longer string.

str = "Stateflow, rule the waves!";
dest = substr(str,0,9);


dest = tostring(X)

Converts numeric, Boolean, or enumerated data to string.

Convert numeric value to string "1.2345".

dest = tostring(1.2345);

Convert Boolean value to string "true".

dest = tostring(1==1);

Convert enumerated value to string "RED".

dest = tostring(RED);


tf = strcmp(s1,s2)

Compares strings s1 and s2. Returns 0 if the two strings are identical. Otherwise returns a nonzero integer.

  • The sign of the output value depends on the lexicographic ordering of the input strings s1 and s2.

  • The magnitude of the output value depends on the compiler that you use. This value can differ in simulation and generated code.

Strings are considered identical when they have the same size and content.

This operator is consistent with the C library function strcmp or the C++ function, depending on the compiler that you select for code generation. The operator behaves differently than the function strcmp in MATLAB®.

Return a value of 0 (strings are equal).

tf = strcmp("abc","abc");

Return a nonzero value (strings are not equal).

tf = strcmp("abc","abcd");

s1 == s2

An alternative way to execute strcmp(s1,s2) == 0.

Return a value of true.

"abc" == "abc";

s1 != s2

An alternative way to execute strcmp(s1,s2) != 0.

Return a value of true.

"abc" != "abcd";

tf = strcmp(s1,s2,n)

Returns 0 if the first n characters in s1 and s2 are identical.

Return a value of 0 (substrings are equal).

tf = strcmp("abc","abcd",3);


L = strlen(str)

Returns the number of characters in the string str.

Return a value of 9.

L = strlen("Stateflow");


X = str2double(str)

Converts the text in string str to a double-precision value.

str contains text that represents a number. Text that represents a number can contain:

  • Digits

  • A decimal point

  • A leading + or - sign

  • An e preceding a power of 10 scale factor

If str2double cannot convert text to a number, then it returns a NaN value.

Return a value of -12.345.

X = str2double("-12.345");

Return a value of 123400.

X = str2double("1.234e5");


A = str2ascii(str,n)

Returns array of type uint8 containing ASCII values for the first n characters in str, where n is a positive integer. Use of variables or expressions for n is not supported.

Return uint8 array {72,101,108,108,111}.

A = str2ascii("Hello",5);


dest = ascii2str(A)

Converts ASCII values in array A of type uint8 to string.

Return string "Hi!".

A[0] = 72;
A[1] = 105;
A[2] = 33;
dest = ascii2str(A);

Where to Use Strings

Use string data at these levels of the Stateflow hierarchy:

  • Chart

  • Subchart

  • State

Use string data as arguments for:

  • State actions

  • Condition and transition actions

  • Graphical functions

  • Simulink® functions

  • Truth table functions that use C as the action language

If you have Simulink Coder™ installed, you can use string data for simulation and code generation.

See Also

| | | | | | | |

Related Topics