Main Content

Customize Display of Object Arrays


Customize the display of nonscalar objects, including empty object arrays.

Design of Custom Display


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

The objective of this customized display is to:

  • Construct a custom header using some elements of the default header

  • Display a subset of property-specific information for each object in the array.

  • List handles to deleted objects in the array using a char vector with links to documentation for handle objects and the class.

  • Display empty objects with a slight modification to the default header

Here is the customized display of an array of three EmployeeInfo objects

1x3 EmployeeInfo array with members:
1. Employee:
          Name: 'Bill Tork'
    Department: 'Product Development'

2. Employee:
          Name: 'Alice Blackwell'
    Department: 'QE'

3. Employee:
          Name: 'Nancy Green'
    Department: 'Documentation'

Deleted object handles in the array indicate their state:

1x3 EmployeeInfo members:

1. Employee:
          Name: 'Bill Tork'
    Department: 'Product Development'

2. handle to deleted EmployeeInfo

3. Employee:
          Name: 'Nancy Green'
    Department: 'Documentation'

To achieve the desired result, the EmployeeInfo class overrides the following methods of the matlab.mixin.CustomDisplay class:

The displayNonScalarObject Override

MATLAB® calls the displayNonScalarObject method to display object arrays. The override of this method in the EmployeeInfo class:

  • Builds header text using convertDimensionsToString to obtain the array size and getClassNameForHeader to get the class name with a link to the help for that class.

  • Displays the modified header text.

  • Loops through the elements in the array, building two different subheaders depending on the individual object state. In the loop, this method:

    • Detects handles to deleted objects (using the isvalid handle class method). Uses getDeletedHandleText and getClassNameForHeader to build text for array elements that are handles to deleted objects.

    • Builds a custom subheader for valid object elements in the array

  • Creates a PropertyGroup object containing the Name and Department properties for valid objects

  • Uses the displayPropertyGroups static method to generate the property display for valid objects.

Here is the implementation of displayNonScalarObjects:

methods (Access = protected)
   function displayNonScalarObject(objAry)
      dimStr = matlab.mixin.CustomDisplay.convertDimensionsToString(objAry);
      cName = matlab.mixin.CustomDisplay.getClassNameForHeader(objAry);
      headerStr = [dimStr,' ',cName,' members:'];
      header = sprintf('%s\n',headerStr);
      for ix = 1:length(objAry)
         o = objAry(ix);
         if ~isvalid(o)
            str1 = matlab.mixin.CustomDisplay.getDeletedHandleText;
            str2 = matlab.mixin.CustomDisplay.getClassNameForHeader(o);
            headerInv = [str1,' ',str2];
            tmpStr = [num2str(ix),'. ',headerInv];
            numStr = sprintf('%s\n',tmpStr);
            numStr = [num2str(ix),'. Employee:'];
            propList = struct('Name',o.Name,...
            propgrp = matlab.mixin.util.PropertyGroup(propList);

The displayEmptyObject Override

MATLAB calls the displayEmptyObject method to display empty object arrays. The implementation of this method in the EmployeeInfo class builds a custom header for empty objects following these steps:

  • Gets the array dimensions in character format using the convertDimensionsToString static method.

  • Gets text with the class name linked to the helpPopup function using the getClassNameForHeader static method.

  • Builds and displays the custom text for empty arrays.

methods (Access = protected)
   function displayEmptyObject(obj)
      dimstr = matlab.mixin.CustomDisplay.convertDimensionsToString(obj);
      className = matlab.mixin.CustomDisplay.getClassNameForHeader(obj);
      emptyHeader = [dimstr,' ',className,' with no employee information'];
      header = sprintf('%s\n',emptyHeader);

For example, an empty EmployeeInfo object displays like this:

Empt = EmployeeInfo.empty(0,5)

Empt = 

0x5 EmployeeInfo with no employee information

 Complete Class Listing

Related Topics