Guy on Simulink

Simulink & Model-Based Design

System Object Editor Enhancements

Today I am happy to welcome guest blogger Jacob Halbrooks to describe enhancements to the MATLAB Editor to help editing System Objects.

Jacob Halbrooks, guest blogger

In R2016a, MATLAB Editor includes new tools to help you write System objects. These tools appear when you create a new System object or open an existing one. Here, we'll use these tools to help us write a simple moving average filter for use in MATLAB or Simulink.

Enhanced MATLAB Editor

We start by creating a new System object in MATLAB. From the MATLAB Home tab, select New and then choose System Object > Basic. A new document is created with template code for a basic System object. In addition, observe the new System Object section of the toolstrip:


This section includes tools to insert code, display a code outline (Analyze), and preview your System block dialog.

Let's change the name of the System object to "MovingAverageFilter", update the class comment, and save it. This gives us a good outline to start from:

Default System Object Template

Preview dialog

A major feature of the MATLAB System block is that it creates a free dialog for you. In MATLAB Editor, you can now preview the block dialog before you even create a System block or open Simulink. Click the top of the System Block button, and you will see the dialog open:

Dialog Preview

You can keep this dialog open, and it will automatically update as you save changes to your System object.

Insert Properties and States

Our moving average filter should have a public property for the window length and a private property to hold a pre-computed constant, which is a value that is calculated once. In addition, the filter requires a discrete-state property. From the toolstrip, pull down the Insert Property menu. The menu lists the most common types of properties used in System objects as well as a custom option to let you define a property with your choice of attributes. Choose Numeric for the window length property:

Insert Property

A new section of code is inserted for this type of property, and the name of the property is selected so you can easily type to rename it:

Rename Property

Rename the property and give it a default value and comments:

Rename Property

Save the file and note how the dialog updates:

Updated dialog preview

Similarly, add a private property from the Insert Property menu and name it "pCoefficients". The state can be added using the Insert State button on the toolstrip:

Insert State

Remove extra space and the unused properties code block, and the System object now looks like:


Insert Methods

Now let's define the behavior of the System object by implementing its methods:

Defining Methods

While its algorithm has been implemented, we want an additional feature for our System object, which is to validate the input data. In order to discover a method to implement that feature, we can select the Insert Method button. This opens a gallery that lists the complete System object API:

Insert Methods

The gallery shows which methods are already implemented and allows you to browse other methods and select one for insertion. The "Validate inputs" method from the "Inputs and Outputs" group sounds like what we want, so select that. The following code is inserted:

Validate Inputs

Now we implement the method and the System object is complete. The final version is:

Final filter

The System object is now ready to use either in MATLAB or in Simulink using the MATLAB System block!

Now it's your turn

We hope this enhancement to the MATLAB Editor will make you life creating system objects easier. Give it a try and let us know what you think by leaving a comment here.

  • print
  • send email


要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。