Main Content

Disable and Hide Dialog Box Controls

About Disabling and Hiding Controls

Simulink® includes a customization API that allows you to disable and hide controls (also referred to as widgets), such as text fields and buttons, on most dialog boxes. The customization API allows you to disable or hide controls on an entire class of dialog boxes, for example, parameter dialog boxes, by way of a single method call.

Before you customize a Simulink dialog box or class of dialog boxes, first make sure that the dialog box or class of dialog boxes is customizable. Any dialog box that appears in the dialog pane of Model Explorer is customizable. In addition, any dialog box that has dialog and widget IDs is customizable. To determine whether a dialog box is customizable, open the dialog box, enable dialog and widget ID display (see Widget IDs), and hover over a widget. If a widget ID appears, you can customize the dialog box.

Once you have determined that a dialog box or class of dialog boxes is customizable, write MATLAB® code to customize the dialog boxes. This entails writing callback functions that disable or hide controls for a specific dialog box or class of dialog boxes (see Write Control Customization Callback Functions) and registering the callback functions using the customization manager (see Register Control Customization Callback Functions). Simulink invokes the callback functions to disable or hide the controls whenever you open the dialog boxes.

Disable a Button on a Dialog Box

This sl_customization.m file disables the Browse button on the Code Generation pane of the Configuration Parameters dialog box for any model whose name contains engine.

function sl_customization(cm)

% Disable for standalone Configuration Parameters dialog box 
configset.dialog.Customizer.addCustomization(@disableRTWBrowseButton,cm);

end

function disableRTWBrowseButton(dialogH)
  hSrc   = dialogH.getSource;  % Simulink.RTWCC
  hModel = hSrc.getModel;
  modelName   = get_param(hModel,'Name');

  if ~isempty(strfind(modelName,'engine'))
    % Takes a cell array of widget Factory ID.
    dialogH.disableWidgets({'STF_Browser'})
  end

end

To test this customization:

  1. Save the sl_customization.m file on the MATLAB path.

  2. Refresh the customizations by entering sl_refresh_customizations at the command line or by restarting MATLAB (see Register Customizations with Simulink).

  3. Open the sldemo_engine model in the Model Engine Timing Using Triggered Subsystems example.

  4. Open the Configuration Parameters dialog box and look at the Code Generation pane to see if the Browse button is disabled.

Write Control Customization Callback Functions

A callback function for disabling or hiding controls on a dialog box accepts one argument: a handle to the dialog box object that contains the controls you want to disable or hide. The dialog box object provides methods that the callback function can use to disable or hide the controls that the dialog box contains.

The dialog box object also provides access to objects containing information about the current model. Your callback function can use these objects to determine whether to disable or hide controls. For example, this callback function uses these objects to disable the Browse button on the Code Generation pane of the Configuration Parameters dialog box for any model whose name contains engine.

function disableRTWBrowseButton(dialogH)

  hSrc = dialogH.getSource;  % Simulink.RTWCC
  hModel = hSrc.getModel;
  modelName = get_param(hModel,'Name');

  if ~isempty(strfind(modelName,'engine'))
     % Takes a cell array of widget Factory ID.
     dialogH.disableWidgets({'STF_Browser'})
  end

Dialog Box Methods

Dialog box objects provide these methods for enabling, disabling, and hiding controls:

  • disableWidgets(widgetIDs)

  • hideWidgets(widgetIDs)

widgetIDs is a cell array of widget identifiers (see Widget IDs) that specify the widgets to disable or hide.

Widget IDs

Widget IDs identify a control on a Simulink dialog box. To determine the widget ID for a particular control, execute the following code at the command line:

cm = sl_customization_manager;
cm.showWidgetIdAsToolTip = true

Open the dialog box that contains the control and hover over the control to display a tooltip listing the widget ID. For example, hovering over the Start time field on the Solver pane of the Configuration Parameters dialog box shows that the widget ID for the Start time field is StartTime.

In the Configuration Parameters dialog box, the pointer is paused on the Start time text box. A tooltip lists the widget ID as StartTime and the tag as Tag_ConfigSet_Solver_StartTime.

Note

The tooltip displays not customizable for controls that are not customizable.

Register Control Customization Callback Functions

To register control customization callback functions for an installation of Simulink, include code in the installation sl_customization.m file (see Register Customizations with Simulink) that invokes the configset.dialog.Customizer.addCustomization method on the callbacks.

This method takes as an argument a pointer to the callback function to register. Invoking this method causes the registered function to be invoked before the dialog box is opened.

This example registers a callback that disables the Browse button on the Code Generation pane of the Configuration Parameters dialog box (see Write Control Customization Callback Functions).

function sl_customization(cm)  

% Disable for standalone Configuration Parameters dialog box 
configset.dialog.Customizer.addCustomization(@disableRTWBrowseButton,cm);

end

Note

Registering a customization callback causes Simulink to invoke the callback for every instance of the class of dialog boxes specified by the method dialog box ID argument. You can therefore use a single callback to disable or hide a control for an entire class of dialog boxes. In particular, you can use a single callback to disable or hide the control for a parameter that is common to most built-in blocks. Most built-in block dialog boxes are instances of the same dialog box super class.

Related Topics