Customize Property Display
Objective
Change the order and number of properties displayed for an object of your class.
Change the Property Order
Suppose your class definition contains the following property definition:
properties Name JobTitle Department Salary Password end
In the default scalar object display, MATLAB® displays all the public properties along with their values. However, you want to display only Department, JobTitle, and Name, in that order. You can do this by deriving from CustomDisplay and overriding the getPropertyGroups method.
Your override
Defines method
Accessasprotectedto match the definition in theCustomDisplaysuperclassCreates a cell array of property names in the desired order
Returns a
PropertyGroupobject constructed from the property list cell array
methods (Access = protected) function propgrp = getPropertyGroups(~) proplist = {'Department','JobTitle','Name'}; propgrp = matlab.mixin.util.PropertyGroup(proplist); end end
When you create a PropertyGroup object using a cell array of property names, MATLAB automatically
Adds the property values for a scalar object display
Uses the property names without values for a nonscalar object display (including empty object arrays)
The getPropertyGroups method is not called to create the display for a scalar handle to a deleted object.
Change the Values Displayed for Properties
Given the same class properties used in the previous section, you can change the value displayed for properties by building the property list as a struct and specifying values for property names. This override of the getPropertyGroups method uses the default property display for nonscalar objects by calling the superclass getPropertyGroups method. For scalar objects, the override:
Changes the value displayed for the
Passwordproperty to a'*'character for each character in the password.Displays the text
'Not Available'for theSalaryproperty.
methods (Access = protected) function propgrp = getPropertyGroups(obj) if ~isscalar(obj) propgrp = getPropertyGroups@matlab.mixin.CustomDisplay(obj); else pd(1:length(obj.Password)) = '*'; propList = struct('Department',obj.Department,... 'JobTitle',obj.JobTitle,... 'Name',obj.Name,... 'Salary','Not available',... 'Password',pd); propgrp = matlab.mixin.util.PropertyGroup(propList); end end end
The object display looks like this:
EmployeeInfo with properties:
Department: 'Product Development'
JobTitle: 'Software Engineer'
Name: 'Bill Tork'
Salary: 'Not available'
Password: '*******'