Customize Display of Scalar Objects
Objective
Customize the display of scalar objects.
Design of Custom Display
Note
This example uses the EmployeeInfo class described in the Class with Default Object Display section.
The objective of this customized display is to:
Modify the header to include the department name obtained from the
DepartmentpropertyGroup properties into two categories titled
Public InfoandPersonal Info.Modify which properties are displayed
Modify the values displayed for
Personal InfocategoryUse the default displayed for nonscalar objects, including empty arrays, and scalar deleted handles
For example, 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: 'Level: 10'
Password: '*******'Implementation
The EmployeeInfo class overrides two matlab.mixin.CustomDisplay methods to implement the display shown:
displayScalarObject— Called to display valid scalar objectsgetPropertyGroups— Builds the property groups for display
displayScalarObject Method Override
MATLAB® calls displayScalarObject to display scalar objects. The EmployeeInfo class overrides this method to implement the scalar display. Once overridden, this method must control all aspects of scalar object display, including creating the header, property groups, and footer, if used.
This implementation:
Builds a custom header using the
getClassNameForHeaderstatic method to return linked class name text and the value of theDepartmentproperty to get the department name.Uses
sprintfto add a new line to the header textDisplays the header with the built-in
dispfunction.Calls the
getPropertyGroupsoverride to define the property groups (see following section).Displays the property groups using the
displayPropertyGroupsstatic method.
Here is the EmployeeInfo override of the displayScalarObject method. The required protected access is inherited from the superclass.
methods (Access = protected) function displayScalarObject(obj) className = matlab.mixin.CustomDisplay.getClassNameForHeader(obj); scalarHeader = [className,' Dept: ',obj.Department]; header = sprintf('%s\n',scalarHeader); disp(header) propgroup = getPropertyGroups(obj); matlab.mixin.CustomDisplay.displayPropertyGroups(obj,propgroup) end end
getPropertyGroups Override
MATLAB calls getPropertyGroups when displaying scalar or nonscalar objects. However, MATLAB does not call this method when displaying a scalar handle to a deleted object.
The EmployeeInfo class overrides this method to implement the property groups for scalar object display.
This implementation calls the superclass getPropertyGroups method if the input is not scalar. If the input is scalar, this method:
Defines two titles for the two groups
Creates a cell array of property names that are included in the first group. MATLAB adds the property values for the display
Creates a
structarray of property names with associated property values for the second group. Using astructinstead of a cell array enables you to replace the values that are displayed for theSalaryandPasswordproperties without changing the personal information stored in the object properties.Constructs two
matlab.mixin.util.PropertyGroupobjects, which are used by thedisplayScalarObjectmethod.
Here is the EmployeeInfo override of the getPropertyGroups method. The required protected access is inherited from the superclass.
methods (Access = protected) function propgrp = getPropertyGroups(obj) if ~isscalar(obj) propgrp = getPropertyGroups@matlab.mixin.CustomDisplay(obj); else gTitle1 = 'Public Info'; gTitle2 = 'Personal Info'; propList1 = {'Name','JobTitle'}; pd(1:length(obj.Password)) = '*'; level = round(obj.Salary/100); propList2 = struct('Salary',... ['Level: ',num2str(level)],... 'Password',pd); propgrp(1) = matlab.mixin.util.PropertyGroup(propList1,gTitle1); propgrp(2) = matlab.mixin.util.PropertyGroup(propList2,gTitle2); end end end
