Why do we need to use dynamic properties?

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'

9 Comments

I have never needed dynamic properties for anything I have been involved with, so this is something I too am unclear on.
Some (many? few?) Matlab and toolbox classes have a property called UserData that allows the user to add some specific information to each instance of a class, which can then be used to implement additional functionality.
f = figure;h = tf(1,[1 1]);
f.UserData
ans = []
h.UserData
ans = []
It seems like dynamic properties extend this a bit so that the user can add several properties to class instances for customization that were not considered by the class designer and use those properties to implement additional functionality with those class instances.
True, I have added UserData to graphics objects from time to time...
Adam
Adam on 6 Sep 2023
Edited: Adam on 6 Sep 2023
It probably depends on style of programming. I'be been OOP programming in Matlab for over 10 years and have never once used dynamic properties. I've considered them once or twice, but they just seem like very bad programming style to me, or certainly don't suit my programming philosophy, so I don't use them.
I want to be able to look at a class definition and see what the class has to offer me, it's the main reason I use classes rather than structs for most things. So I don't want things dynamically popping up on an instance of a class that I may have to search through previous lines of code to understand what they are and why they are there.
For graphical handle class there are also setappdata/getappdata functionality where user can supply a name. Il is more friendly (or deaty dependig how yoy see it) than UserData property.
I often create GUIs where objects (uicontrols, axes, etc) get switched between various states (particularly visibility, view, string, etc) depending on other menu/data selections). Keeping track of this using separate arrays would certainly be possible, but in practice I have often found it easier to store some meta-data in the USERDATA and make choices based on that. It saves storing and passing that meta-data in a separate array.
Another usage is to store e.g. the loop index of graphics objects created in a loop, e.g. for a matrix of buttons. This has the neat benefit that the index is automatically available with every callback, etc. without needing to pass it explicitly in a separate array or using that cell array syntax for extra callback arguments. Quite handy, but of course (like just about everything else in programming) not irreplaceable.
@Stephen23 I feel that you must have appreciated the advantages of dynamic properties. I'm looking forward to you providing a formal answer with example code.
@Stephen23 Does this example fit the situation you mentioned? -------“I often create GUIs where objects (uicontrols, axes, etc) get switched between various states (particularly visibility, view, string, etc) depending on other menu/data selections).”

Sign in to comment.

Answers (1)

They are a convenience, not a requirement. From the documentation:
Use dynamic properties to attach temporary data to objects or to assign data that you want to associate with an instance of a class, but not all objects of that class.

4 Comments

Why can't we use a regular variable to store temporary data to objects? For example, in the previous example, 'OK' can be assigned to a regular variable.
The purpose of my asking this question is: not only to learn how to use the tool but also to understand when to use which tool. I believe that MATLAB must have provided dynamic properties for a specific reason, and it's definitely not intended for users to use it as a variable for storing temporary data. If I am unaware of the special value of dynamic properties, I won't be able to fully utilize their true potential!
Matt J
Matt J on 6 Sep 2023
Edited: Matt J on 6 Sep 2023
Why can't we use a regular variable to store temporary data to objects?
Regular class properties, you mean? Because regular class properties are not temporary, and also all instances of the class must have that property. They will linger permanently as part of the class, and may be seen as code clutter if that variable only gets used in a small set of circumstances.
I believe that MATLAB must have provided dynamic properties for a specific reason, and it's definitely not intended for users to use it as a variable for storing temporary data.
But the documentation I quoted to you says that that is the reason! Why doubt it when Mathworks is telling you that's the reason in their own words?
Why doubt it--------I don't doubt, I don't undstand.
"Use dynamic properties to attach temporary data to objects or to assign data that you want to associate with an instance of a class, but not all objects of that class."--------It tell me How,don't tell me Why.
Well, like I said, it's just so that you can attach data to an object without cluttering the classdef with additional properties that you're only going to use for one application. I don't think there's any more to it than that.

Sign in to comment.

Categories

Products

Release

R2020a

Asked:

on 5 Sep 2023

Commented:

on 7 Sep 2023

Community Treasure Hunt

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

Start Hunting!