Class property empty after initialization

조회 수: 9 (최근 30일)
Sebastian Pietsch
Sebastian Pietsch 2021년 12월 12일
답변: Adarsh 2025년 3월 27일
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
Sebastian Pietsch
Sebastian Pietsch 2021년 12월 13일
편집: Sebastian Pietsch 2021년 12월 13일
I found the cause of the problem.
obj.prop3 is a cell array and changing values in a cell array within a method call seems to not change the object variable after the call is done.
My workaround now is
function prop3 = createC(obj)
prop3 = obj.prop1(obj.prop2);
end
and then set the object parameter in the construcor
obj.prop3 = obj.createC(obj);
.
Has anybody an idea why this is and how to make the changes apear on object level?

댓글을 달려면 로그인하십시오.

답변 (1개)

Adarsh
Adarsh 2025년 3월 27일
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:
  1. prop2 = someValue; => removed someValue as it is not defined
  2. 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.
  3. obj = function C(prop1) => This line has a syntax error, so modified it accordingly to => “function obj = C(prop1)”
  4. 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.

카테고리

Help CenterFile Exchange에서 Handle Classes에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by