Main Content

Customize Header, Property List, and Footer

Objective

Customize each of the three parts of the display — header, property groups, and footer.

Design of Custom Display

Note

This example uses the EmployeeInfo class described in the Class with Default Object Display section.

For the header:

  • Use default header for nonscalar object arrays.

  • Build header text with linked class name and department name (from Department property)

For properties:

  • Nonscalar object arrays display a subset of property names in a different order than the default.

  • Scalar objects create two property groups that have titles (Public Info and Personal Info).

For the footer:

  • Add a footer to the display, only when the object is a valid scalar that displays property values.

Here is the customized display of an object of the EmployeeInfo class.

Emp123 = 

EmployeeInfo Dept: Product Development

   Public Info
        Name: 'Bill Tork'
    JobTitle: 'Software Engineer'

   Personal Info
      Salary: 1000
    Password: 'bill123'

Company Private

Here is the custom display of an array of EmployeeInfo objects:

[Emp123,Emp124]

ans = 

  1x2 EmployeeInfo array with properties:

    Department
    Name
    JobTitle

Here is the display of an empty object array:

>> EmployeeInfo.empty(0,5)

ans = 

  0x5 EmployeeInfo array with properties:

    Department
    Name
    JobTitle

Here is the display of a handle to a deleted object (EmployeeInfo is a handle class):

>> delete(Emp123)
>> Emp123

Emp123 = 

  handle to deleted EmployeeInfo

Implementation

The EmployeeInfo class overrides three matlab.mixin.CustomDisplay methods to implement the display shown:

Each method must produce the desired results with each of the following inputs:

  • Scalar object

  • Nonscalar object array

  • Empty object array

getHeader Method Override

MATLAB® calls getHeader to get the header text. The EmployeeInfo class overrides this method to implement the custom header for scalar display. Here is how it works:

  • Nonscalar (including empty object) arrays call the superclass getHeader, which returns the default header.

  • Scalar handles to deleted objects do not result in a call to getHeader.

  • Scalar inputs build a custom header using the getClassNameForHeader static method to return linked class name text, and the value of the Department property.

Here is the EmployeeInfo override of the getHeader method. The required protected access is inherited from the superclass.

methods (Access = protected)
   function header = getHeader(obj)
      if ~isscalar(obj)
         header = getHeader@matlab.mixin.CustomDisplay(obj);
      else
         className = matlab.mixin.CustomDisplay.getClassNameForHeader(obj);
         newHeader = [className,' Dept: ',obj.Department];
         header = sprintf('%s\n',newHeader);
      end
   end
end

getPropertyGroups Override

MATLAB calls getPropertyGroups to get the PropertyGroup objects, which control how properties are displayed. This method override defines two different property lists depending on the object’s state:

  • For nonscalar inputs, including empty arrays and arrays containing handles to deleted objects, create a property list as a cell array to reorder properties.

    By default, MATLAB does not display property values for nonscalar inputs.

  • For scalar inputs, create two property groups with titles. The scalar code branch lists properties in a different order than the nonscalar case and includes Salary and Password properties. MATLAB automatically assigns property values.

  • Scalar handles to deleted objects do not result in a call to getPropertyGroups.

Both branches return a matlab.mixin.util.PropertyGroup object, which determines how to displays the object properties.

Here is the EmployeeInfo override of the getPropertyGroups method. The protected access is inherited from the superclass.

methods (Access = protected)
   function propgrp = getPropertyGroups(obj)
      if ~isscalar(obj)
         propList = {'Department','Name','JobTitle'};
         propgrp = matlab.mixin.util.PropertyGroup(propList);
      else
         gTitle1 = 'Public Info';
         gTitle2 = 'Personal Info';
         propList1 = {'Name','JobTitle'};
         propList2 = {'Salary','Password'};
         propgrp(1) = matlab.mixin.util.PropertyGroup(propList1,gTitle1);
         propgrp(2) = matlab.mixin.util.PropertyGroup(propList2,gTitle2);
      end
   end
end

getFooter Override

MATLAB calls getFooter to get the footer text. The EmployeeInfo getFooter method defines a footer for the display, which is included only when the input is a valid scalar object. In all other cases, getFooter returns an empty char vector.

Scalar handles to deleted objects do not result in a call to getFooter.

methods (Access = protected)
   function footer = getFooter(obj)
      if isscalar(obj)
         footer = sprintf('%s\n','Company Private');
      else
         footer = '';
      end
   end
end

 Complete Class Listing

Related Topics