| Contents | Index |
| On this page… |
|---|
Suppose that you want to test the response of the sf_car model to a sudden change in value for gear.

This model simulates for 30 seconds, but you want to see what happens when the value of gear changes at t = 10. You can simulate the model, save the SimState at t = 10, load and modify the SimState, and then simulate again between t = 10 and 20.
| Step | Task | Reference |
|---|---|---|
| 1 | Define the SimState for your chart. | Defining the SimState |
| 2 | Load the SimState and modify values. | Loading the SimState and Modifying Values |
| 3 | Test the modified SimState by running the model. | Testing the Modified SimState |
Open the model.
Type sf_car at the command prompt.
Enable saving of a SimState.
Open the Configuration Parameters dialog box and go to the Data Import/Export pane.
Select the Final states check box.
Enter a name, such as sf_car_ctx01.
You can choose any alphanumeric string for the name.
Select the Save complete SimState in final state check box.
Click Apply.
Define the start and stop times for this simulation segment.
In the Configuration Parameters dialog box, go to the Solver pane.
For Start time, enter 0.
For Stop time, enter 10.
Click OK.
Start simulation.
When you simulate the model, you save the complete simulation state at t = 10 in the variable sf_car_ctx01 in the MATLAB base workspace.
At t = 10, the engine is operating at a steady-state value of 2500 RPM.

Disable saving of a SimState.
This step prevents you from overwriting the SimState you saved in the previous step.
Open the Configuration Parameters dialog box and go to the Data Import/Export pane.
Clear the Save complete SimState in final state check box.
Clear the Final states check box.
Click OK.
Enable loading of a SimState.
Open the Configuration Parameters dialog box and go to the Data Import/Export pane.
Select the Initial state check box.
Enter the variable that contains the SimState of your chart: sf_car_ctx01.
Click OK.
Define an object handle for the SimState values of the shift_logic chart.
At the command prompt, type:
blockpath = 'sf_car/shift_logic'; c = sf_car_ctx01.getBlockSimState(blockpath);
Tip If the chart appears highlighted in the model window, you can specify the block path using gcb: c = sf_car_ctx01.getBlockSimState(gcb); |
Look at the contents of the SimState.
c =
Block: "shift_logic" (handle) (active)
Path: sf_car/shift_logic
Contains:
+ gear_state "State (AND)" (active)
+ selection_state "State (AND)" (active)
gear "Block output data" double [1, 1]
down_th "Local scope data" double [1, 1]
up_th "Local scope data" double [1, 1]The SimState of your chart contains a list of states and data in hierarchical order.
Highlight the states that are active in your chart at t = 10.
At the command prompt, type:
c.highlightActiveStates;
In the chart, all active states appear highlighted.

To highlight active states automatically at the end of a simulation, enable chart animation and select Maintain Highlighting in the debugger. For details, see Animating Stateflow Charts.
Tip To check if a single state is active, you can use the isActive method. For example, type: c.gear_state.second.isActive This command returns true (1) when a state is active and false (0) otherwise. For information on other methods, see Methods for Interacting with the SimState of a Chart. |
Change the active substate of selection_state to downshifting.
Use this command:
c.selection_state.downshifting.setActive;
The newly active substate appears highlighted in the chart.

Change the value of output data gear.
When you type c.gear at the command prompt, you see a list of data properties similar to this:
>> c.gear
ans =
Description: 'Block output data'
DataType: 'double'
Size: '[1, 1]'
Range: [1x1 struct]
InitialValue: [1x0 double]
Value: 2
You can change the value of gear from 2 to 1 by typing:
c.gear.Value = 1;
However, you cannot change the data type or size of gear. Also, you cannot specify a new value that falls outside the range set by the Minimum and Maximum parameters. For details, see Rules for Modifying Data Values.
Save the modified SimState.
Use this command:
sf_car_ctx01 = sf_car_ctx01.setBlockSimState(blockpath, c);
Define the new stop time for the simulation segment to test.
In the Configuration Parameters dialog box, go to the Solver pane.
For Stop time, enter 20.
Click OK.
You do not need to enter a new start time because the simulation continues from where it left off.
Start simulation.
The engine reacts as follows:

![]() | Dividing a Long Simulation into Segments | Testing a Chart with Fault Detection and Redundant Logic | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |