MATLAB Examples

Share Functions Across Simulink and Stateflow

Contents

Communicate and Share Resources with Functions

This example shows a model calling functions across Simulink® and Stateflow®. The slexPrinterExample model has three computer clients sharing a printer. Each computer creates print jobs by calling the Simulink function addPrintJob.

In this example, the Stateflow chart communicates with the model by:

  • Defining and exporting a graphical function that is called by Simulink.
  • Calling a Simulink function that is defined in Simulink.
open_system('slexPrinterExample');

Each computer client invokes the printer server with a call to the Simulink function, addPrintJob. The addPrintJob function calls the Stateflow graphical function queuePrintJob to add the print job to the work load. The chart processes the work and calls the Simulink function printerInk to model usage of printer ink.

Call a Simulink Function from Stateflow

The function printerInk is defined in a Simulink Function block at the top level of the model. The function interface printerInk(work) defines one input argument. The Simulink Function, printerInk, also interacts with the model with signal lines through the inport ink and outport ink'. The state Busy matches the function signature for printerInk(work) by passing one input argument.

open_system('slexPrinterExample/Queueing and Processing Incoming Jobs')

Export Stateflow Functions to Simulink

In the chart Queuing and Processing Incoming Jobs, the properties Export Chart Level Functions and Treat Exported Functions as Globally Visible are selected. These properties allow the Simulink function addPrintJob to call the chart graphical function, queuePrintJob.