Page 880 - Mechatronics with Experiments
P. 880
866 MECHATRONICS
% If MATLAB® workspace variable does not exit,
% it is created automatically.
®
We can use the same operator to access MATLAB built-in functions:
a = ml.sin(ml.x) ; % Stateflow variable ’a’ is assigned to
% the value of Sin of MATLAB® workspace
variable ’x’.
b = ml.sin(a) ; % ..................’b’ ...............
% ........... Sin of Stateflow variable ’a’.
Notice the difference between the above two examples: in the first one the data variable ‘x’
®
in the MATLAB workspace is used for the sine function. In the second example, the data
variable ’a’ in the Stateflow is used in the sine function.
Function Calls from a Stateflow Chart: Embedded MATLAB ® Func-
®
tions An “Embedded MATLAB Function” can be added by selecting the “Embedded
MATLAB ® Function” icon on the left pane of the Stateflow Editor, and dragging and
dropping it in the chart, then typing the name of the function. Then double-click on the box
just created to bring up the text editor and edit the content of the function. Input and output
®
arguments from the function can be any MATLAB data type of the form scalar, vector,
or matrix. This is a very effective and convenient way to code “actions” that are more than
a single line.
®
Figure A.38 shows the windows on how to add an Embedded MATLAB Function
in a Stateflow chart. The function is used to define an action.
®
1. We draw the transitions and actions which calls an embedded MATLAB function
in the usual way.
®
2. Then use the “Embedded MATLAB Function” icon on the left pane of the Stateflow
Chart, copy it into the chart.
3. Then type the name of the function in the created rectangle.
4. Double-clicking on the rectangle block for the function brings up the Embedded
®
MATLAB Editor.
®
5. From the Embedded MATLAB Editor, use Tool > Model Explorer, then edit the
input–output data type and size, as well as local variables as needed.
6. Then simply type the logic of the function into the editor.
Figure A.39 shows the window for the ‘Model Explorer” that allows us to edit the data for
the function.
®
If the embedded MATLAB function calls yet other function(s), that function(s) can
®
be included in the same file below the embedded MATLAB function code.
Function Calls from a Stateflow Chart: User Written C Functions
User-written C functions can be called from Stateflow chart’s transition actions and state
actions (Figure A.40). The C-function call is made just like in a C language program, that
is
function_name(argument1, ... ) ;
The string parameters are passed using quotes, such as “string1” or “Hello” in the
argument list. Function calls can be nested, that is one function call can include another
function call in its argument list (Figure A.40). Arguments can be passed by an address