Create Custom Blocks Using MATLAB System Block and System objects - MATLAB & Simulink (original) (raw)

This topic introduces the MATLAB System block, explains how to use the block to implement your System object™, and describes how to configure the block for your Simulink® model.

System Objects

System objects let you implement algorithms using the MATLAB® language. The MATLAB System block enables you to use System objects in Simulink.

Before you use a MATLAB System block, you must have a System object to associate with the MATLAB System block. A System object is a specialized kind of MATLAB class. System objects are designed specifically for implementing and simulating dynamic systems with inputs that change over time. For more information on creating System objects, see Customize System Objects for Simulink.

Why Use the MATLAB System Block?

The MATLAB System block lets you:

System objects exist in other MATLAB products. MATLAB System block supports only the System objects written in the MATLAB language. In addition, if a System object has a corresponding Simulink block, you cannot implement a MATLAB System block for it.

Note

To use your System object in the Simulink environment with MATLAB System block, it must have a constructor that you can call with no arguments. By default, the System object constructor has this capability and you do not need to define your own constructor. However, if you create your own System object constructor, you must be able to call it with no arguments.

There are several mechanisms for including MATLAB algorithms in Simulink, such as:

For help on choosing the right block, see Comparison of Custom Block Functionality.

To implement your System object with a custom block behavior in Simulink use the MATLAB System Block and follow these steps:

  1. Create a new model and add the MATLAB System block to your model.
  2. In the block dialog box, from the New list, selectBasic, Advanced, or Simulink Extension if you want to create a new System object from a template. Modify the template according to your needs and save the System object.
  3. Enter the full path name for the System object in the System object name. Click the list arrow. If valid System objects exist in the current folder, the names appear in the list.
    The MATLAB System block icon and port labels update to those of the corresponding System object. For example, suppose you selected a System object named lmsSysObj in your current folder. The block updates as shown in the figure:
  4. Double-click the block. The MATLAB System block dialog box reflects the System object parameters. The dialog box includes a Source code if the System object uses MATLAB language.
    It does not appear if you have:
    • Converted the System object to P-code.
    • Overridden the default behavior using the getHeaderImpl method.
  5. Click Source code and observe that the public and active properties in the System object appear in the MATLAB System block dialog box as block parameters.
  6. Select how you want the model to simulate the block using theSimulate using parameter. (This parameter appears at the bottom of each MATLAB System block if there is only one tab, or the bottom of the first of multiple tabs.)

Note

After you associate the block with a System object class name, you cannot assign a new System object using the same MATLAB System block dialog box. Instead, right-click the MATLAB System block, selectBlock Parameters (MATLABSystem) and enter a new class name in System object name.

To implement a block with another System object, right-click the MATLAB System block and selectBlock Parameters (MATLABSystem). Then, use the block dialog box to identify a new class name in System object name. For more information, see Implement a System object in Simulink Using MATLAB System Block.

Change Block Icon and Port Labels

You can customize the MATLAB System icon, port labels, and dialog box using the Mask Editor or by adding specific methods to your System object.

Change the MATLAB System Block Icon to an Image

You can change the image of MATLAB System block in MATLAB Editor. For a list of accepted image files, see image. To use an existing image file for the MATLAB System block:

  1. Double-click your MATLAB System block.
  2. In the block dialog box, click the Source code. The MATLAB Editor that contains the System object code opens.
  3. In the MATLAB Editor, from the System Block drop-down list, select Add Image Icon.
  4. In the Add image icon dialog box, clickBrowse to select an image of your choice.
  5. Click OK to insert the corresponding code for the getIconImpl method in your System object.

For more information, see Customize System Block Appearance Programmatically.

Specify Sample Time for MATLAB System Block

The sample time of a block is a parameter that indicates when the block produces an output, and eventually updates its internal state. To specify sample time for aMATLAB System block, implement the getSampleTimeImpl method with a call to createSampleTime method. To query the MATLAB System block for current sample time and simulation time, use the getSampleTime and getCurrentTime methods. For more information, see Specify Sample Time for MATLAB System Block System Objects.

For more information, see Types of Sample Time. To see an example on how to control the sample time of the MATLAB System block using System object methods, see Specify Sample Time for MATLAB System Block System Objects.

Block Simulation Modes

You can use MATLAB System blocks in Simulink models for simulation via interpreted execution or code generation.

Interpreted Execution vs. Code Generation

Implementing a MATLAB System block with a valid System object class name enables the Simulate using parameter. This parameter appears at the bottom of the MATLAB System block dialog box if there is only one tab, or the bottom of the first of multiple tabs. Use the Simulate using parameter to control how the block simulates. This table describes how to choose the right value for your purpose.

Action Select Pros Cons
Upon first model run, simulate and generate code forMATLAB System using only the subset of MATLAB functions supported for code generation. Choosing this option causes the simulation to run the generated code. Code generation (default) Potentially better performance. System object is limited to the subset of MATLAB functions supported for code generation. Simulation may start more slowly.
Simulate model using all supported MATLAB functions. Choosing this option can slow simulation performance. Interpreted execution System object can contain any supported MATLAB function. Faster startup time. Potentially slower performance. If the MATLAB functions in the System object do not support code generation, the System object must contain propagation methods.

To take advantage of faster performance, consider using propagation methods in your System object. For more information, see Add and Implement Propagation Methods.

Note

If your Simulink model is set up to simulate in Normal mode and if you useMATLAB System block via code generation, individualMATLAB System blocks can take advantage of acceleration via code generation. If the Simulink model is set up to simulate in accelerator mode or any of the other target modes, MATLAB System block's Simulate using parameter does not have any effect. However, there is one exception, if the model is set up to simulate in accelerator mode and you use MATLAB System block with interpreted execution mode, the block will run in interpreted execution mode.

Simulation Using Code Generation

Simulating the MATLAB System block using code generation requires that you have a compatible compiler installed in your system. To see a list of supported compilers, open Supported and Compatible Compilers, click the tab that corresponds to your operating system, find the Simulink Product Family table, and go to the **For Model Referencing, Accelerator mode, Rapid Accelerator mode, and MATLAB Function blocks column.

A model can have multiple copies of the same MATLAB System block. Blocks are considered the same if they:

When the model has multiple copies of the same block, the software does not regenerate the code for each block. The model reuses the code from the first time code was generated for the block.

Default Input Signal Attributes of MATLAB System Block

If a MATLAB System block has one or more inputs that are not connected to another block’s output port or connected to a port that has underspecified attributes, the default input signal attributes for the unspecified attributes are:

Data Attribute Default
Data Type double
Size [1 1] scalar
Complexity real

Use Nonvirtual Buses with MATLAB System Block

The MATLAB System block supports nonvirtual buses as input and output signals. The corresponding System object input or output must be a MATLAB structure whose fields match those defined by the nonvirtual bus. If the System object output is a MATLAB structure, it must define propagator methods. In addition, the getOutputDataTypeImpl method must return the name of the corresponding bus object. This bus object must exist in the base workspace or a data dictionary linked to the model. For an example, see Using Buses with MATLAB System Blocks.

Note

If the output is the same bus type as the input, do not use the propagatedInputDataType method to obtain the name of the bus object. Instead, you must return the name of the bus object directly.

MATLAB System Block Limitations

These capabilities are not supported.

Category Limitation Workaround
System Objects Tunable character vector properties of the System object are nontunable parameters in the MATLAB System block.
Data Types The MATLAB System block does not support virtual buses as input or output.System objects cannot use user-defined opaque data types.
Sample Time Cannot use MATLAB System blocks to model continuous time or multirate systems.
Linearizations Cannot use Jacobian based linearization.
Global Variables Global variables defined in the model Configuration Parameters > pane and referenced by the System object are not shared with Stateflow® and the MATLAB Function block. Turn on the in the Simulation Target pane of the Configuration Parameters dialog box.
Debugging MATLAB debugging for code-generation-based simulation. Set the MATLAB System blockSimulate using parameter toInterpreted execution, and then debug. When you are done, set Simulate using back to Code generation.
Fixed-Point Tool The Fixed-Point Tool does not return design min/max, min/max logging, or autoscaling information for MATLAB System blocks.
Model coverage analysis (Simulink Coverage™ software) Simulink Coverage cannot perform model analysis for MATLAB System block with Simulate using parameter set to Interpreted execution.

MATLAB System Block and System Objects Examples

For examples of MATLAB System and System objects, see: