Customize Property Display - MATLAB & Simulink (original) (raw)

Main Content

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

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

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:

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

Full 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 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 end

See Also

Topics