Milan
Milan

Reputation: 1750

Matlab: static class members

I am trying to implement a list which would work as an instance counter in Matlab. I wish this list to be static.

So far, there's no static keyword in matlab, but they provide a pattern which almost do the job:

Declare a class List that inherit handle:

classdef List < handle

   properties (Access = private)
      cellRepresentingAList = {};
   end

   ..... %Implement any functions

end

Then you can use it the following way:

classdef MyClassUsingAList < handle

    properties (Constant, Access = private)
       myListOfStuff = List();
    end

    .....

 end

The fact that we are using an handle object as constant leads to the fact that every instance of MyClassUsingAList will use the same object handle, aka I have a static List.

Everything is working perfectly fine until I run into an exception. Or I debug and cease to debug using the "stop debug" button, or save. At this moment, the list is CLEARED. Empty.

This is not something I can put into production.

Matlab offers persistent variable, which play the role of static variables in functions, but it's not possible to use them in a class (unless you use the keyword 'persistent' in every method using the list).

Is there any reliable method to achieve this goal ?

Thanks

Upvotes: 3

Views: 734

Answers (1)

Tasos Papastylianou
Tasos Papastylianou

Reputation: 22225

Use the persistent keyword, as described here. (you went for the second approach, I would argue that what you need is this one). You can lock the functions from accidentally being cleared in the event of something like what you describe using the mlock commands.

Note that you would not have to define a persistent variable in each method as you say! (in fact, that would probably have the opposite effect to what you intend, since the persistent variable is unique to each function, rather than to the class as a whole). Simply create a single method with static access whose whole point is to keep a copy of that persistent variable, and reference that method in any other non-static methods that need to get or set it.

E.g.

classdef myclass

  properties
    Prop1;
    Prop2;
  end

  methods(Static, Access='public')
    function Out = StaticVar1(In)   % first static "variable"
      mlock
      persistent var;
      if isempty(var); var = 0; end          % initial value
      if nargin < 1; Out = var; return; end  % get value
      var = In;                              % set value
    end
    function Out = StaticVar2(In)   % second static "variable"
      mlock
      persistent var;
      if isempty(var); var = 0; end          % initial value
      if nargin < 1; Out = var; return; end  % get value
      var = In;                              % set value
    end
  end

  methods
    function Out = addStaticVars(o)
      Out = o.StaticVar1 + o.StaticVar2;
    end
  end
end

Example use:

>> a = myclass();
>> myclass.StaticVar1       % access the first static "variable"
ans =
     0
>> myclass.StaticVar2       % access the second static "variable"
ans =
     0
>> myclass.StaticVar1(10)   % set static "variable" to 10
>> myclass.StaticVar2(20)   % set static "variable" to 20
>> a.addStaticVars()        
ans =
    30
>> b = myclass();
>> b.addStaticVars()
ans =
    30

>> clear all;               % inneffective as functions are "locked"
>> myclass.StaticVar1
ans =
    10

>> munlock myclass.StaticVar1
>> clear all;        % this will now succeed in clearing the static "variable"
>> myclass.StaticVar1
ans =
     0

Upvotes: 1

Related Questions