MATLAB: Does the SET function behave incorrectly when used with objects of a user defined class

classdefinedinvokeMATLABobjectsetuser

I am developing a GUI in which I want to use objects of my user-defined class. I want to store an object of myclass(user defined class) in a 'UserData' structure of a GUI panel.
I create an object called "myobj" of class "myclass".
When I execute the following commands :
h = uipanel;
set(h,'UserData',myobj);
MATLAB uses the SET function defined in the class "myclass" instead of invoking the MATLAB SET function on the uipanel handle (h).

Best Answer

There are two ways to work around this issue.
1. Wrap the user defined class object within a MATLAB structure.
h = uipanel;
myclass myobj;
a.obj=myobj;
set(h,'UserData',a);
2. Define the SET function of the user defined class object myobj such that it checks for the first argument to be myobj and if not, dispatches to the builtin set function. The modification needed to the SET function of the user defined class is shown below :
function varargout = set(obj, varargin)
if ~isa(obj, 'myobj')
builtin('set', obj, varargin{:});
end
end