Contenuto principale

Add Menu Items to Model Editor Menus

This page shows you how to add menu items and submenus to Stateflow® context menus. To customize the Simulink® context menus, see Customize Simulink Context Menu Using Extension Points. To customize the Simulink Toolstrip, see Create Custom Simulink Toolstrip Tabs.

To add an item to a menu:

  1. For each item, create a schema function that defines the item (see Define Menu Items).

  2. Register the menu customizations with the Simulink customization manager at startup, for example in an sl_customization.m file on the MATLAB® path. See Register Menu Customizations for details.

  3. Create MATLAB callback functions or expressions that take custom actions when you select the custom items.

Code for Adding Menu Items

The following sl_customization.m file adds three items to the bottom of the context menu of the Stateflow model canvas.

function sl_customization(cm)

%% Register custom menu function.
	cm.addCustomMenuFcn('Stateflow:ContextMenu', @getMyMenuItems);
end

%% Define the custom menu function.
function schemaFcns = getMyMenuItems(callbackInfo) 
	schemaFcns = ...
	{@getItem1, ...
	{@getItem2,2} ... %% Pass 2 as user data to getItem2.
	@getItem3}; 
end

%% Define the schema function for first menu item.
function schema = getItem1(callbackInfo)
	schema = sl_action_schema;
	schema.label = 'Item 1';
	schema.userdata = '1';		
	schema.callback = @myCallback1; 
end

function myCallback1(callbackInfo)	
	disp(['Callback for Item ' callbackInfo.userdata ' was called']);
end

function schema = getItem2(callbackInfo)
	% Make a submenu labeled 'Item 2', with the 2 being passed from getMyItems.
	% The submenu contains Item 1 three times.  
	schema = sl_container_schema;
	schema.label = ['Item ' num2str(callbackInfo.userdata)];     
	schema.childrenFcns = {@getItem1, @getItem1, @getItem1};
end 

function myToggleCallback(callbackInfo)
	if strcmp(get_param(gcs,"SolverName"),"FixedStepDiscrete") == 0
		set_param(gcs,SolverName="FixedStepDiscrete");
	else
		set_param(gcs,SolverName="VariableStepAuto");
	end
end

%% Define the schema function for a toggle menu item.
function schema = getItem3(callbackInfo)
	schema = sl_toggle_schema;
	schema.label = 'Variable Solver';
	if strcmp(get_param(gcs,"SolverName"),"VariableStepAuto") == 1
		schema.checked = 'checked';
	else
		schema.checked = 'unchecked';
	end
	schema.callback = @myToggleCallback; 
end

Define Menu Items

You define an item by creating a function that returns a schema object. The schema object specifies the information needed to create the item. The item you define can take a custom action when selected or display a custom submenu.

Defining Menu Items That Trigger Custom Actions

To define an item that takes a custom action, your schema function must accept a callback info object (see Callback Info Object) and create and return an action schema object (see Action Schema Object). The action schema object specifies the item label and a MATLAB callback function or expression that takes the custom action. For example, this schema function defines an item that displays a message.

function schema = getItem1(callbackInfo) 

	%% Create an instance of an action schema.
	schema = sl_action_schema;

	%% Specify the menu item label.
	schema.label = 'Item 1';
	schema.userdata = '1';

	%% Specify the menu item callback function.
	schema.callback = @myCallback1;

end

function myCallback1(callbackInfo)
	disp(['Callback for Item ' callbackInfo.userdata ' was called']); 
end

Action Schema Object.  This object specifies information about items that take custom actions, including the item label and the MATLAB callback function or expression that takes the custom action. Use the function sl_action_schema to create instances of this object in your schema functions. Properties include:

PropertyDescription
tag (Optional)

Action identifier specified as a character vector

For example, if you want to reference the action in a filter function, specify a tag.

label

Label that appears on all items you assign the action, specified as a character vector

state

State of the action:

  • 'Enabled'(default) – The action appears in the menu and can be selected.

  • 'Disabled'– The action appears in the menu but cannot be selected.

  • 'Hidden'– The action does not appear in the menu.

statustip

Text that appears in editor status bar when you select the item, specified as a character vector

userdata

Data that you specify, which can be of any type

accelerator

Ctrl and key combination to use to trigger this action, specified as a character vector of the form 'Ctrl+K', where K is the shortcut key

For example, use 'Ctrl+Alt+T' for an accelerator invoked by holding down Ctrl and Alt and pressing T.

You can only add a keyboard shortcut for custom items that appear on menu bar menus (to be removed) and cannot redefine accelerators that come with the Simulink Editor. For example, Ctrl+D is an accelerator for Update Diagram in the Simulink Editor, so you cannot redefine it.

callback

MATLAB callback function or expression that takes the custom action, specified as a character vector containing the function handle or expression

The function must accept one input argument: a callback info object.

autoDisableWhen

Property that controls when an item is disabled:

  • 'Locked' (default) – When the active editor is locked or when the model is busy

  • 'Busy'– Only if the model is busy

  • 'Never'– Never

Toggle Schema Object.  This object specifies information about an item that toggles an object on or off. Use the sl_toggle_schema function to create instances of this object in your schema functions. Properties include:

PropertyDescription
tag (Optional)

Action identifier specified as a character vector

For example, if you want to reference the toggle action in a filter function, specify a tag.

label

Label that appears on all items you assign the toggle action, specified as a character vector

checked

Option to display a check mark next to the item label:

  • 'unchecked'(default) – No check mark appears.

    'checked'(default) – A check mark appears.

state

State of the toggle action:

  • 'Enabled'(default) – The toggle action appears in the menu and can be selected.

  • 'Disabled'– The toggle action appears in the menu but cannot be selected.

  • 'Hidden'– The toggle action does not appear in the menu.

statustip

Text that appears in editor status bar when you select the item, specified as a character vector

userdata

Data that you specify, which can be of any type

accelerator

Ctrl and key combination to use to trigger this action, specified as a character vector of the form 'Ctrl+K', where K is the shortcut key

For example, use 'Ctrl+Alt+T' for an accelerator invoked by holding down Ctrl and Alt and pressing T.

You can only add a keyboard shortcut for custom items that appear on menu bar menus (to be removed) and cannot redefine accelerators that come with the Simulink Editor. For example, Ctrl+D is an accelerator for Update Diagram in the Simulink Editor, so you cannot redefine it.

callback

MATLAB callback function or expression that takes the custom action, specified as a character vector containing the function handle or expression

The function must accept one input argument: a callback info object.

autoDisableWhen

Property that controls when an item is disabled:

  • 'Locked' (default) – When the active editor is locked or when the model is busy

  • 'Busy'– Only if the model is busy

  • 'Never'– Never

Defining Custom Submenus

To define a submenu, create a schema function that accepts a callback info object and returns a container schema object (see Container Schema Object). The container schema object specifies the schemas that define the items in the submenu. For example, this schema function defines a submenu that contains three instances of Item 1, the item defined in the example in Defining Menu Items That Trigger Custom Actions. The label is defined using the user data of Item 1. The character vector Item is concatenated with the user data 1, resulting in the label Item 1.

function schema = getItem2( callbackInfo )
	schema = sl_container_schema;
	schema.label = ['Item ' num2str(callbackInfo.userdata)]; 
	schema.childrenFcns = {@getItem1, @getItem1, @getItem1};
end

Container Schema Object.  A container schema object specifies a submenu label and its contents. Use the sl_container_schema function to create instances of this object in your schema functions. Object properties include:

PropertyDescription
tag

Action identifier specified as a character vector

For example, if you want to reference the submenu in a filter function, specify a tag.

label

Submenu label, specified as a character vector

state

State of the submenu:

  • 'Enabled'(default) – The submenu appears in the menu and can be selected.

  • 'Disabled' – The submenu appears in the menu but cannot be selected.

  • 'Hidden' – The submenu does not appear in the menu.

statustip

Text that appears in editor status bar when you select an item in the submenu, specified as a character vector

childrenFcns

Contents of the submenu, specified as a cell array of entries, where each entry can be:

  • A reference to a schema function that defines an item in the submenu (see Define Menu Items)

  • A two-element cell array whose first element is a reference to a schema function that defines an item entry and whose second element is data to be inserted as user data in the callback info object (see Callback Info Object) passed to the schema function.

  • 'separator', which causes a separator to appear between the item defined by the preceding entry in the cell array and the item defined in the following entry. The case is ignored for this entry (for example, 'SEPARATOR' and 'Separator' are both valid entries). A separator is also suppressed if it appears at the beginning or end of the submenu and separators that would appear successively are combined into a single separator (for example, as a result of an item being hidden).

For example, this cell array specifies two submenu entries:

{@getItem1, 'separator', {@getItem2, 1}}

In this example, a 1 is passed to getItem2 via a callback info object.

generateFcn

Reference to a function that returns a cell array defining the contents of the submenu

The cell array must have the same format as that specified for the container schema objects childrenFcns property.

The generateFcn property takes precedence over the childrenFcns property. If you set both, the childrenFcns property is ignored and the cell array returned by the generateFcn is used to create the submenu.

userdata

Data of any type that is passed to generateFcn

autoDisableWhen

Property that controls when an item is disabled:

  • 'Locked' (default) – When the active editor is locked or when the model is busy

  • 'Busy'– Only if the model is busy

  • 'Never'– Never

Register Menu Customizations

To view your custom items, you must register the items with the customization manager (see Register Customizations with Simulink). For each menu that you want to customize, your sl_customization function must invoke the customization manager addCustomMenuFcn method. Each invocation should pass the tag of the menu (see Menu Tags) to be customized and a custom menu function that specifies the items to be added to the menu (see Creating the Custom Menu Function). For example, the following sl_customization function adds custom items to the Stateflow context menu.

function sl_customization(cm)
	cm.addCustomMenuFcn('Stateflow:ContextMenu', @getMyMenuItems);
end

Creating the Custom Menu Function

The custom menu function returns a cell array of schema functions that define custom items that you want to appear on the model editor menus (see Define Menu Items ). The custom menu function returns a cell array similar to that returned by the generateFcn function.

Your custom menu function should accept a callback info object (see Callback Info Object) and return a cell array that lists the schema functions. Each element of the cell array can be either a handle to a schema function or a two-element cell array whose first element is a handle to a schema function and whose second element is user-defined data to be passed to the schema function. For example, the following custom menu function returns a cell array that lists three schema functions.

function schemaFcns = getMyMenuItems(callbackInfo)
	schemaFcns = ...
	{@getItem1, ...
	{@getItem2,2} ... %% Pass 2 as user data to getItem2.
	@getItem3}; 
end

Callback Info Object

Instances of these objects are passed to menu customization functions. Methods and properties of these objects include:

Method or PropertyDescription
uiObject

Method to get the handle to the owner of the menu for which this is the callback

The owner can be the Simulink Editor or the Stateflow Editor.

model

Method to get the handle to the model being displayed in the editor window

userdata

User data property, which can be any data type

Debugging Custom Menu Callbacks

On systems using the Microsoft® Windows® operating system, selecting a custom menu item whose callback contains a breakpoint can cause the mouse to become unresponsive or the menu to remain open and on top of other windows. To fix these problems, use the MATLAB code debugger keyboard commands to continue execution of the callback.

Menu Tags

A menu tag identifies an editor or context menu. To add items to a menu, you must know the menu tag (see Register Menu Customizations).

Menus bound to Stateflow:FileMenu only appear when the Stateflow Editor is active.

TagWhat It Adds
Stateflow:PreContextMenuItem to the beginning of a Stateflow Editor context menu.
Stateflow:ContextMenuItems to the end of a Stateflow Editor context menu.

See Also

Topics