Customize the display of scalar objects.
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 Department
property
Group properties into two categories titled Public Info
and Personal Info
.
Modify which properties are displayed
Modify the values displayed for Personal Info
category
Use 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: '*******'
The EmployeeInfo
class overrides two matlab.mixin.CustomDisplay
methods to implement the display shown:
displayScalarObject
— Called to display valid scalar objects
getPropertyGroups
— Builds the property groups for display
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 getClassNameForHeader
static method to return linked class name text and the value of the Department
property to get the department name.
Uses sprintf
to add a new line to the header text
Displays the header with the built-in disp
function.
Calls the getPropertyGroups
override to define the property groups (see following section).
Displays the property groups using the displayPropertyGroups
static 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
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 struct
array of property names with associated property values for the second group. Using a struct
instead of a cell array enables you to replace the values that are displayed for the Salary
and Password
properties without changing the personal information stored in the object properties.
Constructs two matlab.mixin.util.PropertyGroup
objects, which are used by the displayScalarObject
method.
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