Customize Display of Object Arrays - MATLAB & Simulink (original) (raw)

Main Content

Objective

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

Design of Custom Display

The objective of this customized display is to:

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:

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); disp(header) 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); disp(numStr) else numStr = [num2str(ix),'. Employee:']; disp(numStr) propList = struct('Name',o.Name,... 'Department',o.Department); propgrp = matlab.mixin.util.PropertyGroup(propList); matlab.mixin.CustomDisplay.displayPropertyGroups(o,propgrp); end end end end

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:

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); disp(header) end end

For example, an empty EmployeeInfo object displays like this:

Empt = EmployeeInfo.empty(0,5)

Empt =

0x5 EmployeeInfo with no employee information

Complete Class Listing

classdef EmployeeInfo < handle & matlab.mixin.CustomDisplay properties Name JobTitle Department Salary Password end methods function obj = EmployeeInfo obj.Name = input('Name: '); obj.JobTitle = input('Job Title: '); obj.Department = input('Department: '); obj.Salary = input('Salary: '); obj.Password = input('Password: '); end end 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); disp(header) 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); disp(numStr) else numStr = [num2str(ix),'. Employee']; disp(numStr) propList = struct('Name',o.Name,... 'Department',o.Department); propgrp = matlab.mixin.util.PropertyGroup(propList); matlab.mixin.CustomDisplay.displayPropertyGroups(o,propgrp); end end end

  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);
     disp(header)
  end

end end

See Also

Topics