Customize Display of Object Arrays - MATLAB & Simulink (original) (raw)
Main Content
Objective
Customize the display of nonscalar objects, including empty object arrays.
Design of Custom Display
The objective of this customized display is to:
- Construct a custom header using some elements of the default header
- Display a subset of property-specific information for each object in the array.
- List handles to deleted objects in the array using a
char
vector with links to documentation for handle objects and the class. - Display empty objects with a slight modification to the default header
Here is the customized display of an array of three EmployeeInfo
objects
1x3 EmployeeInfo array with members:
Employee: Name: 'Bill Tork' Department: 'Product Development'
Employee: Name: 'Alice Blackwell' Department: 'QE'
Employee: Name: 'Nancy Green' Department: 'Documentation'
Deleted object handles in the array indicate their state:
1x3 EmployeeInfo members:
Employee: Name: 'Bill Tork' Department: 'Product Development'
handle to deleted EmployeeInfo
Employee: Name: 'Nancy Green' Department: 'Documentation'
To achieve the desired result, the EmployeeInfo
class overrides the following methods of the matlab.mixin.CustomDisplay
class:
- displayNonScalarObject — Called to display nonempty object arrays
- displayEmptyObject — Called to display empty object arrays
The displayNonScalarObject Override
MATLAB® calls the displayNonScalarObject
method to display object arrays. The override of this method in the EmployeeInfo
class:
- Builds header text using convertDimensionsToString to obtain the array size and getClassNameForHeader to get the class name with a link to the help for that class.
- Displays the modified header text.
- Loops through the elements in the array, building two different subheaders depending on the individual object state. In the loop, this method:
- Detects handles to deleted objects (using the isvalid handle class method). Uses getDeletedHandleText and getClassNameForHeader to build text for array elements that are handles to deleted objects.
- Builds a custom subheader for valid object elements in the array
- Creates a PropertyGroup object containing the
Name
andDepartment
properties for validobjects
- Uses the displayPropertyGroups static method to generate the property display for valid objects.
Here is the implementation of displayNonScalarObjects
:
methods (Access = protected) function displayNonScalarObject(objAry) dimStr = matlab.mixin.CustomDisplay.convertDimensionsToString(objAry); cName = matlab.mixin.CustomDisplay.getClassNameForHeader(objAry); headerStr = [dimStr,' ',cName,' members:']; header = sprintf('%s\n',headerStr); disp(header) for ix = 1:length(objAry) o = objAry(ix); if ~isvalid(o) str1 = matlab.mixin.CustomDisplay.getDeletedHandleText; str2 = matlab.mixin.CustomDisplay.getClassNameForHeader(o); headerInv = [str1,' ',str2]; tmpStr = [num2str(ix),'. ',headerInv]; numStr = sprintf('%s\n',tmpStr); disp(numStr) else numStr = [num2str(ix),'. Employee:']; disp(numStr) propList = struct('Name',o.Name,... 'Department',o.Department); propgrp = matlab.mixin.util.PropertyGroup(propList); matlab.mixin.CustomDisplay.displayPropertyGroups(o,propgrp); end end end end
The displayEmptyObject Override
MATLAB calls the displayEmptyObject
method to display empty object arrays. The implementation of this method in the EmployeeInfo
class builds a custom header for empty objects following these steps:
- Gets the array dimensions in character format using the
convertDimensionsToString
static method. - Gets text with the class name linked to the
helpPopup
function using thegetClassNameForHeader
static method. - Builds and displays the custom text for empty arrays.
methods (Access = protected) function displayEmptyObject(obj) dimstr = matlab.mixin.CustomDisplay.convertDimensionsToString(obj); className = matlab.mixin.CustomDisplay.getClassNameForHeader(obj); emptyHeader = [dimstr,' ',className,' with no employee information']; header = sprintf('%s\n',emptyHeader); disp(header) end end
For example, an empty EmployeeInfo
object displays like this:
Empt = EmployeeInfo.empty(0,5)
Empt =
0x5 EmployeeInfo with no employee information
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 displayNonScalarObject(objAry) dimStr = matlab.mixin.CustomDisplay.convertDimensionsToString(objAry); cName = matlab.mixin.CustomDisplay.getClassNameForHeader(objAry); headerStr = [dimStr,' ',cName,' members:']; header = sprintf('%s\n',headerStr); disp(header) for ix = 1:length(objAry) o = objAry(ix); if ~isvalid(o) str1 = matlab.mixin.CustomDisplay.getDeletedHandleText; str2 = matlab.mixin.CustomDisplay.getClassNameForHeader(o); headerInv = [str1,' ',str2]; tmpStr = [num2str(ix),'. ',headerInv]; numStr = sprintf('%s\n',tmpStr); disp(numStr) else numStr = [num2str(ix),'. Employee']; disp(numStr) propList = struct('Name',o.Name,... 'Department',o.Department); propgrp = matlab.mixin.util.PropertyGroup(propList); matlab.mixin.CustomDisplay.displayPropertyGroups(o,propgrp); end end end
function displayEmptyObject(obj)
dimstr = matlab.mixin.CustomDisplay.convertDimensionsToString(obj);
className = matlab.mixin.CustomDisplay.getClassNameForHeader(obj);
emptyHeader = [dimstr,' ',className,' with no employee information'];
header = sprintf('%s\n',emptyHeader);
disp(header)
end
end end