Guy on Simulink

Simulink & Model-Based Design

Defining custom shortcuts in Simulink

On this blog, I sometimes highlight features that it took me a while to appreciate and include in my daily workflow.

I hate to admit it, but quite often those are features that I was aware of for a long time, but for various reasons I was scared of using. Most of the time, I had seen those used in some complex application and I thought it was very complicated to use.

This week I will share something that turns out to be simpler then it looks: Adding menu items in Simulink.

Which task do you often need to do?

In Simulink, we try to add menu items and define shortcuts for common tasks that we know most users need to do. For example, everybody needs to save models, so we have a button on the toolbar and a shortcut (Ctrl+S).

However, if there is a task you do frequently, adding custom menus and shortcuts can significantly speedup your workflow.

My need: Debugging Buses

In my case, I often need to debug large models made of buses, simplifying the problem by removing parts of the model. I reduce the complexity by replacing many blocks with one Constant block outputting a bus signal.

Replacing a bus by a Constant Block

To do this I create bus objects and initialize the bus with a structure. One morning I got tired of typing Simulink.Bus.createObject(bdroot,gcb) and Simulink.Bus.createMATLABStruct, so I decided that I wanted a shortcut.

Creating the sl_custmization.m file

When I realized I could do that with less than 10 lines of code, I was pretty happy:

A simple example of customized menu

Here is how it works:

  • In the first function, I specify that my menu entry will be located in the Tools menu, and the menu content will be described by the function getMyMenuItems.
  • In getMyMenuItems, I output a cell array of functions defining the menu entries. Multiple elements in the cell array can become sub-menus.
  • In the third function createBO, I specify the properties of my entry. It will read Create Bus Object, will have the shortcut Ctrl+Shift+B and when triggered will execute the callback function createBOcb.
  • The last function createBOcb does the work, in this case creating a bus object and a MATLAB structure based on the currently selected block (using gcb).

I could probably make the callback more robust, but I wanted to keep this example as simple as possible to highlight the concept.

The Final Result

Once this file is saved somewhere on my path, I refresh the customizations using sl_refresh_customization and I can use my new entry from the Tools menu, or using Ctrl+Shift+B.

Customized menu

My other customizations

Here are a few other things for which I created shortcuts using sl_customization files

Now it's your turn

For more examples, I recommend looking at the MATLAB Central submission highlighted by Seth in a previous post: Customizing the Simulink Interface Demo by Saurabh Mahapatra.

Do you add menu entries and shortcuts for the tasks you accomplish frequently? What are those tasks? Let us know by leaving a comment here.

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.