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.
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(
.
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
n
)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 Checking | Description |
---|---|
error | Simulation stops with an error. |
warning | String is truncated. Simulation continues with a warning. |
none | String is truncated. Simulation continues with no error or warning. |
Note
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.
To manipulate string data in a Stateflow chart, use the operators listed in this table.
Operator | Syntax | Description | Example |
---|---|---|---|
strcpy |
| An alternative way to execute | Assign string data to strcpy(s1,'So long'); strcpy(s2,"Farewell"); strcpy(s3,s2); |
| Assigns string | Assign string data to s4 = 'Auf Wiedersehen'; s5 = "Adieu"; s6 = s4; | |
strcat |
| Concatenates strings
| Concatenate strings to form
s1 = "State"; s2 = "flow"; dest = strcat(s1,s2); |
substr |
| Returns the substring of length | Extract substring str = "Stateflow, rule the waves!";
dest = substr(str,0,9); |
tostring |
| Converts numeric, Boolean, or enumerated data to string. | Convert numeric value to string
dest = tostring(1.2345); |
Convert Boolean value to string
dest = tostring(1==1); | |||
Convert enumerated value to string
dest = tostring(RED); | |||
strcmp |
| Compares strings
Strings are considered identical when they have the same size and content. This operator is
consistent with the C library function
| Return a value of tf = strcmp("abc","abc"); |
Return a nonzero value (strings are not equal). tf = strcmp("abc","abcd"); | |||
| An alternative way to execute | Return a value of
"abc" == "abc"; | |
| An alternative way to execute | Return a value of
"abc" != "abcd"; | |
| Returns 0 if the first
n characters in s1 and
s2 are identical. | Return a value of tf = strcmp("abc","abcd",3); | |
strlen |
| Returns the number of characters in the string
str . | Return a value of
L = strlen("Stateflow"); |
str2double |
| Converts the text in string
If | Return a value of
X = str2double("-12.345"); |
Return a value of
X = str2double("1.234e5"); | |||
str2ascii |
| Returns array of type | Return A = str2ascii("Hello",5); |
ascii2str |
| Converts ASCII values in array | Return string
A[0] = 72; A[1] = 105; A[2] = 33; dest = ascii2str(A); |
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.
ascii2str | str2ascii | str2double | strcat | strcmp | strcpy | strlen | substr | tostring