Why do I see inconsistent behavior accessing class methods and properties through the "." operator in MATLAB 7.6 (R2008a)?

1 view (last 30 days)
I am trying to implement "SINGLETON" object oriented design pattern in MATLAB. I have a class "dbgDemo" which has a property, a regular method, and the static method by name "instance".
classdef dbgDemo < handle
properties
dbgFlag = 0;
end
methods
function show(obj)
fprintf('Debug Flag: %d\n', obj.dbgFlag);
end
end
methods (Static)
function i = instance()
persistent inst;
if (isempty(inst))
inst = dbgDemo();
end
i = inst;
end
end
end
To get the value stored in the debug flag, the following command works fine when given at the MATLAB command prompt:
dbgDemo.instance.dbgFlag
However, to set a value to the property, the command,
dbgDemo.instance.dbgFlag = 77
creates a local struct called "dbgDemo" with one field, a nested struct called "instance" with a field called "dbgFlag".

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 4 Jan 2012
This is intended behavior in MATLAB 7.6 (R2008a) in the way that the same variable used as an l-value and r-value can mean different things.
To workaround this issue, first create an instance of the class and then access the property using this instance. Here is an example:
x = dbgDemo.instance
x.dbgFlag = 77

More Answers (0)

Categories

Find more on Class Introspection and Metadata in Help Center and File Exchange

Products


Release

R2008a

Community Treasure Hunt

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

Start Hunting!