Customize Display of Scalar Objects - MATLAB & Simulink (original) (raw)

Main Content

Objective

Customize the display of scalar objects.

Design of Custom Display

The objective of this customized display is to:

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 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:

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:

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

Complete Class Listing

classdef EmployeeInfo4 < handle & matlab.mixin.CustomDisplay properties Name JobTitle Department Salary Password end methods function obj = EmployeeInfo4 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 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

  function propgrp = getPropertyGroups(obj)
     if ~isscalar(obj)
        propgrp = getPropertyGroups@matlab.mixin.CustomDisplay(obj);
     else
        % property groups for scalars
        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 end

See Also

Topics