Can I 'initialize' a handle object in such a way that isvalid(object) would return false?

조회 수: 12 (최근 30일)
I'm trying to initialize a variable (MyHandle) that I want to be a handle pointing to some object but which can't currently be assigned to said object. I have code that needs to behave differently depending on if MyHandle is assigned or not yet and I haven't found a good way to test if MyHandle has been assigned to a valid object or not.
The function 'isvalid' seems like the right choice to test this, however, isvalid throws the error "Incorrect number or types of inputs or outputs for function isvalid." when run on MyHandle in its unassigned state. I've tried different ways to initialize it like "handle([])" and "[]", but they all return that same error.
I've found some bandaid solutions like initializing MyHandle to an instance of some arbitrary handle subclass and then deleting that initialized object, or assigning MyHandle to be a value of type double and then using "isnumeric" as my test function instead of "isvalid," but I was wondering if there is a more "correct" way to do it for the sake of best practices.
Below is a sample class that recreates the behavior described above. To restate my question in terms of the class below: Is there an initialization for MyHandle I can use in the properties block that will allow IsHandleAssigned to return false before the first time AssignHandle is run with a valid handle and return true afterwards? OR is there a different function besides isvalid that I should be using in IsHandleAssigned to achieve that behavior?
classdef ExampleClass < handle
properties
MyHandle %Will be a handle pointing to an object of an arbitrary class
% MyHandle = handle([]) < This initialization returns an error
% MyHandle = [] < This initialization returns an error
% MyHandle = 0 < This works if I replace 'isvalid' with 'isnumeric'
% but that seems like poor practice to do since it obfuscates the
% purpose of MyHandle
end
methods
function obj = AssignHandle(obj,handleIn)
%This method assigns the handle of whatever class it happens to
%be to my property.
obj.MyHandle = handleIn;
end
function HandleIsAssigned = IsHandleAssigned(obj)
%This method tests whether or not my handle was assigned. Is
%there a better function than isvalid to use?
HandleIsAssigned = isvalid(obj.MyHandle);
end
end
end

채택된 답변

Matt J
Matt J 2024년 8월 30일
편집: Matt J 2024년 8월 30일
One way:
classdef ExampleClass < handle
properties
MyHandle
end
methods
function obj = AssignHandle(obj,handleIn)
obj.MyHandle = handleIn;
end
function HandleIsAssigned = IsHandleAssigned(obj)
HandleIsAssigned = isa(obj.MyHandle,'handle') && isvalid(obj.MyHandle);
end
end
end
  댓글 수: 2
Aaron Coville
Aaron Coville 2024년 8월 30일
That's perfect! And the && has short circuit properties so that isvalid won't execute (and throw an error) if isa returns false... very clean solution. Thanks!
Anthony
Anthony 2025년 1월 8일
First, I would advocate for using set.MyHandle(obj, handleIn) instead of AssignHandle(obj, handleIn) in this context. This would enable you to use the obj.MyHandle = handleIn syntax over having to use obj.AssignHandle(handleIn). This is a matter of personal taste though.
Second, one issue I have with this solution is that the type of MyHandle is mutable. It would be preferable to restrict it to a handle class using MyHandle handle = handle('invalid') syntax, or something similar to enforce strong typing. Thus if the handle is uninitialized, we could use isvalid(obj.MyHandle) directly instead of having to add an ad hoc IsHandleAssigned() method for every handle property. Unfortunately, isvalid(handle(0)) == true and isvalid(handle([])) returns an error for missing toolboxes. Unfortunately, since handle is an abstract class, this syntax is invalid:
properties
MyHandle handle; % Error: Class handle is abstract.
end
My current workaround is to do something like this:
properties
MyHandle handle = handle(0); % isvalid(obj.MyHandle) == true, which is not desired.
end
% ...
function ExampleClass(obj) % or startupFcn in AppDesigner
% ...
delete(obj.MyHandle); % Force handle to be invalid.
end
This is clunky though, because it leads people to believe that handle(0) is invalid. if they haven't read the relevant code in the constructor.
Does anyone know of a better way to achieve this?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by