Class property empty after initialization
Show older comments
OS: Windows 10
Hi every one!
I'm implementing matlab classes and observed some strange behaviour I don't have an explanation for and cannot find an answer.
Here is short example of my code:
classdef A < handle % Some superclass a with an abstract property and an already initialized one
properties(Abstract)
prop1
end
properties(Constant)
prop2 = someValue;
end
methods
obj = function A(prop2)
obj.prop2 = prop2;
end
end
end
classdef C % Class C provides an interface which has to be implemented by specific subclasses of class A
methods
obj = function C(prop1)
obj.prop1 = prop1;
end
end
end
classdef B < A
properties
prop1 = @C; % initialize prop1 with interface of class C
prop3
end
methods
obj = function B(prop2)
obj@A(prop2); % pass prop2 arg to superconstructor of class A
obj.createC(); % create an instance of class C
end
function createC(obj)
obj.prop3 = obj.prop1(obj.prop2);
end
end
end
When I create an instance of class B, objectB.prop3 is empty, where it should contain an instance of class C.
I checked the creation of the class C object in the debugger. Function createC properly creates the object and it is stored in B.prop3.
So, only when creation of the class B object is done, prop3 is empty.
Thanks in advance,
cheers
1 Comment
Sebastian Pietsch
on 13 Dec 2021
Edited: Sebastian Pietsch
on 13 Dec 2021
Answers (1)
Adarsh
on 27 Mar 2025
0 votes
After running the code, I've realized that some adjustments are necessary. I've observed that the property ("prop3") is being updated correctly and is not empty, which is the desired behaviour. Since all the classes inherit from the "handle class," there's no need to update the object again, as any changes will automatically be reflected in the properties.
Here are a few changes that I have made to the code:
- “prop2 = someValue;” => removed “someValue” as it is not defined
- “obj.prop2 = prop2;” => removed this statement from “A” ’s constructor as prop2 is declared constant so it has to be assigned a value directly and cannot be modified or reassigned.
- “obj = function C(prop1)” => This line has a syntax error, so modified it accordingly to => “function obj = C(prop1)”
- “obj = function A(prop2)” => This line has a syntax error, so modified it accordingly to => “function obj = A(prop2)”
Here is a working example with the modified code:
Class A Implementation:
classdef A < handle
properties(Abstract)
prop1
end
properties
prop2 = 1;
end
methods
function obj = A(prop2)
obj.prop2 = prop2;
end
end
end
Class B Implementation:
classdef B < A
properties
prop1 = @C;
prop3
end
methods
function obj = B(prop2)
obj@A(prop2);
obj.createC();
end
function createC(obj)
obj.prop3 = obj.prop1(obj.prop2);
end
end
end
Class C Implementation:
classdef C
properties
prop1
end
methods
function obj = C(prop1)
obj.prop1 = prop1;
end
end
end
Here is the code which I have used for testing the working of handle class:
Obj = B(12);
Obj
For more information on handle classes you can refer to the following documentation link:
I hope it helps.
Categories
Find more on Handle Classes 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!