Customize Header, Property List, and Footer - MATLAB & Simulink (original) (raw)

Main Content

Objective

Customize each of the three parts of the display — header, property groups, and footer.

Design of Custom Display

For the header:

For properties:

For the footer:

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: 1000 Password: 'bill123'

Company Private

Here is the custom display of an array of EmployeeInfo objects:

[Emp123,Emp124]

ans =

1x2 EmployeeInfo array with properties:

Department
Name
JobTitle

Here is the display of an empty object array:

EmployeeInfo.empty(0,5)

ans =

0x5 EmployeeInfo array with properties:

Department
Name
JobTitle

Here is the display of a handle to a deleted object (EmployeeInfo is a handle class):

delete(Emp123) Emp123

Emp123 =

handle to deleted EmployeeInfo

Implementation

The EmployeeInfo class overrides three matlab.mixin.CustomDisplay methods to implement the display shown:

Each method must produce the desired results with each of the following inputs:

getHeader Method Override

MATLAB® calls getHeader to get the header text. The EmployeeInfo class overrides this method to implement the custom header for scalar display. Here is how it works:

Here is the EmployeeInfo override of the getHeader method. The required protected access is inherited from the superclass.

methods (Access = protected) function header = getHeader(obj) if ~isscalar(obj) header = getHeader@matlab.mixin.CustomDisplay(obj); else className = matlab.mixin.CustomDisplay.getClassNameForHeader(obj); newHeader = [className,' Dept: ',obj.Department]; header = sprintf('%s\n',newHeader); end end end

getPropertyGroups Override

MATLAB calls getPropertyGroups to get the PropertyGroup objects, which control how properties are displayed. This method override defines two different property lists depending on the object’s state:

Both branches return a matlab.mixin.util.PropertyGroup object, which determines how to displays the object properties.

Here is the EmployeeInfo override of the getPropertyGroups method. The protected access is inherited from the superclass.

methods (Access = protected) function propgrp = getPropertyGroups(obj) if ~isscalar(obj) propList = {'Department','Name','JobTitle'}; propgrp = matlab.mixin.util.PropertyGroup(propList); else gTitle1 = 'Public Info'; gTitle2 = 'Personal Info'; propList1 = {'Name','JobTitle'}; propList2 = {'Salary','Password'}; propgrp(1) = matlab.mixin.util.PropertyGroup(propList1,gTitle1); propgrp(2) = matlab.mixin.util.PropertyGroup(propList2,gTitle2); end end end

getFooter Override

MATLAB calls getFooter to get the footer text. The EmployeeInfo getFooter method defines a footer for the display, which is included only when the input is a valid scalar object. In all other cases, getFooter returns an empty char vector.

Scalar handles to deleted objects do not result in a call to getFooter.

methods (Access = protected) function footer = getFooter(obj) if isscalar(obj) footer = sprintf('%s\n','Company Private'); else footer = ''; end end end

Complete Class Listing

classdef EmployeeInfo < handle & matlab.mixin.CustomDisplay properties Name JobTitle Department Salary Password end methods function obj = EmployeeInfo 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 header = getHeader(obj) if ~isscalar(obj) header = getHeader@matlab.mixin.CustomDisplay(obj); else className = matlab.mixin.CustomDisplay.getClassNameForHeader(obj); newHeader = [className,' Dept: ',obj.Department]; header = sprintf('%s\n',newHeader); end end

  function propgrp = getPropertyGroups(obj)
     if ~isscalar(obj)
        propList = {'Department','Name','JobTitle'};
        propgrp = matlab.mixin.util.PropertyGroup(propList);
     else
        gTitle1 = 'Public Info';
        gTitle2 = 'Personal Info';
        propList1 = {'Name','JobTitle'};
        propList2 = {'Salary','Password'};
        propgrp(1) = matlab.mixin.util.PropertyGroup(propList1,gTitle1);
        propgrp(2) = matlab.mixin.util.PropertyGroup(propList2,gTitle2);
     end
  end
  
  function footer = getFooter(obj)
     if isscalar(obj)
        footer = sprintf('%s\n','Company Private');
     else
        footer = '';
     end
  end

end end

See Also

Topics