Main Content

matlab.lang.HandlePlaceholder Class

Namespace: matlab.lang

Basic subclass of handle

Since R2024b

Description

This class is a subclass of handle, and it defines no members other than what it inherits from handle. You can use matlab.lang.HandlePlaceholder when you need a basic handle class to test code. You can also use an object of this class as a default value when defining a property with the WeakHandle attribute.

The matlab.lang.HandlePlaceholder class is a handle class.

Creation

The no-argument constructor of matlab.lang.WeakReference returns an instance with its Handle property set to a handle to a deleted matlab.lang.WeakReference object. You can also call the matlab.lang.HandlePlaceholder class constructor directly.

Examples

collapse all

Create a 2-by-3 array of weak reference placeholders by calling createArray with matlab.lang.WeakReference as the class.

weakArray = createArray(2,3,"matlab.lang.WeakReference")
weakArray = 

  2×3 WeakReference array with properties:

    Handle

Verify that the Handle property of the array elements is set to a handle to a deleted matlab.lang.HandlePlaceholder object.

weakArray(1,1).Handle
ans = 

  handle to deleted HandlePlaceholder

When defining a property using the WeakHandle attribute, you must use class validation. You can use handle as a way of allowing all handle class values, but because handle is an abstract class, you must provide a default value as part of the validation. Define a class with a WeakHandle property named Property1, and validate the property value with the handle class and an empty array of matlab.lang.HandlePlaceholder as the initial value.

classdef NonspecificWeak
    properties (WeakHandle)
        Property1 handle = matlab.lang.HandlePlaceholder.empty
    end
end

Construct an instance of the class.

x = NonspecificWeak
x = 

  NonspecificWeak with properties:

    Property1: [0×0 matlab.lang.HandlePlaceholder]

If you also need to use size validation, rewrite the class to use the matlab.lang.invalidHandle function to create an array of handles to deleted matlab.lang.HandlePlaceholder objects instead.

classdef SizedNonspecificWeak
    properties (WeakHandle)
        Property1 (2,2) handle = matlab.lang.invalidHandle("matlab.lang.HandlePlaceholder",2,2)
    end
end

Construct an instance of the class. Access an element of Property1 to verify that the value is a handle to a deleted matlab.lang.HandlePlaceholder object.

y = SizedNonspecificWeak;
y.Property1(1,1)
 
ans = 

  handle to deleted HandlePlaceholder

Version History

Introduced in R2024b