What is the difference between 'Property Get and Set Methods' and the 'set' function?
7 views (last 30 days)
Show older comments
'Property Get and Set Methods' are abbreviated as 'Pset,' while the'set/get' function is abbreviated as 'set/get' functions
"In my book, there is a dedicated section that introduces 'set/get' functions. It explicitly states:
1. 'set/get' functions are methods of the abstract class 'hgsetget.' To use 'set/get,' you must first make your defined class a subclass of 'hgsetget.'
2. 'set/get' functions are distinct from 'Property Get and Set Methods.'Using 'set/get' functions, we can set and query the properties of graphical objects. I have not found content in the MATLAB documentation that is identical to what is presented in the book. The content on this page is the closest match to the content in the book."
Here is the content from the book.
"Differing from the set and get method calls on property members, the set/get interface methods of the 'hgsetget' class can be both accessed and queried using the object with the '.' operator referencing property members or called directly in the form of regular functions, such as:
v = get(h, 'PropertyName');
or
set(h, 'PropertyName', PropertyValue);"
"Here, 'v' returns the value of the member 'PropertyName' within the handle object 'h.' Meanwhile,
set(h, 'PropertyName', PropertyValue);
or
h.PropertyName = PropertyValue;
is used to set the value of the member 'PropertyName' within the handle object 'h' to 'PropertyValue.'
If you have redefined the set/get interface methods for a property member in a derived class of 'hgsetget,' it will override the base class 'hgsetget' set/get interface methods. However, you can still directly call 'set' and 'get' in the form of regular functions when using them."
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The content above has left me quite confused, and it has raised at least two questions for me:
1、"If 'set(h, 'PropertyName', PropertyValue);' and 'h.PropertyName = PropertyValue;' are equivalent, then we don't really need the 'set/get' function at all because all object properties can be assigned values using 'h.PropertyName = PropertyValue;' without the requirement that your defined class must be a subclass of 'hgsetget.'"
2、You might argue that the relationship between 'set(h, 'PropertyName', PropertyValue);' and 'h.PropertyName = PropertyValue;' is similar to that between '+' and the 'plus' function. With the 'plus' function, you can overload 'plus' in your class. However, I would like to point out that if you want to overload the process of 'h.PropertyName = PropertyValue;', you can achieve this entirely using 'Property Get and Set Methods'in your class. Moreover, it doesn't require your class to be a subclass of 'hgsetget.' It offers more flexibility.
In summary, do set/get functions serve a redundant purpose?
2 Comments
Stephen23
on 5 Sep 2023
Edited: Stephen23
on 5 Sep 2023
"In summary, do set/get functions serve a redundant purpose?"
No, it is rather the other way around: accessing the property directly is just syntactic sugar that only offers a subset of what SET/GET can do. SET/GET work on all version of MATLAB (e.g. graphics prior to R2014b) and also on arrays of (e.g. graphics) objects.
In short: you can write more code with SET/GET than you can using object properties.
Answers (2)
Bruno Luong
on 5 Sep 2023
Edited: Bruno Luong
on 5 Sep 2023
"In summary, do set/get functions serve a redundant purpose"
No since functional form is prefered and even required if you use feval or such.
I believe way back the graphic handles are exclusively double and not class, only set/get can work
h = [];
hold on
for k=1:2
h(k) = plot(rand(1,10));
% h(k).color = rand()*[1 1 1]) % this won't work
set(h(k), 'color', rand()*[1 1 1]); % this works however
end
Also set can work on multiple objects
h = [];
hold on
for k=1:2
h(k) = plot(rand(1,10));
end
set(h, 'color', [1 0 0]);
4 Comments
Bruno Luong
on 5 Sep 2023
I already told you already : before 2014 graphic HG1 handles are double. Only since HG2 that the handle are encapsulated to a true handle class.
But double handle are still working to ensure backward compatibility.
Steven Lord
on 5 Sep 2023
"If 'set(h, 'PropertyName', PropertyValue);' and 'h.PropertyName = PropertyValue;' are equivalent, then we don't really need the 'set/get' function at all because all object properties can be assigned values using 'h.PropertyName = PropertyValue;' without the requirement that your defined class must be a subclass of 'hgsetget.'"
The error is in your first clause. Those two syntaxes are not equivalent, and set and get existed in MATLAB long before the introduction of classdef-based classes that allowed using dot notation to set and query properties.
If I recall correctly, originally set and get were implemented to support specifying properties of Handle Graphics objects using case insensitive and partial names for convenience. For example, a figure has properties Units and Position. If you use set you can change those properties using names 'units' and 'pos' rather than the full Units and Position. Dot notation does not allow the abbreviation or the case insensitivity.
f = figure;
set(f, 'units', 'normalized', 'pos', [0.25 0.25 0.5 0.5])
f.Position
try
f.units = 'normalized'
catch ME
fprintf('Call threw error:\n%s\n', ME.message)
end
Those functions also support setting multiple properties of Handle Graphics objects simultaneously, which could help avoid warnings.
h = plot(1:10, 1:10);
h.XData = 1:11;
drawnow % force MATLAB to redraw and warn
h.YData = 1:11;
On MATLAB Answers the line plot appears at the end of the set call, but if you ran those lines one at a time in desktop MATLAB you'd see the line disappears when the warning is issued.
Compare with:
h = plot(1:10, 1:10);
set(h, 'XData', 1:11, 'YData', 1:11) % No redraw in the middle of the set call, no warning
So why include the dot notation at all if set and get provide additional flexibility? I believe one of the main considerations is efficiency. Having to check which property 'pos' refers to for a figure does have some overhead, while I believe using dot notation to set the Position property has less overhead. It also avoids needing to define set and get methods (or property access methods named set.<property name> and get.<property name>) for your class if the default property setting and querying infrastructure does what you want.
You might argue that the relationship between 'set(h, 'PropertyName', PropertyValue);' and 'h.PropertyName = PropertyValue;' is similar to that between '+' and the 'plus' function. With the 'plus' function, you can overload 'plus' in your class. However, I would like to point out that if you want to overload the process of 'h.PropertyName = PropertyValue;', you can achieve this entirely using 'Property Get and Set Methods'in your class. Moreover, it doesn't require your class to be a subclass of 'hgsetget.' It offers more flexibility.
No, I wouldn't make that argument. The dot notation for setting properties is not simply an operator form of the set function. You can use dot notation for objects that don't define a set method. For example, consider the datetime class.
dt = datetime
I can change its Year property using dot notation.
dt.Year = 9999
But I can't change its property using set because set is not defined for this class.
set(dt, 'Year', 2023)
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!