MATLAB Object-Oriented Programming

(Joyce) #1

delete(P);


Assign Data to the Dynamic Property


Suppose, you are using a predefined set of user interface widget classes (buttons, sliders,
check boxes, etc.). You want to store the location of each instance of the widget class.
Assume that the widget classes are not designed to store location data for your particular
layout scheme. You want to avoid creating a map or hash table to maintain this
information separately.


Assuming the button class is a subclass of dynamicprops, add a dynamic property to
store your layout data. Here is a simple class to create a uicontrol button:


classdef button < dynamicprops
properties
UiHandle
end
methods
function obj = button(pos)
if nargin > 0
if length(pos) == 4
obj.UiHandle = uicontrol('Position',pos,...
'Style','pushbutton');
else
error('Improper position')
end
end
end
end
end


Create an instance of the button class, add a dynamic property, and set its value:


b1 = button([20 40 80 20]);
b1.addprop('myCoord');
b1.myCoord = [2,3];


Access the dynamic property just like any other property, but only on the object on which
you defined it:


b1.myCoord


ans =


2 3


Dynamic Properties — Adding Properties to an Instance
Free download pdf