| Stateflow® | ![]() |
| On this page… |
|---|
Replacing C Math Library Functions with Target-Specific Implementations |
You can call the following small subset of the C Math Library functions:
abs* ** | acos** | asin** | atan** | atan2 | ceil** |
cos** | cosh** | exp** | fabs | floor** | fmod |
labs | ldexp | log** | log10** | pow | rand |
sin** | sinh** | sqrt** | tan** | tanh** |
* The Stateflow® abs function goes beyond that of its standard C counterpart with its own built-in functionality. See Calling the abs Function.
** You can also replace calls to the C Math Library with target-specific implementations for this subset of functions. For more information, see Replacing C Math Library Functions with Target-Specific Implementations
You can call the above C Math Library functions without doing anything special as long as you are careful to call them with the right data types. In case of a type mismatch,Stateflow software replaces the input argument with a cast of the original argument to the expected type. For example, if you call the sin function with an integer argument, Stateflow software replaces the argument with a cast of the original argument to a floating-point number of type double.
If you call other C library functions not specified above, be sure to include the appropriate #include... directive in the Custom Code pane of the Simulation Target dialog. For details, see Building Targets.
Interpretation of the Stateflow abs function goes beyond the standard C version to include integer and floating-point arguments of all types as follows:
If x is an integer of type int32, the standard C function abs applies to x, or abs(x).
If x is an integer of type other than int32, the standard C abs function applies to a cast of x as an integer of type int32, or abs((int32)x).
If x is a floating-point number of type double, the standard C function fabs applies to x, or fabs(x).
If x is a floating-point number of type single, the standard C function fabs applies to a cast of x as a double, or fabs((double)x).
If x is a fixed-point number, the standard C function fabs applies to a cast of the fixed-point number as a double, or fabs((double)Vx), where Vx is the real-world value of x.
If you want to use the abs function in the strict sense of standard C, be sure to cast its argument or return values to integer types. See Type Cast Operations.
Note If you declare x in custom code, the standard C abs function applies in all cases. For instructions on inserting custom code into Stateflow charts, see Building Targets. |
Although min and max are not C library functions, you can use them by emitting the following macros automatically at the top of generated code.
#define min(x1,x2) ((x1) > (x2))? (x2):(x1) #define max(x1,x2) ((x1) > (x2))? (x1):(x2)
To allow compatibility with user graphical functions named min() or max(), generated code uses a mangled name of the following form: <prefix>_min. However, if you export min() or max() graphical functions to other Stateflow charts in the Stateflow machine, the name of these functions can no longer be emitted with mangled names in generated code and conflict occurs. To avoid this conflict, rename the min() and max() graphical functions.
You can use the target function library published by Real-Time Workshop® Embedded Coder™ code generation software to replace the default implementations of a subset of C-library functions with target-specific implementations (see Supported Target Library Functions). When you specify a target function library, Stateflow software generates code that calls the target implementations instead of the associated C library functions. Furthermore, Stateflow software also uses target implementations in cases where the compiler generates calls to math functions, such as in fixed-point arithmetic utilities.
To learn how to create and register function replacement tables in a target function library, see Target Function Libraries in the Real-Time Workshop Embedded Coder User's Guide. To select and view target function libraries, see Selecting and Viewing Target Function Libraries in the Real-Time Workshop® User's Guide.
You can replace the following C Math Library functions with target-specific implementations:
abs
Note See Replacing Calls to abs. |
acos
asin
atan
ceil
cos
cosh
exp
floor
log
log10
sin
sinh
sqrt
tan
tanh
Stateflow software replaces calls to abs with target functions as follows:
| For: | Action: |
|---|---|
| abs with floating-point arguments | Replaces with target function |
| abs with integer arguments | Replaces with ANSI C function |
| abs with fixed-point arguments (zero bias) | Replaces with ANSI C function |
| abs with fixed-point arguments (non-zero bias) | Generates error |
You can install your own C code functions for use in the Stateflow action language for simulation and for C code generation.
To specify custom C functions for simulation, see Building Targets.
To specify custom C functions for code generation, follow these steps:
In the Stateflow Editor, select Simulation > Configuration Parameters.
The Configuration Parameters dialog box appears, displaying the general Real-Time Workshop configuration parameters.
In the left pane of the Configuration Parameters dialog, select Custom Code and specify your custom C files as described in Real-Time Workshop Pane: Custom Code.
Follow these guidelines when using your own C code functions in the Stateflow action language:
Define a function by its name, any arguments in parentheses, and an optional semicolon.
Pass string parameters to user-written functions using single quotation marks. For example, func('string').
An action can nest function calls.
An action can invoke functions that return a scalar value (of type double in the case of MATLAB® functions and of any type in the case of C user-written functions).
These are example formats of function calls using transition action notation.

If S1 is active, event e occurs, c is true, and the transition destination is determined, then a function call is made to function_name with arg1, arg2, and arg3. The transition action in the transition from S2 to S3 shows a function call nested within another function call.
These are example formats of function calls using state action notation.

When the default transition into S1 occurs, S1 is marked active and then its entry action, a function call to function_name1 with the specified arguments, is executed and completed. If S2 is active and an event occurs, the during action, a function call to function_name3 with the specified arguments, executes and completes.
A Stateflow action can pass arguments to a user-written function by reference rather than by value. In particular, an action can pass a pointer to a value rather than the value itself. For example, an action could contain the following call
f(&x);
where f is a custom-code C function that expects a pointer to x as an argument.
If x is the name of a data item defined in the Stateflow hierarchy, the following rules apply.
Do not use pointers to pass data items input from a Simulink® model.
If you need to pass an input item by reference, for example, an array, assign the item to a local data item and pass the local item by reference.
If x is a Simulink output data item having a data type other than double, the chart property Use strong data typing with Simulink IO must be on (see Specifying Chart Properties).
If the data type of x is boolean, you must turn off the coder option Use bitsets to store state-configuration (see Optimizing Real-Time Workshop® Code Generation of the Main Model).
If x is an array with its first index property set to 0 (see Setting Data Properties in the Data Dialog), then the function must be called as follows.
f(&(x[0]));
This passes a pointer to the first element of x to the function.
If x is an array with its first index property set to a nonzero number (for example, 1), the function must be called in the following way:
f(&(x[1]));
This passes a pointer to the first element of x to the function.
![]() | Using Special Symbols in Actions | Using MATLAB® Functions and Data in Actions | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |