MATLAB Object-Oriented Programming

(Joyce) #1
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 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 = [email protected](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

18 Customizing Object Display

Free download pdf