Why do we need to use dynamic properties?(matlab2020a code)

41 views Asked by At

Why do we need to use dynamic properties? In what situations must we use dynamic properties? What value do they bring? If I must add a property, why not add it within the class instead of adding a dynamic property to the object?

The book I'm reading provides examples of dynamic properties, but I find that they seem to function like regular variables. Furthermore, it seems like this program can achieve its goals without dynamic properties. Please take a look at the code below.

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

The book instructs to execute the following commands.

hbutton1 = MyButton ([20 40 100 40]);   
addprop(hbutton1,'Caption');  
hbutton1.Caption='OK';
set(hbutton1.UiHandle,'String',hbutton1.Caption)

However, I found that removing dynamic properties does not affect the outcome.

hbutton1 = MyButton ([20 40 100 40]);
hbutton1.UiHandle.String='OK'
0

There are 0 answers