Constructor Not Assigning Values in Matlab - matlab

I am trying to create a few objects in Matlab. I am used to programming in Java, but being able to do this in Matlab would be beneficial for my research.
So, I have a particular class:
classdef Lifter < handle
properties
weightClass = 0;
mean = 0;
var = 0;
nation = '';
end
methods
function Lifter = Lifter(weightClass,mean,var,nation) %Constructor
Lifter.weightClass = weightClass;
Lifter.mean = mean;
Lifter.var = var;
Lifter.nation = nation;
end
function set.weightClass(this,weightClass)
end
function set.mean(this,mean)
end
function set.var(this,var)
end
function set.nation(this,nation)
end
function value = get.weightClass(this)
value = this.weightClass;
end
function value = get.mean(this)
value = this.mean;
end
function var = get.var(this)
var = this.mean;
end
function nation = get.nation(this)
nation = this.nation;
end
end
end
Pretty standard, doesn't really do much.
So, in another page, I have:
function Competition()
Lifter1 = Lifter(56,1,1,'USA')
end
However, running this gives:
Lifter1 =
Lifter with properties:
weightClass: 0
mean: 0
var: 0
nation: ''
Any help to figure out why this values are not being initialized correctly from the constructor would be very helpful.
Also, an example of how to actually call a setter or getter method on my Lifter1 object would be helpful. Thank you!

Those setters are for dependent properties (declared as properties(Dependent)). But also, your setters are not implemented (empty), which means that all your property assignments won't do anything. Which means that the properties stay with the default values declared in the properties section.
Later Edit
If I would have to implement the class, I'd do it like in the following code:
classdef Lifter < handle
% --- PUBLIC SECTION ---
properties(Dependent)
weight_class;
end;
methods
function obj = Lifter(weight_class)
obj.weight_class = weight_class;
end;
function set.weight_class(obj, weight_class)
if ~isa(obj, 'Lifter') ...
|| ~isscalar(obj) ...
|| ~isvalid(obj)
error('Lifter:Arg', '"obj" must be a scalar valid handle object of class "Lifter".');
elseif ~isnumeric(weight_class) ...
|| ~isscalar(weight_class) ...
|| ~isreal(weight_class) ...
|| (weight_class <= 0) % + other value tests
error('Lifter:Arg', '"weight_class" must be a numeric positive scalar.');
else
obj.weight_class_ = double(weight_class);
end;
end;
function val = get.weight_class(obj)
val = obj.weight_class_;
end;
end;
% --- PRIVATE SECTION ---
properties(Access=private)
weight_class_;
end;
end
The code for the rest of the properties is similar. Test with:
a = Lifter(100); % Constructor + setter call
a.weight_class = 75; % Setter call
disp(a.weight_class); % Getter call
a.weight_class = '85'; % Setter call with wrong value

Your setters and getters should not contain dots (although I agree it would be more elegant your way if it worked):
function set.weightClass(this,weightClass)
end
Could be (should be) :
function set_weightClass(this,weightClass)
end
If I replace all of them, it works properly.

Related

Trouble modifying property in superclass from subclass in matlab

I'm having a weird problem using object oriented matlab, and I'm not sure if I'm completely missing something, or I've hit some unusual behaviour.
I have a superclass defined as follows:
classdef (Abstract) AbstractSimulationData < matlab.mixin.Heterogeneous
properties (Abstract)
name
data
end
properties
timeStamp = -1
end
methods (Abstract)
CalculateData(obj, t)
end
methods
function data = GetData(obj, t)
if obj.timeStamp == t.t
data = obj.data;
else
obj.timeStamp = t.t;
obj.CalculateData(t);
data = obj.data;
end
end
end
end
When the concrete class implements the method CalculateData I intend it to set the superclass property data.
Two concrete classes from this are:
classdef A < AbstractSimulationData
properties
name = 'A'
data = []
end
methods
function obj = A
% No special initialisation
end
function CalculateData(obj, t)
test = t.B.GetData(t)
obj.data = rand(2,10);
end
end
end
And:
classdef B < AbstractSimulationData
properties
name = 'B'
data = containers.Map
end
methods
function obj = B
% No special initialisation
end
function CalculateData(obj, t)
if isempty(obj.data)
obj.data('left') = 1;
obj.data('right') = 2;
end
% Other operations
end
end
end
Within the A.CalculateData method I call the GetData method for B. This in turn calls the B.CalculateData method.
Now, as it filters back up, the data property gets set correctly so that B.GetData returns the value that was set in B.CalculateData.
But, when I go the next step up to A.GetData it returns [], the initialised value. Going through debug mode, the calculations definitely work in A.CalculateData and it seems to set data correctly, but when it jumps back up to the superclass obj.data is the default value from A.
A minimum working example to get this behaviour:
t.B = B();
t.t = 1;
a = A;
a.GetData(t);
>> test =
Map with properties:
Count: 2
KeyType: char
ValueType: any
>> ans =
[]
This is all part of a pretty big project, so it's hard to give the full context, but if it's possible from this, can someone explain why setting superclass data works for B but not A?
If it makes a difference B is returning a container.Map and A is returning 2xn matrix.
As far as I can tell from the documentation, it should work.

Dynamically change property attributes

There are dependent properties in class A, based on an argument in constructor I want to make some of properties Hidden, so that user will not be able to set/get these properties.
classdef A
properties (Dependent = true)
prop1
prop2
end
methods
function value = get.prop1(obj)
...
end
function value = get.prop2(obj)
...
end
end
methods(Access = public)
function obj = A(arg1)
if arg1 == 1
% make prop1 Hidden for the constructed object
end
end
end
end
and here is sample usages:
a1 = A(2);
a1.prop1; % ok
a2 = A(1);
a2.prop1; % problem, user will not know about existence prop1
The Access level is fixed, as in any OOP language I know. It is fundamental to how the class interacts with other code.
Your only workaround is to use Dependent properties of a matlab.mixin.SetGet type class, and have a conditional behaviour based on the construction argument. Here is a POC class to demonstrate:
Class:
classdef POC < matlab.mixin.SetGet
properties ( Dependent = true )
prop
end
properties ( Access = private )
arg % Construction argument to dictate obj.prop behaviour
prop_ % Private stored value of prop
end
methods
function obj = POC( arg )
% constructor
obj.prop = 'some value'; % Could skip setting this if argCheck fails
obj.arg = arg;
end
% Setter and getter for "prop" property do obj.argCheck() first.
% This throws an error if the user isn't permitted to set/get obj.prop
function p = get.prop( obj )
obj.argCheck();
p = obj.prop_;
end
function set.prop( obj, p )
obj.argCheck();
obj.prop_ = p;
end
end
methods ( Access = private )
function argCheck( obj )
% This function errors if the property isn't accessible
if obj.arg == 1
error( 'Property "prop" not accessible when POC.arg == 1' );
end
end
end
end
Output
>> A = POC(1);
l>> A.prop
Error using POC/get.prop (line 17)
Property "prop" not accessible when POC.arg == 1
>> A = POC(2);
>> A.prop
ans =
'some value'
Edit: I'd create two different classes, one with hidden and one with visible attributes.
In that case you can just set the corresponding property attributes in the hidden class. This should do it:
properties (Dependent = true, Hidden = True, GetAccess=private, SetAccess=private)

Matlab - Constructor doesn't initliaize member values

Dear lovely community,
I already looked in Google and in the forum and also found some interesting posts. But in the end I still didn't get it worked. So, I am going to post the question here. I know in Matlab are already get/ Set methods implemented, but I am using objects and therefore I didn't understand where to implement them.
Overall Structure:
+Measurement\MeasurerIF
+Measurement\MeasurerComponent
In my Interface I declare my functions I want to implement and this Interface is furthermore abstract. It looks like this:
classdef MeasuererIF < handle
methods (Abstract=true)
getStatus(cObj) ;
setStatus(cObj,iStatus) ;
getInfo(cObj) ;
setInfo(cObj,sInfo) ;
end
end
Then I created the other class MeasurerComponent in which I implemented the methods and also the constructor:
classdef MeasurerComponent < PerformanceMeasurement.MeasuererIF
%% MeasurerComponent: Evaluates which model is used and contains them as childs.
% Detailed explanation goes here
properties (Access=protected)
miStatus;
msInfo;
mcData;
end
methods
%constructor
function cObj = PerformanceMeasurement.MeasurerComponent ;
cObj.miStatus = -1 ;
cObj.msInfo = 'Import' ;
cObj.mcData = [] ;
end
%Status
function setStatus(cObj,iStatus)
cObj.miStatus = iStatus;
end
function iStatus = getStatus(cObj)
iStatus = cObj.miStatus;
end
%Info
function setInfo(cObj,sInfo)
cObj.msInfo = sInfo;
end
function sInfo = getInfo(cObj)
sInfo = cObj.msInfo ;
end
end
end
Earlier I just used the getMethods and recently added the setMethods and now it doesn't work anymore. The Problem is that when I create an object
obj = Measurement.MeasurerComponent
the programm doesn't initialize the values anymore.
For a = obj.getInfo;
I receive just []
Does someone has an idea why it doesn't get initialized anymore? After I received a tip I changed the initialization process to the constructor due to the handle class.
I am really thankful and glad about each hint and tip!
Cheers
Your constructor should be defined by function cObj = MeasurerComponent, without the PerformanceMeasurement prefix. This is just the way that packages are defined and used in Matlab - you add the prefix if using the class from outside the package, but not within the package (explained here: "Note that definitions do not use the package prefix" -
http://uk.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html?refresh=true).
Also, you have a typo - your abstract parent class file is called +Measurement\MeasurerIF but in the definition you spell it MeasuererIF. It doesn't matter which one it is called, but the name of the class does have to match the filename!
If I correct both of these issues, then your code becomes
+Measurement\MeasurerIF
classdef MeasurerIF < handle
methods (Abstract=true)
getStatus(cObj)
setStatus(cObj,iStatus)
getInfo(cObj)
setInfo(cObj,sInfo)
end
end
+Measurement\MeasurerIF
classdef MeasurerComponent < Measurement.MeasurerIF
%% MeasurerComponent: Evaluates which model is used and contains them
%% as childs.
properties (Access=protected)
miStatus
msInfo
mcData
end
methods
%constructor
function cObj = MeasurerComponent
cObj.miStatus = -1 ;
cObj.msInfo = 'Import' ;
cObj.mcData = [] ;
end
%Status
function setStatus(cObj,iStatus)
cObj.miStatus = iStatus;
end
function iStatus = getStatus(cObj)
iStatus = cObj.miStatus;
end
%Info
function setInfo(cObj,sInfo)
cObj.msInfo = sInfo;
end
function sInfo = getInfo(cObj)
sInfo = cObj.msInfo ;
end
end
end
and if I type the following:
obj = Measurement.MeasurerComponent;
obj.getInfo
then I get back
ans =
Import
which is what I expect.

Dynamically assign the getter for a dependent property in MATLAB

In Matlab, I can define a class as such:
classdef klass < handle
properties(Dependent)
prop
end
end
Matlab is perfectly happy instantiating an object of this class, even without defining a getter for prop. It only fails when I try to access it (understandably). I'd like to set the GetMethod dynamically based upon the property's name.
Unfortunately, even when the property is Dependent, the meta.property field for GetMethod is still read-only. And while inheriting from dynamicprops could allow adding a property and programmatically setting its GetMethod in every instance, I don't believe it could be used to change an existing property. I may have to go this route, but as prop must exist for every object I'd prefer to simply set the getter on a class-by-class basis. Is such a thing possible?
An alternative solution could be through some sort of catch-all method. In other languages, this could be accomplished through a Ruby-like method_missing or a PHP-like __get(). But as far as I know there's no (documented or otherwise) analog in Matlab.
(My use case: this class gets inherited by many user-defined subclasses, and all their dependent properties are accessed in a similar way, only changing based on the property name. Instead of asking users to write get.* methods wrapping a call to the common code for each and every one of their dependent properties, I'd like to set them all dynamically with anonymous function pointers containing the necessary metadata).
Here is my proposal: create a method in the superclass called add_dyn_prop. This method is to be called in the subclasses instead of creating a dependent property the usual way.
The idea is that the superclass inherit from dynamicprops and use addprop to add a new property, and set its accessor methods manually based on its name.
classdef klass < dynamicprops
methods (Access = protected)
function add_dyn_prop(obj, prop, init_val, isReadOnly)
% input arguments
narginchk(2,4);
if nargin < 3, init_val = []; end
if nargin < 4, isReadOnly = true; end
% create dynamic property
p = addprop(obj, prop);
% set initial value if present
obj.(prop) = init_val;
% define property accessor methods
% NOTE: this has to be a simple function_handle (#fun), not
% an anonymous function (#()..) to avoid infinite recursion
p.GetMethod = #get_method;
p.SetMethod = #set_method;
% nested getter/setter functions with closure
function set_method(obj, val)
if isReadOnly
ME = MException('MATLAB:class:SetProhibited', sprintf(...
'You cannot set the read-only property ''%s'' of %s', ...
prop, class(obj)));
throwAsCaller(ME);
end
obj.(prop) = val;
end
function val = get_method(obj)
val = obj.(prop);
end
end
end
end
now in the subclass, instead of defining a dependent property the usual way, we use this new inherited function in the constructor to define a dynamic property:
classdef subklass < klass
%properties (Dependent, SetAccess = private)
% name
%end
%methods
% function val = get.name(obj)
% val = 'Amro';
% end
%end
methods
function obj = subklass()
% call superclass constructor
obj = obj#klass();
% define new properties
add_dyn_prop(obj, 'name', 'Amro');
add_dyn_prop(obj, 'age', [], false)
end
end
end
The output:
>> o = subklass
o =
subklass with properties:
age: []
name: 'Amro'
>> o.age = 10
o =
subklass with properties:
age: 10
name: 'Amro'
>> o.name = 'xxx'
You cannot set the read-only property 'name' of subklass.
Of course now you can customize the getter method based on the property name as you initially intended.
EDIT:
Based on the comments, please find below a slight variation of the same technique discussed above.
The idea is to require the subclass to create a property (defined as abstract in the superclass) containing the names of the desired dynamic properties to be created. The constructor of the superclass would then create the specified dynamic properties, setting their accessor methods to generic functions (which could customize their behavior based on the property name as you requested). I am reusing the same add_dyn_prop function I mentioned before.
In the subclass, we are simply required to implement the inherited abstract dynamic_props property, initialized with a list of names (or {} if you dont want to create any dynamic property). For example we write:
classdef subklass < klass
properties (Access = protected)
dynamic_props = {'name', 'age'}
end
methods
function obj = subklass()
obj = obj#klass();
end
end
end
The superclass is similar to what we had before before, only now is it its responsibility to call the add_dyn_prop in its constructor for each of the property names:
classdef klass < dynamicprops % ConstructOnLoad
properties (Abstract, Access = protected)
dynamic_props
end
methods
function obj = klass()
assert(iscellstr(obj.dynamic_props), ...
'"dynamic_props" must be a cell array of strings.');
for i=1:numel(obj.dynamic_props)
obj.add_dyn_prop(obj.dynamic_props{i}, [], false);
end
end
end
methods (Access = private)
function add_dyn_prop(obj, prop, init_val, isReadOnly)
% input arguments
narginchk(2,4);
if nargin < 3, init_val = []; end
if nargin < 4, isReadOnly = true; end
% create dynamic property
p = addprop(obj, prop);
%p.Transient = true;
% set initial value if present
obj.(prop) = init_val;
% define property accessor methods
p.GetMethod = #get_method;
p.SetMethod = #set_method;
% nested getter/setter functions with closure
function set_method(obj,val)
if isReadOnly
ME = MException('MATLAB:class:SetProhibited', sprintf(...
'You cannot set the read-only property ''%s'' of %s', ...
prop, class(obj)));
throwAsCaller(ME);
end
obj.(prop) = val;
end
function val = get_method(obj)
val = obj.(prop);
end
end
end
end
Note: I did not use ConstructOnLoad class attribute or Transient property attribute, as I am still not sure how they would affect loading the object from a saved MAT-file in regards to dynamic properties.
>> o = subklass
o =
subklass with properties:
age: []
name: []
>> o.name = 'Amro'; o.age = 99
o =
subklass with properties:
age: 99
name: 'Amro'
Check if this is what you want. The problem is that the user will need to get the properties using (), which may be quite boring, but anyway, I think this way you can change the variables. You can't change them directly on the class, but you can change the objects property values on demand. It doesn't need to change the values on the constructor, you can do that using another function that will be inherited by the classes.
klass1.m
classdef(InferiorClasses = {?klass2}) klass < handle
methods
function self = klass
selfMeta = metaclass(self);
names = {selfMeta.PropertyList.Name};
for name = names
switch name{1}
case 'prop_child_1'
self.(name{1}) = #newGetChild1PropFcn;
case 'prop_child_2'
self.(name{1}) = #newGetChild2PropFcn;
end
end
end
end
methods(Static)
function out = prop
out = #defaultGetPropFcn;
end
end
end
function out = defaultGetPropFcn
out = 'defaultGetPropFcn';
end
function out = newGetChild1PropFcn
out = 'newGetChild1PropFcn';
end
function out = newGetChild2PropFcn
out = 'newGetChild2PropFcn';
end
klass2.m
classdef klass2 < klass
properties
prop_child_1 = #defaultGetChildPropFcn1
prop_child_2 = #defaultGetChildPropFcn2
end
methods
function self = klass2
self = self#klass;
end
end
end
function out = defaultGetChildPropFcn1
out = 'defaultGetChildPropFcn1';
end
function out = defaultGetChildPropFcn2
out = 'defaultGetChildPropFcn2';
end
Output:
a = klass2
b=a.prop_child_1()
b =
newGetChild1PropFcn

Change Matlab methods attributes

I am trying to create a Matlab Class, where the methods attributes are changed in the Class Constructor. The purpose of this is to hide / make visible some methods, depending on the class input.
For example:
classdef (HandleCompatible) myClass < dynamicprops & handle % & hgsetget
properties (Hidden)
myProp
end
methods (Hidden)
function obj = myClass(input)
%class constructor
%add some dynamic properties
switch input
case 1
%unknown code:
%make myMethod1 visible
case 2
%unknown code:
%make myMethod2 visible
otherwise
%unknown code:
%make myMethod1 visible
%make myMethod2 visible
end
end
end
methods (Hidden)
function myMethod1 (obj, input)
%function...
end
function output = myMethod2(obj, input)
%function...
end
end
end
I tried to use the following:
mco = metaclass(obj);
mlist = mco.MethodList;
mlist(myMethod1Index).Hidden = false;
, but I get the following error:
Setting the 'Hidden' property of the 'meta.method' class is not allowed.
Thank you for your reply.
This could be a solution, if I need to access my methods selectively in the class constructor. Though, I need to use these methods in my program, and to have them visible or not, at tab completion:
%Obj1
myObj1 = myClass (inputs, '-1');
myObj1.myMethod1(arg);
%myObj1.myMethod2 - hidden
%Obj2
myObj2 = myClass (inputs, '1');
%myObj2.myMethod1 - hidden
value1 = myObj2.myMethod2(arg);
%Obj3
myObj3 = myClass (inputs, '0');
myObj3.myMethod1(arg);
value2 = myObj3.myMethod2(arg);
%here i want to be able to access both methods
Maybe it is possible to select the method properties, during class constructor, and change the attributes. But this has to be done without using the metaclass
Why not expose only a factory method and build instances of different classes depending on the input? You can use access qualifiers to lock things down like so:
% a.m
classdef a
properties, a_thing, end
methods ( Access = ?factory )
function obj = a()
end
end
end
% b.m
classdef b
properties, b_thing, end
methods ( Access = ?factory )
function obj = b()
end
end
end
% factory.m
classdef factory
methods ( Static )
function val = build(arg)
if isequal(arg, 'a')
val = a;
else
val = b;
end
end
end
end