How to create a custom setter method in MATLAB 2013b?
Take the following class
classdef MyClass
properties (Access = public)
MyProperty;
end
methods
function this = MyClass()
% initialize the class
this.MyProperty = [];
this.LoadProperty(2,2);
end
function p = GetProperty(this)
p = this.MyProperty;
end
function this = LoadProperty(this, m, n)
% loads the property
this.MyProperty = zeros(m, n);
end
end
end
And then you call:
obj = MyClass();
obj.GetProperty()
It will return [] -- which is the first value assigned to MyProperty in
the constructor method.
The LoadProperty method is acting as a setter, but it does not set
anything. How can I create a setter for MyProperty? I am coming from C#
background and there it is very straightforward. -> SOLVED (see below)
I suspect that it is an issue with references and objects, as MATLAB
always send the object itself as the first parameter to every method of a
class, instead of sending only a reference as C# do.
Thank you in advance!
EDIT:
If I change the line this.LoadProperty(2,2); to this =
this.LoadProperty(2,2);, it works.
Now, is there a way to create a void-return method in MATLAB, which only
sets a class property, as it would be normally expected in C#, C++, Java,
etc?
No comments:
Post a Comment