Main Content

Design Charts with金宝appFunctions

In this tutorial, you use a Simulink®function in a Stateflow®chart to improve the design of a model that contains a function-call subsystem. You can replace a function-call subsystem with a Simulink function in a chart when:

  • The subsystem performs calculations required by the chart.

  • Other blocks in the model do not need access to the subsystem outputs.

Simulink functions are not supported in standalone Stateflow charts in MATLAB®

Note

To skip the conversion steps, open the modelsf_car:

openExample("stateflow/AutomaticTransmissionWithActiveStateDataExample")

Open the Model

Open the modelold_sf_car。这个模型包含了一个函数调用子系统namedThreshold Calculationand a Stateflow chart namedshift_logic

When you run this model, the chart broadcasts the output eventCALC_TH傅来触发nction-call subsystem. The subsystem interpolates two values for theshift_logicchart. The subsystem outputs (up_thanddown_th) return to the chart as inputs.

After the simulation, the Scope blocks display these results.

Add a金宝appFunction to the Chart

Follow these steps to add a Simulink function to theshift_logicchart.

  1. In the Simulink model, right-click the Threshold Calculation block in the lower left corner and selectCutfrom the context menu.

  2. Open theshift_logicchart.

  3. In the chart, right-click belowselection_stateand selectPastefrom the context menu.

  4. Expand the new Simulink function so that the signature fits inside the function box.

    Tip

    To change the font size of a function, right-click the function box and select a new size from theFont Sizemenu.

  5. Expand the border ofselection_stateto include the new function.

    Note

    The function resides in this state instead of the chart level because no other state in the chart requires the function outputsup_thanddown_th。SeeBind a Simulink Function to a State

  6. Rename the Simulink function fromThreshold_Calculationtocalc_thresholdby entering[down_th, up_th] = calc_threshold(gear, throttle)in the function box.

Change the Scope of Chart Data

In the Model Explorer, change the scope of chart-level dataup_thanddown_thtoLocalbecause calculations for those data now occur inside the chart.

Update State Action in the Chart

In the Stateflow Editor, change theduringaction inselection_stateto call the Simulink functioncalc_threshold

during: [down_th, up_th] = calc_threshold(gear, throttle);

Add Data to the Chart

Because the functioncalc_thresholdtakesthrottleas an input, you must define that data as a chart input. (For details, seeAdd Stateflow Data。)

  1. Add input datathrottleto the chart with aPortproperty of 1.

    Using port 1 prevents signal lines from overlapping in the Simulink model.

  2. In the Simulink model, add a signal line forthrottleto an input to the Engine block and to an input to theshift_logicchart.

Remove Unused Items in the Model

  1. In the Model Explorer, delete the function-call output eventCALC_THbecause theThreshold Calculationblock no longer exists.

  2. Delete any dashed signal lines from your model.

Run the New Model

Your new model looks something like this:

If you simulate the new model, the results match those of the original design.

Related Topics