| Stateflow User's Guide | ![]() |
Creating a Model with Embedded MATLAB Function Decisions
In this topic you examine an example model you can use to generate a model coverage report for two Embedded MATLAB functions in Stateflow. The following model is named intersecting_rectangles. It contains a single Stateflow block with output data sent to a Scope block as shown.
The preceding Stateflow diagram has a state with a default transition and entry and during actions. The state executes its entry action the first time that it is entered for the first time sample. Each succeeding time sample calls the during action of the active state.
The entry and during actions of state A call the Embedded MATLAB function run_intersect_test, which appears as follows in the Embedded MATLAB Editor window:
run_intersect_test calls the function rect_intersect with two rectangle arguments that each consist of coordinates for the lower left corner of the rectangle (origin), and its width and height. The first rectangle is a test rectangle, and the second is a stationary rectangle. The coordinates for the origin of the test rectangle are represented by the Stateflow data x1 and y1, which are both initialized to -1. This means that x1 and y1 are 0 for the first sample. The progression of rectangle arguments during simulation is as follows:
In the preceding display, the stationary rectangle is shown in bold with a lower left origin of (2,4) and a width and height of 2. At time t = 0, the first test rectangle has an origin of (0,0) and a width and height of 2. For each succeeding sample, the origin of the test rectangle is incremented by (1,1). The rectangles at sample times t = 2, 3, and 4 intersect with the test rectangle.
The function rect_intersect, as shown, checks to see if two rectangles intersect.
rect_intersect receives the two rectangle arguments from run_intersect_test. It first calculates horizontal (x) coordinates for the left and right sides, and vertical (y) values for the top and bottom sides for each rectangle and compares them in the nested if-else decisions shown. The function returns a logical value of 1 if the rectangles intersect and 0 if they do not.
Scope output during simulation, which plots the return value against the sample time, confirms the intersecting rectangles for sample 2, 3, and 4.
| Types of Model Coverage in Embedded MATLAB Functions | Understanding Embedded MATLAB Function Model Coverage | ![]() |
© 1994-2005 The MathWorks, Inc.