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)