Main Content

Choose a Technique for Display Customization

Ways to Implement a Custom Display

The way you customize object display using the matlab.mixin.CustomDisplay class depends on:

  • What parts of the display you want to customize

  • What object states you want to use the custom display

If you are making small changes to the default layout, then override the relevant part builder methods (Part Builder Methods). For example, suppose you want to:

  • Change the order or value of properties, display a subset of properties, or create property groups

  • Modify the header text

  • Add a footer

If you are defining a nonstandard display for a particular object state (scalar, for example), then the best approach is to override the appropriate state handler method (State Handler Methods).

In some cases, a combination of method overrides might be the best approach. For example, your implementation of displayScalarObject might

  • Use some of the utility methods (Utility Methods) to build your own display strings using parts from the default display

  • Call a part builder method to get the default text for that particular part of the display

  • Implement a completely different display for scalar objects.

Once you override any CustomDisplay method, MATLAB® calls your override in all cases where the superclass method would have been called. For example, if you override the getHeader method, your override must handle all cases where a state handler method calls getHeader. (See Methods Called for a Given Object State)

Sample Approaches Using the Interface

Here are some simple cases that show what methods to use for the particular customized display.

Change the Display of Scalar Objects

Use a nonstandard layout for scalar object display that is fully defined in the displayScalarObject method:

classdef MyClass < matlab.mixin.CustomDisplay
   ...
   methods (Access = protected)
      function displayScalarObject(obj)
         % Implement the custom display for scalar obj
      end
   end
end

Custom Property List with Standard Layout

Use standard display layout, but create a custom property list for scalar and nonscalar display:

classdef MyClass < matlab.mixin.CustomDisplay
   ...
   methods(Access = protected)
      function groups = getPropertyGroups(obj)
         % Return PropertyGroup instances
      end
   end
end

Custom Property List for Scalar Only

Use standard display layout, but create a custom property list for scalar only. Call the superclass getPropertyGroups for the nonscalar case.

classdef MyClass < matlab.mixin.CustomDisplay
   properties
      Prop1
      Prop2
      Prop3
   end
   methods(Access = protected)
      function groups = getPropertyGroups(obj)
         if isscalar(obj)
            % Scalar case: change order 
            propList = {'Prop2','Prop1','Prop3'};
            groups = matlab.mixin.util.PropertyGroup(propList)
         else
            % Nonscalar case: call superclass method
            groups = getPropertyGroups@matlab.mixin.CustomDisplay(obj);
         end
      end
   end
end

Custom Property List with Modified Values

Change the values displayed for some properties in the scalar case by creating property/value pairs in a struct. This getPropertyGroups method displays only Prop1 and Prop2, and displays the value of Prop2 as Prop1 divided by Prop3.

classdef MyClass < matlab.mixin.CustomDisplay
   properties
      Prop1
      Prop2
      Prop3
   end
   methods(Access = protected)
      function groups = getPropertyGroups(obj)
         if isscalar(obj)
            % Specify the values to be displayed for properties
            propList = struct('Prop1',obj.Prop1,...
               'Prop2',obj.Prop1/obj.Prop3);
            groups = matlab.mixin.util.PropertyGroup(propList)
         else
            % Nonscalar case: call superclass method
            groups = getPropertyGroups@matlab.mixin.CustomDisplay(obj);
         end
      end
   end
end

Complete Class Definitions

For complete class implementations, see these sections: