Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

Embedded MATLAB Function Editor

Customizing the Embedded MATLAB Editor

Use the toolbar icons to customize the appearance of the Embedded MATLAB Editor in the same manner as the MATLAB editor. See Arranging the Desktop in the MATLAB documentation.

Embedded MATLAB Editor Tools

The following tools are specific to Embedded MATLAB:

Tool ButtonDescription

Edit Data/Ports

Opens the Ports and Data Manager dialog to add or modify arguments for the current Embedded MATLAB Function block (see Ports and Data Manager). You can also open this dialog by selecting Edit Data/Ports from the Tools menu.

To define and modify input and output arguments for any Embedded MATLAB Function block in the model hierarchy, use the Model Explorer, which you can open from the Tools menu.

Open Compilation Report

Opens the compilation report for the Embedded MATLAB Function block. For more information, see Working with Compilation Reports.

Simulation Target

Opens the Simulation Target pane in the Configuration Parameters dialog to enable debugging or include custom code. See Enabling and Disabling Debugging for more information on debugging, and Including C Functions in Simulation Targets in the Embedded MATLAB documentation for more information on including custom code.

Go To Diagram

Displays the Embedded MATLAB function in its native diagram without closing the Embedded MATLAB Editor.

Update Ports

Updates the ports of the Embedded MATLAB Function block with the latest changes made to the function argument and return values without closing the Embedded MATLAB Editor.

See Defining Inputs and Outputs for an example of defining an input argument for an Embedded MATLAB Function block.

Editing and Debugging Embedded MATLAB Code

Commenting Out a Block of Code

To comment out a block of code in the Embedded MATLAB editor:

  1. Highlight the text that you would like to comment out.

  2. Hold the mouse over the highlighted text and then right-click and select Comment from the context menu. (Alternatively, select Comment from the Text menu).

For more information, refer to Adding Comments in the MATLAB Desktop Tools and Development Environment documentation.

Manual Indenting

To indent a block of code manually:

  1. Highlight the text that you would like to indent.

  2. Hold the mouse over the highlighted text and then right-click and select one of the following options from the context menu:

    • Smart Indent to indent lines that start with keyword functions or that follow lines containing certain keyword functions. (Alternatively, select Smart Indent from the Text menu).

    • Decrease Indent to move selected lines further to the left. (Alternatively, select Decrease Indent from the Text menu).

    • Increase Indent to move selected lines further to the right. (Alternatively, select Increase Indent from the Text menu).

For more information, refer to Indenting in the MATLAB Desktop Tools and Development Environment documentation.

Opening a Selection

You can open a subfunction, function, file, or variable from within a file in the Embedded MATLAB Function Editor.

To open a selection:

  1. Position the cursor in the name of the item you would like to open.

  2. Right-click and select Open Selection from the context menu.

The Editor chooses the appropriate tool to open the selection. For more information, refer to in the MATLAB Desktop Tools and Development Environment documentation

Evaluating a Selection

You can use the Evaluate a Selection menu option to report the value for an Embedded MATLAB function variable or equation in the MATLAB window during simulation.

To evaluate a selection:

  1. Highlight the variable or equation that you would like to evaluate.

  2. Hold the mouse over the highlighted text and then right-click and select Evaluate Selection from the context menu. (Alternatively, select Evaluate Selection from the Text menu).

When you reach a breakpoint, the MATLAB command Window displays the value of the variable or equation at the Command Line Debugger prompt.

debug>> stdev
 
	1.1180

debug>> 

Setting Data Scope

To set the data scope of an Embedded MATLAB Function block input parameter:

  1. Highlight the input parameter that you would like to modify.

  2. Hold the mouse over the highlighted text and then right-click and select Data Scope from the context menu.

  3. Select:

    • Input if your input data is provided by the Simulink model via an input port to the Embedded MATLAB Function block.

    • Parameter if your input is a variable of the same name in the MATLAB or model workspace or in the workspace of a masked subsystem containing this block.

For more information, refer to Setting General Properties in the Ports and Data Manager documentation.

Ports and Data Manager

The Ports and Data Manager provides a convenient method for defining objects and modifying their properties in an Embedded MATLAB Function block that is open and has focus.

The Ports and Data Manager provides the same data definition capabilities as the Model Explorer, but supports only individual Embedded MATLAB Function blocks. To modify objects and properties for blocks across the model hierarchy, use the The Model Explorer.

Ports and Data Manager Dialog

The Ports and Data Manager dialog allows you to add and define data arguments, input triggers, and function call outputs for Embedded MATLAB Function blocks. Using this dialog, you can also modify properties for the Embedded MATLAB Function block and the objects it contains.

The dialog consists of two panes:

Properties vary according to the scope and type of the object. Therefore, the Ports and Data Manager properties dialogs are dynamic, displaying only the property fields that are relevant for the object you add or modify.

When you first open the dialog, it displays the properties of the Embedded MATLAB Function block.

Opening the Ports and Data Manager

To open the Ports and Data Manger from the Embedded MATLAB Editor, select Tools > Edit Data/Ports or click the Edit Data/Ports icon .

The Ports and Data Manager appears for the Embedded MATLAB Function block that is open and has focus.

Ports and Data Manager Tools

The following tools are specific to the Ports and Data Manager:

Tool ButtonDescription

Goto Block Editor

Displays the Embedded MATLAB function in the Embedded MATLAB Editor.

Show Block Dialog

Displays the default Embedded MATLAB function properties (see Setting Embedded MATLAB Function Block Properties). Use this button to return to the settings used by the block after viewing data associated with the block arguments.

Setting Embedded MATLAB Function Block Properties

The Dialog pane for an Embedded MATLAB Function block looks like this:

This section describes each property of an Embedded MATLAB Function block.

Name.   Name of the Embedded MATLAB Function block, following the same naming conventions as for Simulink blocks (see Manipulating Block Names).

Update method.   Method for activating the Embedded MATLAB Function block. You can choose from the following update methods:

Update MethodDescription
Inherited
(default)
Input from the Simulink model activates the Embedded MATLAB Function block.

If you define an input trigger, the Embedded MATLAB Function block executes in response to a Simulink signal or function-call event on the trigger port. If you do not define an input trigger, the Embedded MATLAB Function block implicitly inherits triggers from the model. These implicit events are the sample times (discrete or continuous) of the signals that provide inputs to the chart.

If you define data inputs, the Embedded MATLAB Function block samples at the rate of the fastest data input. If you do not define data inputs, the Embedded MATLAB Function block samples as defined by its parent subsystem's execution behavior.
DiscreteThe Embedded MATLAB Function block is sampled at the rate you specify as the block's Sample Time property. An implicit event is generated at regular time intervals corresponding to the specified rate. The sample time is in the same units as the Simulink simulation time. Note that other blocks in the model can have different sample times.
ContinuousThe Simulink software wakes up (samples) the Embedded MATLAB Function block at each step in the simulation, as well as at intermediate time points that can be requested by the solver. This method is consistent with the continuous method.

Saturate on integer overflow.   Option that determines how the Embedded MATLAB Function block handles overflow conditions during integer operations:

SettingAction When Overflow Occurs
Enabled
(default)
Saturates an integer by setting it to the maximum positive or negative value allowed by the word size. Matches MATLAB behavior.
DisabledIn simulation mode, generates a runtime error. For Real-Time Workshop code generation, the behavior depends on your C language compiler.

When you enable Saturate on integer overflow, the Embedded MATLAB Function block adds additional checks in the generated code to detect integer overflow or underflow. Therefore, it is more efficient to disable this option if you are sure that integer overflow and underflow will not occur in your Embedded MATLAB function code.

Even when you disable this option, the code for a simulation target checks for integer overflow and underflow. If either condition occurs, simulation stops and an error is generated. If you enabled debugging for the Embedded MATLAB Function block, the debugger displays the error and lets you examine the data.

If you have not enabled debugging for the Embedded MATLAB Function block, the block generates a runtime error, as in this example:

It is important to note that the code for a Real-Time Workshop target does not check for integer overflow or underflow and, therefore, may produce unpredictable results when Saturate on integer overflow is disabled. In this situation, it is recommended that you simulate first to test for overflow and underflow before generating the Real-Time Workshop target.

Lock Editor.   Option for locking the Embedded MATLAB Editor. When enabled, this option prevents users from making changes to the Embedded MATLAB Function block.

Treat these inherited Simulink signal types as fi objects.   Parameter that applies to Embedded MATLAB Function blocks in models that use fixed-point or integer data types. You can control how the Embedded MATLAB Function block handles Simulink input signals using the Treat these inherited Simulink signal types as fi objects parameter.

PropertyDescription
Treat these inherited Simulink signal types as fi objects

Determines whether to treat inherited fixed-point and integer signals as Fixed-Point Toolbox fi objects.

  • When you select Fixed-point, the Embedded MATLAB Function block treats all fixed-point inputs as Fixed-Point Toolbox fi objects.

  • When you select Fixed-Point & Integer, the Embedded MATLAB Function block treats all fixed-point and integer inputs as Fixed-Point Toolbox fi objects.

Embedded MATLAB Function block fimath.   Setting that defines fimath properties for the Embedded MATLAB Function block. The block associates the fimath properties you specify with the following objects:

You can select one of the following options for the Embedded MATLAB Function block fimath.

SettingDescription
Same as MATLAB

When you select this option, the block uses the same fimath properties as the current global fimath. The edit box appears dimmed and displays the current global fimath in read-only form.

For more information on the global fimath, see Working with the Global fimath in the Fixed-Point Toolbox documentation.

Specify other

When you select this option, you can specify your own fimath object in the edit box. You can do so in one of two ways:

  • Constructing the fimath object inside the edit box.

  • Constructing the fimath object in the MATLAB or model workspace and then entering its variable name in the edit box. If you use this option and plan to share your model with others, make sure you define the variable in the model workspace. See Sharing Models with Fixed-Point Embedded MATLAB Function Blocks in the Fixed-Point Toolbox documentation for more information.

For more information on fimath objects, see Working with fimath Objects in the Fixed-Point Toolbox documentation.

Description.   Description of the Embedded MATLAB Function block.

Document link.   Link to documentation for the Embedded MATLAB Function block. To document an Embedded MATLAB Function block, set the Document link property to a Web URL address or MATLAB expression that displays documentation in a suitable format (for example, an HTML file or text in the MATLAB Command Window). The Embedded MATLAB Function block evaluates the expression when you click the blue Document link text.

Adding Data to an Embedded MATLAB Function Block

You can define input and output data arguments for an Embedded MATLAB Function block directly in the script, or by using the Ports and Data Manager or Model Explorer. You can use the Ports and Data Manager to add data arguments to an Embedded MATLAB Function block that is open and has focus. You can also modify the properties of data arguments in the block.

You can define data arguments for Embedded MATLAB Function blocks in the following methods:

MethodFor DefiningReference
Define data directly in the Embedded MATLAB function scriptInput and output dataSee Defining Inputs and Outputs.
Use the Ports and Data ManagerInput, output, and parameter data in the Embedded MATLAB Function block that is open and has focusSee Defining Data in the Ports and Data Manager.
Use the Model ExplorerInput, output, and parameter data in Embedded MATLAB Function blocks at all levels of the model hierarchySee Defining Data in the Model Explorer

Defining Data in the Ports and Data Manager.   To add a data argument and modify its properties, follow these steps:

  1. In the Ports and Data Manager, click the Add Data icon .

    The Ports and Data Manager adds a default definition of the data to the Embedded MATLAB Function block and displays the new data argument.

  2. Select the row containing the new data argument.

  3. Select the data property you want to modify, and specify a new value, as in this example:

  4. Repeat step 3 to specify values for other data properties.

  5. Return to the Embedded MATLAB Function block properties at any time by clicking the Show Block Dialog icon .

Defining Data in the Model Explorer.   The Data properties dialog in the Model Explorer allows you to set and modify the properties of data arguments in Embedded MATLAB Function blocks. Properties vary according to the scope and type of the data object. Therefore, the Data properties dialog is dynamic, displaying only the property fields that are relevant for the data argument you are defining.

Open the Data properties dialog by selecting a data argument in the Contents pane.

The Data properties dialog provides a set of tabbed panes, as in this example:

Each tab lets you define different features of your data argument:

Setting General Properties.   The General tab of the Data properties dialog looks like this:

You can set the following properties in the General tab:

PropertyDescription
NameName of the data argument, following the same naming conventions used in MATLAB (see Naming Variables.
Scope

Where data resides in memory, relative to its parent. Scope determines the range of functionality of the data argument. You can set scope to one of the following values:

  • Parameter— Specifies that the source for this data is a variable of the same name in the MATLAB or model workspace or in the workspace of a masked subsystem containing this block. If a variable of the same name exists in more than one of the workspaces visible to the block, the variable closest to the block in the in the workspace hierarchy is used (see Using Model Workspaces).

  • Input— Data provided by the model via an input port to the Embedded MATLAB Function block.

  • Output— Data provided by the Embedded MATLAB Function block via an output port to the model.

For more information, see Defining Inputs and Outputs and Parameter Arguments in Embedded MATLAB Functions.

PortIndex of the port associated with the data argument. This property applies only to input and output data.
TunableIndicates whether the parameter used as the source of this data item is tunable (see Tunable Parameters). This property applies only to parameter data. You must clear this option if you want to use the parameter where Embedded MATLAB requires a constant expression, such as zeros (see entry for zeros in Embedded MATLAB Function Library — Alphabetical List in the Embedded MATLAB documentation).
Data must resolve to Simulink signal objectSpecifies that the data argument must resolve to a Simulink signal object. This property applies only to output data. See Resolving Symbols for more information.
SizeSize of the data argument. Size can be a scalar value or a MATLAB vector of values. Size defaults to –1, which means that it is inherited, as described in Inheriting Argument Sizes from Simulink.

For more details, see Sizing Function Arguments.

ComplexityIndicates real or complex data arguments. You can set complexity to one of the following values:
  • Off— Data argument is a real number

  • On— Data argument is a complex number

  • Inherited— Data argument inherits complexity based on its scope. Input and output data inherit complexity from the Simulink signals connected to them; parameter data inherits complexity from the parameter to which it is bound.

Sampling modeSpecifies how an output signal propagates through a model. This property applies only to data with scope equal to Output. You can set sampling mode to one of the following values:
  • Sample based: Propagate the signal sample by sample (default)

  • Frame based: Propagate the signal in batches of samples

TypeType of data object. You can specify the data type by:
  • Selecting a built-in type from the Type drop down list.

  • Entering an expression in the Type field that evaluates to a data type (see Working with Data Types in the Simulink User's Guide).

  • Using the Data Type Assistant to specify a data Mode, then specifying the data type based on that mode.

      Note   Click the Show data type assistant button to display the Data Type Assistant.

For more information, see Specifying Argument Types.

Setting Value Attributes Properties.   The Value Attributes tab of the Data properties dialog looks like this:

You can set the following properties on the Value Attributes tab:

PropertyDescription
Save final value to base workspaceThe Embedded MATLAB Function block assigns the value of the data argument to a variable of the same name in the MATLAB base workspace at the end of simulation.
Limit range properties

Specify the range of acceptable values for input or output data. The Embedded MATLAB Function block uses this range to validate the input or output as it enters or leaves the block. You can enter an expression or parameter that evaluates to a numeric scalar value.

  • Minimum — The smallest value allowed for the data item during simulation. The default value is -inf.

  • Maximum — The largest value allowed for the data item during simulation. The default value is inf.

Setting Description Properties.   The Description tab of the Data properties dialog looks like this:

You can set the following properties on the Description tab:

PropertyDescription
DescriptionDescription of the data argument.
Document linkLink to documentation for the data argument. You can enter a Web URL address or a MATLAB command that displays documentation in a suitable format, such as an HTML file or text in the MATLAB Command Window. When you click the blue text, Document link, displayed at the bottom of the Data properties dialog, the Embedded MATLAB Function block evaluates the link and displays the documentation.

Adding Input Triggers to an Embedded MATLAB Function Block

You can use the Ports and Data Manager to add input triggers to an Embedded MATLAB Function block that is open and has focus. You can also modify the properties of input triggers in the block.

To add an input trigger and modify its properties, follow these steps:

  1. In the Ports and Data Manager, click the Add Input Trigger icon .

    The Ports and Data Manager adds a default definition of the new input trigger to the Embedded MATLAB Function block and displays the Trigger properties dialog.

  2. Modify properties for the new input trigger, using one of the following methods:

    • In the Contents pane, select the row that contains the input trigger you want to modify and then edit the property of interest, as in this example:

    • Modify fields in the Trigger properties dialog, as described in The Trigger Properties Dialog.

    • Return to the Embedded MATLAB Function block properties at any time by clicking the Show Block Dialog icon .

The Trigger Properties Dialog.   The Trigger properties dialog in the Ports and Data Manager allows you to set and modify the properties of input triggers in Embedded MATLAB Function blocks.

You can open the Trigger properties dialog using one of these methods:

The Trigger properties dialog looks like this:

Setting Input Trigger Properties.   You can set the following properties in the Trigger properties dialog:

PropertyDescription
NameName of the input trigger, following the same naming conventions used in MATLAB (see Naming Variables .
PortIndex of the port associated with the input trigger. The default value is 1.
TriggerType of event that triggers execution of the Embedded MATLAB Function block. You can select one of the following types of triggers:
  • Rising (default) — Triggers execution of the Embedded MATLAB Function block when the control signal rises from a negative or zero value to a positive value (or zero if the initial value is negative).

  • Falling— Triggers execution of the Embedded MATLAB Function block when the control signal falls from a positive or zero value to a negative value (or zero if the initial value is positive).

  • Either— Triggers execution of the Embedded MATLAB Function block when the control signal is either rising or falling.

  • Function call— Triggers execution of the Embedded MATLAB Function block from a block that outputs function-call events, or from an S-function

DescriptionDescription of the input trigger.
Document linkLink to documentation for the input trigger. You can enter a Web URL address or a MATLAB command that displays documentation in a suitable format, such as an HTML file or text in the MATLAB Command Window. When you click the blue text that reads Document link displayed at the bottom of the Trigger properties dialog, the Embedded MATLAB Function block evaluates the link and displays the documentation.

Adding Function Call Outputs to an Embedded MATLAB Function Block

You can use the Ports and Data Manager to add and modify function call outputs to an Embedded MATLAB Function block that is open and has focus. You can also modify the properties of function call outputs in the block.

To add a function call output and modify its properties, follow these steps:

  1. In the Ports and Data Manager, click the Add Function Call Output icon .

    The Ports and Data Manager adds a default definition of the new function call output to the Embedded MATLAB Function block and displays the Function Call properties dialog.

  2. Modify properties for the new function call output, using one of the following methods:

    • In the Contents pane, select the row that contains the function call output you want to modify and then edit the property of interest, as in this example:

    • Modify fields in the Function Call properties dialog, as described in The Function Call Properties Dialog.

    • Return to the Embedded MATLAB Function block properties at any time by clicking the Show Block Dialog icon .

The Function Call Properties Dialog.   The Function Call properties dialog in the Ports and Data Manager allows you to edit the properties of function call outputs in Embedded MATLAB Function blocks.

You can open the Function Call properties dialog in the Dialog pane by selecting a function call output in the Contents pane of the Ports and Data Manager.

The Function Call properties dialog looks like this:

Setting Function Call Output Properties.   You can set the following properties in the Function Call properties dialog:

PropertyDescription
NameName of the function call output, following the same naming conventions used in MATLAB (see Naming Variables in the MATLAB documentation.
PortIndex of the port associated with the function call output. The default value is 3.
DescriptionDescription of the function call output.
Document linkLink to documentation for the function call output. You can enter a Web URL address or a MATLAB command that displays documentation in a suitable format, such as an HTML file or text in the MATLAB Command Window. When you click Document link displayed at the bottom of the Function Call properties dialog, the Embedded MATLAB Function block evaluates the link and displays the documentation.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS