Class with Default Object Display
The EmployeeInfo Class
The EmployeeInfo class defines a number of
properties to store information about company employees. This simple
class serves as the example class used in display customization sample
classes.
EmployeeInfo derives from the matlab.mixin.CustomDisplay class
to enable customization of the object display.
EmployeeInfo is also a handle class. Therefore
instances of this class can be in the state referred to as a handle
to a deleted object. This state does not occur with value classes
(classes not derived from handle).
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 end
The matlab.mixin.CustomDisplay is handle compatible. Therefore, subclasses
can be either handle or value classes.
Default Display — Scalar
Here is the creation and display of a scalar EmployeeInfo object.
By default, MATLAB® displays properties and their values for scalar
objects.
Provide inputs for the constructor:
>>Emp123 = EmployeeInfo; Name: 'Bill Tork' Job Title: 'Software Engineer' Department: 'Product Development' Salary: 1000 Password: 'bill123'
Display the object:
>>Emp123
Emp123 =
EmployeeInfo with properties:
Name: 'Bill Tork'
JobTitle: 'Software Engineer'
Department: 'Product Development'
Salary: 1000
Password: 'bill123'Testing for Scalar Objects
To test for scalar objects, use isscalar.
Default Display — Nonscalar
The default display for an array of objects does not show property
values. For example, concatenating two EmployeeInfo objects
generates this display:
>>[Emp123,Emp124]
ans
1x2 EmployeeInfo array with properties:
Name
JobTitle
Department
Salary
PasswordTesting for Nonscalar Objects
To test for nonscalar objects, use a negated call to isscalar.
Default Display — Empty Object Array
An empty object array has at least one dimension equal to zero.
>> Empt = EmployeeInfo.empty(0,5)
Empt =
0x5 EmployeeInfo array with properties:
Name
JobTitle
Department
Salary
PasswordTesting for Empty Object Arrays
Use isempty to test for
empty object arrays. An empty object array is not scalar because its
dimensions can never be 1–by-1.
>> emt = EmployeeInfo.empty
emt =
0x0 EmployeeInfo array with properties:
Name
JobTitle
Department
Salary
Password
>> isscalar(emt)
ans =
0Default Display — Handle to Deleted Object
When a handle object is deleted, the handle variable can remain in the workspace.
>> delete(Emp123) >> Emp123 Emp123 = handle to deleted EmployeeInfo
Testing for Handles to Deleted Objects
To test for a handle to a deleted object, use isvalid.
Note
isvalid is a handle class method. Calling isvalid on
a value class object causes an error.
Default Display — Detailed Display
The details method does not support customization and always returns the standard detailed display:
details(Emp123)
EmployeeInfo handle with properties:
Name: 'Bill Tork'
JobTitle: 'Software Engineer'
Department: 'Product Development'
Salary: 1000
Password: 'bill123'
Methods, Events, Superclasses