Simulink Functions Overview - MATLAB & Simulink (2024)

Simulink Functions Overview

What Are Simulink Functions?

A Simulink® function is a computational unit that calculates a set of outputs whenprovided with a set of inputs. The function header uses a notation similar to programminglanguages such as MATLAB® and C++. You can define and implement a Simulink function in several ways:

  • Simulink Function block —Function defined using Simulink blocks within a Simulink Function block.

  • Exported Stateflow® graphical function — Function defined with statetransitions within a Stateflow chart, and then exported to a Simulink model.

  • Exported StateflowMATLAB function — Function defined with MATLAB language statements within a Stateflow chart, and then exported to a Simulink model.

  • S-function — Function defined using anS-function block. For an example with an S-function, opensfcndemo_simulinkfunction_getset.

What Are Simulink Function Callers?

A Simulink function caller invokes the execution of a Simulink function from anywhere in a model or chart hierarchy.

  • Function Caller block — Calla function defined in Simulink or exported from Stateflow. See FunctionCaller.

  • Stateflow chart transition — In a Stateflow chart, call a function defined in Simulink or exported from Stateflow.

  • MATLAB Function block — Call a function from a MATLAB language script.

  • S-Function block — Call a function usingsystem methods. See ssDeclareFunctionCaller and ssCallSimulinkFunction.

  • MATLAB System block — Call a function using aSystem object™ and the MATLAB language.

Connect to Local Signals

In addition to Argument Inport and Argument Outportblocks, a Simulink Function block can interface to signals in thelocal environment of the block through Inport or Outport blocks. These signals are hidden fromthe caller. You can use port blocks to connect and communicate between two SimulinkFunction blocks or connect to root Inport andOutport blocks that represent external I/O.

Simulink Functions Overview- MATLAB & Simulink (1)

You can also connect the Outport blocks to sink blocks that includelogging (To File, To Workspace) and viewing(Scope, Display) blocks. However, these blocks execute lastafter all other blocks.

A Simulink Function block can output a function-call event to anOutport block.

Reusable Logic with Functions

Use functions when you need reusable logic across a model hierarchy. Consider an examplewhere a Simulink Function with reusable logic is defined in a Stateflow chart.

Simulink Functions Overview- MATLAB & Simulink (2)

You can move the reusable logic from inside the Stateflow chart to a Simulink Function block. The logic is then reusableby function callers in Simulink subsystems (Subsystem and Model blocks) and in Stateflow charts at any level in the modelhierarchy.

Simulink Functions Overview- MATLAB & Simulink (3)

The result is added flexibility for structuring your model for reuse.

Note

Input and output argument names (x2, y2) forcalling a function from a Stateflow chart do not have to match the argument names in the function prototype(u, y) of a Simulink Function block.

Input/Output Argument Behavior

The function prototype for a Simulink Function block can have identicalinput and output arguments. For example, a function that filters noise could input a signaland then return the signal after filtering.

mySignal = filter(mySignal)

You can call the function with a Function Caller block and add noise to atest signal to verify the function algorithm.

Simulink Functions Overview- MATLAB & Simulink (4)

When generating code for this model, the input argument for the SimulinkFunction block passes a pointer to the signal, not a copy of the signalvalue.

void filter(real_T *rtuy_mySignal){. . .
*rtuy_mySignal = model_P.DiscreteFilter_NumCoef * DiscreteFilter_tmp; }

Shared Resources with Functions

Use functions when you model a shared resource, such as a printer. The modelslexPrinterExample uses Simulink Function blocks as acommon interface between multiple computers and a single Stateflow chart that models a printer process.

To open slexPrinterExample, see Monitor Ink Status on Shared Printer Using Simulink Functions.

Simulink Functions Overview- MATLAB & Simulink (5)

How a Function Caller Identifies a Function

The function interface uses MATLAB syntax to define its input and output arguments. The name of thefunction is a valid ANSI® C identifier. The model hierarchy can contain only one functiondefinition with the identified function name. Simulink verifiesthat:

  • The arguments in the Function prototype parameter for aFunction Caller block matches the arguments specified in the function.For example, a function with two input arguments and one output argument appearsas:

    y = MyFunction(u1, u2)
  • The data type, dimension, and complexity of the arguments must agree. For aFunction Caller block, you can set the Input argumentspecifications and Output argument specificationsparameters, but usually you do not need to specify these parameters manually.Simulink derives the specification from thefunction.

    The only case where you must specify the argument parameters is when theFunction Caller block cannot find the function in the model or in anychild model it references. This situation can happen when the FunctionCaller block and called function are in separate models that are referenced bya common parent model. See Simulink Function Blocks in Referenced Modelsand Argument Specification for Simulink Function Blocks.

Reasons to Use a Simulink Function Block

Function-Call Subsystem blocks with direct signal connections fortriggering provide better signal traceability than Simulink Function blocks,but Simulink Function blocks have other advantages.

  • Eliminate routing of signal lines. TheFunction Caller block allows you to execute functions defined with aSimulink Function block without a connecting signal line. In addition,functions and their callers can reside in different models or subsystems. This approacheliminates signal routing problems through a hierarchical model structure and allowsgreater reuse of model components.

  • Use multiple callers to the same function. MultipleFunction Caller blocks or Stateflow charts can call the same function. If the function contains state (forexample, a Unit Delay block), the state is shared between the differentcallers.

  • Separate function interface from functiondefinition. Functions separate their interface (input and output arguments)from their implementation. Therefore, you can define a function using a SimulinkFunction block, an exported graphical function from Stateflow, or an exported MATLAB function from Stateflow. The caller does not need to know how or where the function wasimplemented.

Choose a Simulink Function or Reusable Subsystem

A consideration for using a Simulink Function block or aSubsystem block has to do with shared state between function calls. ASimulink Function block has shared state while a Subsystemblock, even if specified as a reusable function, does not.

  • For a Simulink Function block, when one block has multiple callers,code is always generated for one function. If the Simulink Function blockcontains blocks with state (for example, Delay or Memory),the state is persistent and shared between function callers. In this case, the order ofcalls is an important consideration.

  • For a Subsystem block, when a block has multiple instances and isconfigured as a reusable function, code is usually generated for one function as anoptimization. If the Subsystem block contains blocks with state, code isstill generated for one function, but a different state variable is passed to thefunction. State is not shared between theinstances.

When Not to Use a Simulink Function Block

Simulink Function blocks allow you to implement functions graphically,but sometimes using a Simulink Function block is not the bestsolution.

For example, when modeling a PID controller or a digital filter and you have to modelthe equations defining the dynamic system. Use an S-Function,Subsystem, or Model block to implement systems of equations,but do not use a Simulink Function block, because these conditions can occur:

  • Persistence of state between function calls. If aSimulink Function block contains any blocks with state (for example,Unit Delay or Memory), then their state values arepersistent between calls to the function. If there are multiple calls to that function,the state values are also persistent between the calls originating from differentcallers.

  • Inheriting continuous sample time. ASimulink Function block cannot inherit a continuous sample time.Therefore, do not use this block in systems that use continuous sample times to modelcontinuous systemequations.

Tracing Simulink Functions

Visually display connections between a Simulink function and their callers with lines that connect callers to functions:

  • Turning on/off tracing lines — On the Debugtab, under Information OverlaysSimulink Functions Overview- MATLAB & Simulink (6), click Connectors.Select Function Connectors option from theConnectors pane that appears on the Simulinkcanvas.

  • Direction of tracing lines — Lines connected at the bottom of a block arefrom a function caller. Lines connected at the top of a block are to a Simulink function or a subsystem containing the function.

    Simulink Functions Overview- MATLAB & Simulink (7)

    Simulink Functions Overview- MATLAB & Simulink (8)

  • Navigation to functions — A function caller can be within a subsystem.

    Simulink Functions Overview- MATLAB & Simulink (9)

    Navigate from a caller in a subsystem to a function by first opening the subsystem,and then clicking a link to the function.

    Simulink Functions Overview- MATLAB & Simulink (10)

    If the function is at the root level of a model, the function opens. If the functionis within a subsystem, the subsystem containing the function opens.

Monitor Ink Status on a Shared Printer Using Simulink Functions

After selecting Function Connectors, the modelslexPrinterExample shows the relationships between callers andfunctions.

In this example, the Function Caller in the SimulinkFunction block addPrintJob, calls the exported Stateflow function queuePrintJob. The subchartBusy calls the Simulink Function blockprinterInk. Tracing lines are drawn into and out of the Stateflowchart.

To open slexPrinterExample, see Monitor Ink Status on Shared Printer Using Simulink Functions.

Simulink Functions Overview- MATLAB & Simulink (11)

Highlight and Animate Function Calls

Use animation to highlight function calls.

This example shows a still of an animated Simulink function call.

Simulink Functions Overview- MATLAB & Simulink (12)

To access animation, in the toolstrip, on the Debug tab, in theEvent Animation section, set the animation speed toSlow, Medium, orFast.

Event Animation is visible when you have event blocks in yourmodel, such as blocks from the Messages & Events library, Stateflow charts, Function-Call Subsystem blocks, Simulink functions, or SimEvents® blocks.

See Also

Simulink Function | Argument Inport | Argument Outport | MATLAB Function | Function Caller

Related Topics

  • Monitor Ink Status on Shared Printer Using Simulink Functions
  • Add a Simulink Function to a Model
  • Simulink Function Blocks in Referenced Models
  • Scoped, Global, and Port-Scoped Simulink Function Blocks Overview
  • Scoped Simulink Function Blocks in Subsystems
  • Scoped Simulink Function Blocks in Models
  • Define Diagnostic Services Using Simulink Functions

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Simulink Functions Overview- MATLAB & Simulink (13)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Simulink Functions Overview
- MATLAB & Simulink (2024)

References

Top Articles
Latest Posts
Article information

Author: Virgilio Hermann JD

Last Updated:

Views: 6350

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Virgilio Hermann JD

Birthday: 1997-12-21

Address: 6946 Schoen Cove, Sipesshire, MO 55944

Phone: +3763365785260

Job: Accounting Engineer

Hobby: Web surfing, Rafting, Dowsing, Stand-up comedy, Ghost hunting, Swimming, Amateur radio

Introduction: My name is Virgilio Hermann JD, I am a fine, gifted, beautiful, encouraging, kind, talented, zealous person who loves writing and wants to share my knowledge and understanding with you.