Code covered by the BSD License  

Highlights from
Customizing the Simulink Interface Demo

image thumbnail

Customizing the Simulink Interface Demo

by

 

This demo walks you through the steps of being able to customize the Simulink interface.

Customizing Simulink Graphical User Interface

Customizing Simulink Graphical User Interface

This demonstration shows how you can customize the Simulink graphical user interface (GUI).

We will demonstrate four capabilities:

  • Reordering Libraries in Simulink Library Browser.
  • Disabling and Hiding Libraries in Simulink Library Browser.
  • Customizing Simulink Menus.
  • Disabling and Hiding Options in Configuration Paramaters Dialog.

To make the customizations we use different versions of the sl_customization.m file. In this demo, we use three different versions, each covering one of the capabilities in the above list.

To make each customization effective, we add the appropriate version of sl_customization.m to the MATLAB path and then update the Simulink user interface with the settings specified in that file.

Contents

Reordering Libraries in Simulink Library Browser

In this section we show how you can reorder libraries in your Simulink Library Browser. Our reordering will place the Simulink Extras library above the Simulink library. On opening Simulink library browser, you can see that the Simulink library is placed above relative to the Simulink Extras library.

simulink('open');

Now let us look at sl_customization.m file that we will use to make the changes to the order of libraries.

In this demo, the sl_custom_order.m file is copied into the current directory as sl_customization.m. Since the current directory is always on the MATLAB path, update of the Simulink GUI as specified by sl_customization.m will be applied automatically.

However, it is recommended that when customizing Simulink GUI for your project, you add the directory that contains the sl_customization.m file to the MATLAB path using the following command:

addpath(dir,'-end');

where dir is the directory that constains the sl_customization.m file

copyfile('sl_custom_order.m', 'sl_customization.m', 'f');
type sl_customization.m
function sl_customization(cm)
% This code changes the order of libraries in Simulink library browser. 
cm.LibraryBrowserCustomizer.applyOrder( {'simulink', -1,...
                                         'simulink_extras', -2} );
end

sl_refresh_customizations function applies the changes to the Simulink GUI settings. There is a change in the order of libraries in the Simulink library browser.

Note that the name of the libraries in the Simulink browser can be obtained by using the following command:

findblib

and then clicking on the library. The name of the libary can be obtained from the top left corner.

sl_refresh_customizations;

If the directory containing sl_customization.m to MATLAB path, you can revert to the original GUI settings by removing this directory from the path using the following command:

rmpath(pwd);

In this demo, we simply delete sl_customization.m from the current directory. The Simulink library browser reverts to the original GUI settings after the the sl_refresh_customizations function is executed.

delete('sl_customization.m');
sl_refresh_customizations;
simulink('close');

Disabling and Hiding Libraries in Simulink Library Browser

In this section we show how you can disable and/or hide libraries in the Simulink library browser. We hide the Continuous library and disable the Discrete library. Let us look at sl_customization.m file that brings about these changes.

simulink('open');
copyfile('sl_custom_filter.m', 'sl_customization.m', 'f');
type sl_customization.m
function sl_customization(cm)
% This code hides Continuos library and disables Discrete library. 
cm.LibraryBrowserCustomizer.applyFilter( { 'simulink/Continuous', 'Hidden', ...
                                          'simulink/Discrete', 'Disabled' } );
% This code, currently commented out show how you can hide  and disable
% libraries for specific products. For example, to hide Aerospace Blockset
% and to disable Communications Blockset library, the code below would have
% to be uncommented
%cm.LibraryBrowserCustomizer.applyFilter( { 'Aerospace Blockset', 'Hidden', ...
%                                          'Communications Blockset', 'Disabled' } );
end


Observe the changes in Simulink library browser - Continuous library is hidden and Discrete library is disabled.

sl_refresh_customizations;

Simulink reverts to the original GUI settings after the sl_customization.m is removed from the MATLAB path and the sl_refresh_customizations function is executed.

delete('sl_customization.m');
sl_refresh_customizations;
simulink('close');

Customizing Simulink Menus

In this section we show how you can customize Simulink menus. Let us look at sl_customization.m file that brings about these changes.

copyfile('sl_custom_menu.m', 'sl_customization.m','f');
type sl_customization.m
function sl_customization(cm)
%% Register custom menu function.
  cm.addCustomMenuFcn('Simulink:ToolsMenu', @getMyMenuItems);
end

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

 %% Define the schema function for the menu items.
 
 function schema = getItem1(callbackInfo)
  schema = sl_action_schema;
  schema.label = 'Item One';
  schema.userdata = 'one';	
  schema.callback = @myCallback1; 
 end
 
 function myCallback1(callbackInfo)
  disp(['Callback for item ' callbackInfo.userdata ' was called']);
 end

function schema = getItem2(callbackInfo)
  % Make a submenu label 'Item Two' with     
  % the menu item above three times.      
  schema = sl_container_schema;
  schema.label = 'Item Two';    
  schema.userdata = 'two';	
  schema.childrenFcns = {@getItem1, @getItem1, @getItem1};
end 

function schema = getItem3(callbackInfo)
  % Create a menu item whose label is
  % 'Item Three: 3', with the 3 being passed
  % from getMyItems above.
  schema = sl_action_schema;
  schema.label = 'Item Three';  
  schema.userdata = 'three';
  schema.callback = @myCallback3; 
end

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

function myToggleCallback(callbackInfo)
    if strcmp(get_param(gcs, 'ScreenColor'), 'red') == 0
        set_param(gcs, 'ScreenColor', 'red');
    else
        set_param(gcs, 'ScreenColor', 'white');
    end
end

%% Define the schema function for a toggle menu item.
function schema = getItem4(callbackInfo)
  schema = sl_toggle_schema;
  schema.label = 'Red Screen';
  if strcmp(get_param(gcs, 'ScreenColor'), 'red') == 1
    schema.checked = 'checked';
  else
    schema.checked = 'unchecked';
  end
  schema.callback = @myToggleCallback; 
end

Clicking on the Tools menu of F14 model(F-14 Longitudinal Flight Control demo) shows the changes. There are new items on this menu that were defined in sl_customization.m file. If Item One menu item is clicked, it will execute the callback function and print a message that identifies the callback for Item One. Since Item Two is made up of three Item One sub-menu items, the callback that gets executed when any of these are clicked is the exactly the same one that corresponds to Item One. Similar behavior is obtained with Item Three menu item.

open_system('f14');

% Close all the scopes.
blocks = find_system(bdroot,'BlockType','Scope');
for i = 1:length(blocks)
set_param(blocks{i},'Open','off')
end

sl_refresh_customizations;

If the "Red Screen" menu item is clicked, the background color screen toggles from white to red.

We can revert back to the original GUI settings by using the following commands:

delete('sl_customization.m');
sl_refresh_customizations;
bdclose(gcs)

Disabling and Hiding Options in Configuration Paramaters Dialog

In this section we show how you can customize the Configuration Parameters dialog. First, we open the F14 model and show the various parameters in the dialog. Initially, the Start Time option is visible. The Solver Type and Solver are enabled. A close look at the sl_customization.m file shows that Start Time should be hidden. Solver Type and Solver should be disabled.

open_system('f14');

% Close all the scopes.
blocks = find_system(bdroot,'BlockType','Scope');
for i = 1:length(blocks)
set_param(blocks{i},'Open','off')
end

myConfigObj = getActiveConfigSet(gcs);
openDialog(myConfigObj);
copyfile('sl_custom_disable_hide.m', 'sl_customization.m','f');
type sl_customization.m
function sl_customization(cm)

% Disable for standalone Configuration Parameters dialog box.
cm.addDlgPreOpenFcn('Simulink.ConfigSet',@disableSolverButton)

% Disable for Configuration Parameters dialog box that appears in
% the Model Explorer.
cm.addDlgPreOpenFcn('Simulink.SolverCC',@disableSolverButton)

end

function disableSolverButton(dialogH)

% Disable the SolverType dialog box
dialogH.disableWidgets({'Simulink.SolverCC.SolverType'})

% Disable the Solver dialog box
dialogH.disableWidgets({'Simulink.SolverCC.Solver'}) 

% Hide the Start Time dialog box
dialogH.hideWidgets({'Simulink.SolverCC.StartTime'})

end

To see the dialog and widget names in the Configuration Parameters dialog, use the following commands:

cm = sl_customization_manager;

cm.showWidgetIdAsToolTip = true;

The following command closes the Configuration Parameters dialog

closeDialog(myConfigObj);

The solver type is set to fixed-step discrete in the model's configuration set and the customizations are applied. The customization hides the Start Time dialog, sets the Solver Type to Fixed-step, sets Solver field to Discrete, and disables both options.

set_param(myConfigObj,'Solver','FixedStepDiscrete');
sl_refresh_customizations;
openDialog(myConfigObj);

We can revert back to the original Simulink GUI settings by using the following commands:

closeDialog(myConfigObj);
delete('sl_customization.m');
sl_refresh_customizations;
bdclose(gcs)

Contact us