What is the difference between 'Property Get and Set Methods' and the 'set' function?

20 views (last 30 days)
'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
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.
fa wu
fa wu on 5 Sep 2023
Is there anything that syntactic sugar and "Property Get and Set Methods" cannot achieve, and must be implemented using set/get functions? If it's just a matter of saving a few lines of code, I feel that the necessity of set/get functions is not particularly significant.

Sign in to comment.

Answers (2)

Bruno Luong
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
fa wu
fa wu on 5 Sep 2023
It is work
h ={};
hold on
for k=1:4
h{k}= plot(rand(1,10));
h{k}.Color =rand(1,3); % this won't work
end
---------------------------------------------------------------------------------------------
Thank you for your response. I've learned something and also have some questions. When I execute
b = plot(rand(1,10));
, I get a ‘matlab.graphics.chart.primitive.Line’ object. But when I execute
h = [];
h(1) = plot(rand(1,10));
, I get a double constant. This causes an error when I try
h(1).Color = rand(1,3);
, of course, it would error out because you can't assign a value to a constant. However, why does this constant work in
set(h(k), 'Color', rand()*[1 1 1]);
?
This page tells me that h should be "the specified graphics object or a vector of objects." But your code does seem to work, which is quite mysterious. Would you like to explain why it works?
Bruno Luong
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.

Sign in to comment.


Steven Lord
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
ans = 1×4
0.2500 0.2500 0.5000 0.5000
try
f.units = 'normalized'
catch ME
fprintf('Call threw error:\n%s\n', ME.message)
end
Call threw error: Unrecognized property 'units' for class 'matlab.ui.Figure'.
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;
Warning: Error creating or updating Line
Error in value of one or more of the following properties: XData YData
Array is wrong shape or size
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
dt = datetime
05-Sep-2023 14:58:38
I can change its Year property using dot notation.
dt.Year = 9999
dt = datetime
05-Sep-9999 14:58:38
But I can't change its property using set because set is not defined for this class.
set(dt, 'Year', 2023)
Error using set
Cannot find 'set' method for datetime class.
  1 Comment
fa wu
fa wu on 5 Sep 2023
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
.------------In general, your view is that set/get provides some convenience, but they are not strictly necessary. Dot notation can handle most situations. Is that correct?

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!