Manage Textual Information by Using Strings - MATLAB & Simulink (original) (raw)

Main Content

Control chart behavior and output easy-to-read text by using strings to create textual data.

Creating Strings in Stateflow

In Stateflow®, a string is a piece of text surrounded by double quotes ("..."). In addition, single-quoted strings ('...') are supported in charts that use C as the action language. For example, this chart takes string data as input. Based on that input, the chart produces a corresponding string output.

Chart that uses a string input to switch between two states, On and Off.

To specify a string symbol, first open the Property Inspector. In the Symbols pane, select the data that you want to convert to a string. In the Property Inspector, set the Type to string. Stateflow dynamically allocates memory space for this type of data.

Computation with Strings

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

Goal MATLAB Action Language Function Example C Action Language Function Example
Concatenate two strings plus h = "Hello," w = " world!" x = plus(h,w) strcat s1 = "State"; s2 = "flow"; dest = strcat(s1,s2);
Determine the length of a string strlength h = "Hello, world!" x = strlength(h) strlen L = strlen("Stateflow");
Convert a string to a double str2double X = str2double("-12.345"); str2double X = str2double("-12.345");
Convert numeric, Boolean, or enumerated data to string string a = [1307]; str = string(a) tostring dest = tostring(RED);

String Truncation

You can also create string data with a maximum number of characters. To specify symbol as a string with a buffer size of n characters, set the Type field of the symbol tostringtype(_`n`_). The text of the string can be shorter than the buffer, but if the string length exceeds the buffer size, then the text in the string is truncated. For example, if the Type property of the symbol output in the previous chart is stringtype(10), then its value in the stateOn is truncated to "All system".

You can enable the String truncation checking parameter to choose whether to stop simulation or generate a warning when a string exceeds the length specified by stringtype(n).

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 andn, and not one newline character.

Differences Between Charts That Use MATLAB and C as the Action Language

Key differences between strings in charts that use C as the action language and charts that use MATLAB® include:

Limitations

Parameter data cannot be strings.

The following limitations exist for charts that use MATLAB as the action language:

See Also

ascii2str | str2ascii | str2double | strcat | strcmp | strcpy | strlen | substr | tostring

Topics