Main Content

slConvertCustomMenus

Convert custom toolbar menu into toolstrip tab

Description

example

slConvertCustomMenus converts a custom Simulink® toolbar menu into a toolstrip tab that is stored in a toolstrip component called custom in the current folder. The new tab has the title Custom and contains a single section with the same name as the top level of the toolbar menu. The section is populated with the converted toolbar menu items:

  • Action schemas and toggle schemas are converted into push buttons.

  • Container schemas are converted into drop-down buttons.

The toolbar menu is not automatically deleted during the conversion. To remove it, delete or comment out the corresponding addCustomMenuFcn call in the sl_customization.m file, save the file, and rebuild the toolstrip by entering sl_refresh_customizations in the MATLAB® Command Window.

If a toolstrip component called custom already exists in the current folder, a prompt will appear in the Command Window asking you for permission to overwrite the component.

When the conversion is complete, the new tab can be edited like any other custom toolstrip tab. You can adjust the layout of the tab, its labels, and its functionality. See Create Custom Simulink Toolstrip Tabs for details.

slConvertCustomMenus(Name=Value) converts a custom Simulink toolbar menu into a toolstrip tab that is stored in a toolstrip component whose name and resource location can be specified by name-value arguments.

Permission to overwrite an existing toolstrip component of the same name and at the same location as the one that the conversion creates can be specified via name-value argument as well. If you do not specify this argument and such a component exists, a prompt will appear in the Command Window asking you for permission to overwrite the component.

Examples

collapse all

Start with the custom Simulink toolbar menu defined by this sl_customization.m file.

function sl_customization( cm )
    cm.addCustomMenuFcn( 'Simulink:ToolsMenu', @toolsMenu );
end
 
%% Tool Menu customization
function schemas = toolsMenu()
        schemas = {
        @toolsMenu1, ...
        @toolsMenu2, ...
        @toolsMenu3
    };
end
 
function schema = toolsMenu1( cbinfo )
    schema = sl_action_schema;
    schema.tag = 'toolsMenu1';
    schema.label = 'Tools Menu 1';
    schema.callback = @toolsMenu1CB;
end

function schema = toolsMenu2( cbinfo )
    schema = sl_action_schema;
    schema.tag = 'toolsMenu2';
    schema.label = 'Tools Menu 2';
    schema.callback = @toolsMenu2CB;
end
 
function schema = toolsMenu3( cbinfo )
    schema = sl_container_schema;
    schema.tag = 'toolsMenu3';
    schema.label = 'Tools Menu 3';
    schema.childrenFcns = {
    @toolsMenu31, ...
    @toolsMenu32
    };
end

function schema = toolsMenu31( cbinfo )
    schema = sl_action_schema;
    schema.tag = 'toolsMenu31';
    schema.label = 'Tools Menu 31';
    schema.callback = @toolsMenu31CB;
end
 
function schema = toolsMenu32( cbinfo )
    schema = sl_action_schema;
    schema.tag = 'toolsMenu32';
    schema.label = 'Tools Menu 32';
    schema.callback = @toolsMenu32CB;
end
 
function toolsMenu1CB( cbinfo )
    disp( 'Executing toolsMenu1' );
end

function toolsMenu2CB( cbinfo )
    disp( 'Executing toolsMenu2' );
end

function toolsMenu31CB( cbinfo )
    disp( 'Executing toolsMenu31' );
end
 
function toolsMenu32CB( cbinfo )
    disp( 'Executing toolsMenu32' );
end

To see the toolbar menu, open Simulink.

Custom toolbar menu labeled Tools that consists of two list items without pop-up lists and one list item with pop-up lists

If this menu is converted in its current state, the conversion of its callback functions will fail because they are saved in the sl_customization.m file. To enable the conversion of the callback functions for Tools Menu 2 and Tools Menu 3, save them in separate function files: toolsMenu2CB.m, toolsMenu31CB.m, and toolsMenu32CB.m.

For comparison, leave the callback function for Tools Menu 1 in the sl_customization.m file.

Convert the toolbar menu into a toolstrip tab. In the Command Window, enter this:

slConvertCustomMenus;

Delete the custom toolbar menu by commenting out the addCustomMenuFcn call in the sl_customization.m file, and then saving the file.

function sl_customization( cm )
    %cm.addCustomMenuFcn( 'Simulink:ToolsMenu', @toolsMenu );
end

To see the results of the menu conversion, open a blank model in Simulink.

Rebuild the toolstrip by entering the sl_refresh_customizations command in the Command Window.

Open and view the Custom tab.

Custom toolstrip tab generated by converting the toolbar menu, consisting of a single tab labeled TOOLS, with two push buttons and one drop-down button

The tab has a single section titled Tools because that is the title of the top level of the toolbar menu from which the tab was created.

The action schemas Tools Menu 1 and Tools Menu 2 from the toolbar menu are converted into push buttons of the same names in the tab.

The container schema Tools Menu 3 in the toolbar menu has a pop-up list, and is converted into a drop-down button.

Click on the Tools Menu 1 push button. The diagnostic viewer shows an error message, stating that the callback of the Tools Menu 1 action schema failed to convert. This is because the callback function of that schema is saved in the sl_customization.m file.

Click on the Tools Menu 2, Tools Menu 31, and Tools Menu 32 buttons. The callbacks execute without error because the callback functions for these buttons are saved in separate function files.

Input Arguments

collapse all

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: slConvertCustomMenus(CompName="Name",CompFolder="Folder")

Name of toolstrip component, specified as a string or character vector.

Data Types: char | string

File path of toolstrip component, specified as a string or character vector.

Data Types: char | string

Permission to overwrite an existing toolstrip component of the same name and at the same location as the one that the conversion creates, specified as a numeric or logical 1 (true) or 0 (false).

Data Types: logical

Limitations

  • When converting a custom toolbar menu into a toolstrip tab, any functionality that custom tabs do not support is ignored. However, you can edit the generated tab. For more information, see Create Custom Simulink Toolstrip Tabs.

  • If a toolstrip component with the name specified by CompName already exists in the CompFolder location at the time of conversion, a prompt will appear in the Command Window asking you for permission to overwrite the component. If a component of another name already exists in the CompFolder location, the menu cannot be converted.

  • The conversion of a schema callback fails if its function handle points to a function local to the sl_customization.m file. Move all callback functions into separate function files.

Tips

To delete custom tab elements that are the result of a conversion, you can do one of the following:

Version History

Introduced in R2022a