Use of Persistent Variables in Class Methods Producing Incorrect Result.

I am attempting to make use of a persistent variable within a class method. However, invoking the method from different instances is producing incorrect results. Here is a simple case of what I mean:
CLASS:
classdef MYCLASS < handle
properties(GetAccess = 'private', SetAccess = 'private')
m_Count
end
methods(Access = 'public')
%constructor
function obj = MYCLASS
obj.m_Count = 0;
end
function out = DO_SOMETHING_MOUSE(obj)
persistent n
if isempty(n)
n = 0;
end
n = n+1;
out = n;
end
function out = DO_SOMETHING_CAT(obj)
obj.m_Count = obj.m_Count + 1;
out = obj.m_Count;
end
end
end
MATLAB FILE:
clear all
clear classes
clc
Instance_A = MYCLASS;
Instance_B = MYCLASS;
n = Instance_A.DO_SOMETHING_MOUSE
n = Instance_B.DO_SOMETHING_MOUSE
n = Instance_A.DO_SOMETHING_CAT
n = Instance_B.DO_SOMETHING_CAT
COMMAND WINDOW RESULTS:
a =
1
b =
2
a =
1
b =
1
>>
Here you can see that we call the DO_SOMETHING_MOUSE method from two different instances of the class, yet, the value of the persistent variable "n" was somehow shared between instances.
The result we should have gotten is that which was illustrated by the call to the DO_SOMETHING_CAT method. Each instance had it's own value correctly stored.
What's wrong?

 Accepted Answer

This is the expected behavior. Persistent variables are the same as global variables expect for the fact that these variables can be accessed only within the function in which they are created. These variables retain their value till they are cleared from the memory.
When you created two different instances of the class, two independent instances were created with their own properties, whereas only one instance of the persistent variable was created in the memory. Therefore both objects access the same memory for the persistent variable

6 Comments

Sounds reasonable. However, a reference to the documentation of this behavior would have made your answer more valuable.
The standard documentation on Persistent variables applies there is no change in behavior when used in a class method.
I find it hard to include in my mental model that separate instances of the same class share values of variables. Consequently, it took me some time to get the difference between assigning a handle object as value of a property in the properties block and in the constructor. Now I avoid the properties block. (And I never use global.)
I can only think of one good use of persistent in a class definition and that is the Singleton pattern. In that case I have no problem, because there is only one instance by definition.
Class is not mentioned in the documentation of persistent. I think that myself and others would be served by a sentence or two in the documentation on the use of persistent in class definitions.
i know this is an old thread, but i do feel like i need to comment on this for future readers.
The Singleton pattern is NOT a good usecase for persistent data. It is better to use a constant property with an instance of a handleclass assigned to it for this usecase.
Persistent data is used where you would use simple static data inside your class, think of it like a protected static variable in java, whereas the constant property is more a public static final in java.
Hi @Srijith Vijay and all others
Sorry to comment on this old post, but I need something 'similar'. And as a PLC programmer I'm still struggling with matlab.
In my case I'm trying to create a class that shall handle a part of our device. For maintenance reasons I would like to be able to store some countervalues 'permanent' for each instance.
So as an example
Our machine will have multiple cylinders and I want to count (and store) the number of strokes for each of them. Our maintenance engineer could inspect those values and if the countnumber is above a certain value, then the unit can be replaced (and the counter resetted).
Within a PLC programm (CoDeSys), I can set those counters as 'persistent' and each instance of them will be stored somewere in the PLC memory. But we are now transferring to an embedded microprocessor solution and I need to make it within the matlab environment. Thanks for any suggestions.
With kind regards
Ludo
Ludo, if you could provide a simple example or pseudocode of your intended task, I or another community member might be able to offer you some suggestions.

Sign in to comment.

More Answers (1)

I reproduced the behavior with R2013a and it's not what I would have expected. However, whether it's "incorrect results" depends on what the documentation says.
It resembles a case, which is discussed at UndocumentedMatlab, Handle object as default class property value. See especially the comment by David Foti at April 10, 2015 at 2:42 pm.

Categories

Find more on Code Execution in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!