Guy and Seth on Simulink

Defining custom shortcuts in Simulink 1

Posted by Guy Rouleau,

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.

1 CommentsOldest to Newest

Hi Seth,
I really like that post, often we as users forget to sharpen our tool before we start working…

Ive encountered an issue using this…

function schema = getItem_TL_SG01_E01_SG01_E10(~)
schema = sl_action_schema;
schema.label = ‘Dark Orange’;
schema.statustip = ['Set color to ' schema.label];
schema.userdata = [252, 213, 180];
schema.accelerator = ‘Ctrl+0′;
schema.callback = @setColor;
end

% ========= getItem_TL_SG01_E02_SG01_E1
function schema = getItem_TL_SG01_E02_SG01_E01(~)
schema = sl_action_schema;
schema.label = ‘AutoSizeOnConnect’;
schema.statustip = [schema.label];
%schema.userdata = [];
schema.accelerator = ‘Ctrl+U’;
schema.callback = @AutoSizeOnConnect_fcn;
end

In this case the the ‘Ctrl+0′ was working but the ‘Ctrl+U’ shortcut does not work… Both work by clicking on the menu item. (when I use an accelerator like (‘Ctrl+Shift+T) or similar, the display in the meny says it is ‘Ctrl+K’)…This is not what i expect… Might it be a reason that I use a different keyboard Layout (German)?

Is there a function that displays me the key that I pressed?

Cheers

Add A Comment

What is 3 + 5?

Preview: hide

These postings are the author's and don't necessarily represent the opinions of MathWorks.