Say I have a class like such:
classdef exampleClass
properties (Dependent=true)
x
end
methods
function this=exampleClass(this)
this.x = 4;
end
function x=get.x(this)
x=4;
end
end
end
What's the difference in accessing x
as classInstance.x
and classInstance.x()
?
The function get.x(this)
is called a getter of the property x. It actually has nothing to do whether the property has the attribute Dependent or not, it is the same for any type of property.
If you have a setter/getter defined for your property, Matlab will always call the function get.PropertyName or set.PropertyName when you do something like:
tmp_var = my_instance.x
or
my_instance.x = 3.1416;
So if you have in your code my_instance.x
or my_instance.x()
is practically the same. But if you want to follow best practices, you must avoid the function call.
Now, as an extra point: for performance reasons, it is recommended that you do not use setters/getters because every time you modify your property (even inside your class) you will pay the price of the overhead of the setter/getter.