| Stateflow | ![]() |
www.kxcad.net Home > CAE Software Index > MATLAB Index >
| 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** |
* Stateflow extends the abs function beyond that of its standard C counterpart with its own built in functionality. See Calling the abs Function.
** Stateflow also allows you to 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 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 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 included at the top of generated code field of the Target Options dialog. See the section Specifying Custom Code Options for Stateflow Targets.
Stateflow extends the interpretation of its abs function 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, Stateflow evaluates abs(x) with the standard C function abs applied to x, or abs(x).
If x is an integer of type other than int32, Stateflow evaluates abs(x) with the standard C abs function applied 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, Stateflow evaluates abs(x) with the standard C function fabs applied to x, or fabs(x).
If x is a floating point number of type single, Stateflow evaluates abs(x) with the standard C function fabs applied to a cast of x as a double, or fabs((double)x).
If x is a fixed-point number, Stateflow evaluates abs(x) with the standard C function fabs applied 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 Stateflow 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 x is declared in Stateflow custom code, Stateflow evaluates abs(x) with the standard C abs function in all cases. For instructions on inserting custom code into Stateflow diagrams, see Integrating Custom Code with Stateflow Targets. |
Although min and max are not C library functions, Stateflow enables 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(), Stateflow generates code for them with 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 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 generates code that calls the target implementations instead of the associated C library functions. Furthermore, Stateflow 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 documentation. 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 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.
For additional information, seeSpecifying Custom Code Options for Stateflow Targets.
Include a header file containing the declarations of your C code functions in the Custom code included at the top of generated code field.
Specify the source file name containing your C code functions in the Custom source files field.
To specify custom C functions for simulation, see Including Custom Code in Stateflow Simulation Targets.
To specify custom C functions for generated code, follow these steps:
In the Stateflow Editor, select Tools > Open RTW Target.
The Configuration Parameters dialog box opens, displaying the general Real-Time Workshop configuration parameters.
In the left pane of the Configuration Parameters dialog box, 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.
String parameters to user-written functions are passed between 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 data dictionary, the following rules apply.
Do not use pointers to pass data items input from Simulink.
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, the coder option Use bitsets to store state-configuration must be turned off (see Configuring Real-Time Workshop for Stateflow).
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-2007 The MathWorks, Inc. Terms of Use Patents Trademarks Acknowledgments