Cannot update class definition in Matlab

I ran into the problem of homelessness with Matlab and an earlier answer apparently to the same problem, unfortunately, did not help me. I apologize that the question is quite long - you need a lot of information to reproduce the problem (I tried to trim it as much as I could).

The problem is this: no matter what I do, after I used the class, I cannot โ€œmake Matlab forgetโ€. The values โ€‹โ€‹used seem constant, and changes to the class definition will not stick. In the latter case, the error message is:

Note: the class file for 'myClass' has been modified; but the change cannot be applied because objects based on the old class file still exist. If you use these objects, you may get unexpected results. You can use the "clear" command to delete these objects. See "Help clear" for information on how to delete these objects.

I get this message even after

>> clear all >> clear functions >> clear ans 

Somehow the definition of a class is constant, despite my attempts to clear it. Even worse, when I change the value of an instance of a class, then clear it, the value is somehow not "forgotten". To illustrate, here is the source code for myClass :

 % a simple class definition that shows the problem that I cannot % figure out how to redefine a class without restarting Matlab classdef myClass < handle properties precursors = {'none'}; numPre = {1}; value = 1; end methods function obj = myClass(pre, num, val) % constructor if nargin > 0 obj.precursors = pre; obj.numPre = num; obj.value = val; end end function v = sumVal(obj) % find the sum of the value of all precursors n = numel(obj.precursors); v = 0; for ii = 1:n pc = obj.precursors{ii}; if isa(pc, 'myClass') if ii==1 v = 0; end v = v + sumVal(pc) * obj.numPre{ii}; else v = obj.value; end end end end % only the following named instances may exist: enumeration grandpa ({'none'}, {1}, 1) father ({myClass.grandpa}, {3}, -1) son ({myClass.father}, {2}, -1) end end 

In a new Matlab instance, I do the following:

 >> son = myClass.son; >> sumVal(son) ans = 6 >> grandpa = myClass.grandpa; >> grandpa.value = 5; >> sumVal(son) ans = 30 

So far so good. The sumVal function detects fathers and grandfathers, and sumVal calculated correctly (6 * 1 in the first case, 6 * 5 in the second case).

Now I delete "everything" (I think):

 >> clear all >> clear functions >> clear ans 

And I only create one variable:

 >> son = myClass.son; 

Now comes the kicker - an unexpected answer

 >> sumVal(son) ans = 30 

When I check the loaded variables, I find

 >> whos Name Size Bytes Class Attributes son 1x1 112 myClass 

There is no instance of grandpa , and the class definition file was not affected. However, the grandpa value (which I created and then deleted) is somehow stable.

And when I make a small change in the myClass.m file and try to create a new variable (after clear all ), I get the message shown above. All this leads me to my question:

Where Matlab hides an instance of my class so that the variables are saved after clear all , and how to clear the workspace (without restarting), so the definition of the class is "reset"?

I don't know if this matters, but I'm using Matlab 7.14.0.739 (R2012a)

+4
source share
2 answers

You have an intermediate instance of myClass.father that is not destroyed by MATLAB. You must delete yourself

 >> clear grandpa >> delete(son.precursors{1}) >> clear son >> clear classes >> son = myClass.son son = son >> sumVal(son) ans = 6 

Edit: Alternatively, you can add a destructor to your class

  function delete(obj) if isa(obj.precursors{1}, 'myClass') delete(obj.precursors{1}); end end 

and use delete(son) instead of leaving it for the clear function to destroy. You can extend this to your case and recursively delete all instances in your tree.

+6
source

These instances are "hidden" in the myClass enumeration class itself. Matlab stores a link to each of these named instances, so when you reference them as myClass.father , you get the same object back, rather than creating a new one. It probably looks like the way values โ€‹โ€‹are stored in Constant properties on classes.

If you have other classes related to myClass.xxx enumerated instances in Constant properties, enums, or persistent variables, they can also reference them.

Try to make clear classes several times in a row, and not just once.

To debug this, you can put a couple of printf() debugging commands in the constructor and destructor for this class so you can see when the instances are actually created and cleaned up.

  function obj = myClass(pre, num, val) % constructor if nargin > 0 obj.precursors = pre; obj.numPre = num; obj.value = val; end printf('myClass: created (%d, %d, nargin=%d)\n', obj.numPre, obj.value, nargin); end function delete(obj) printf('myClass: deleting (%d, %d)\n', obj.numPre, obj.value); end 
+2
source

Source: https://habr.com/ru/post/1487541/


All Articles