On Thu, Apr 23, 2009 at 11:46 AM, John W. Eaton
<address@hidden> wrote:
On 23-Apr-2009, Judd Storrs wrote:
| So, I think Matlab keeps an internal memory of each class structure.
| Probably the first time a class of a specific type is created, Matlab
| remembers the member information. If Matlab doesn't have member information
| it calls the constructor with no arguments. I'll do some more testing to
| verify.
OK. Is that information separate from any objects that are in the
symbol table? I.e., does "clear all" followed by loading the file
result in the error message when there is no valid constructor?
Clear all doesn't seem to clear the class memory
>> bar = Foo()
bar = Foo object: 1-by-1
>> save test.mat bar
>> clear all
>> load test.mat
>> class(bar)
ans = Foo
Outside of matlab, break @Foo/Foo.m
>> clear all
>> load test.mat
>> class(bar)
ans =Foo
>> clear all
>> bar2 = Foo()
bar2 = []
>> load test.mat
>> class(bar)
ans = Foo
What happens if you modify the constructor (for example, to add or
remove a field from the class structure) when Matlab is running and
already has some object(s) created using the previous constructor?
Does that generate an error?
Reset @Foo/Foo.m
function Foo ()
f.dataelement = 'I am Foo';
f = class(f,'Foo');
>> bar = Foo()
bar = Foo object: 1-by-1
>> struct(bar)
ans = dataelement: 'I am Foo'
Outside matlab add a new field to @Foo/Foo.m:
function Foo ()
f.dataelement = 'I am Foo';
f.newelement = 'I am even more Foo';
f = class(f,'Foo');
>> bar2 = Foo()
??? Error using ==> class
Number of fields for class Foo cannot be changed without clear classes.
Error in ==> Foo.Foo at 4
f = class(f,'Foo');
>> clear classes
>> bar2 = Foo()
bar2 = Foo object: 1-by-1
>> class(bar)
??? Error using ==> bar at 48
Not enough input arguments.
>> struct(bar2)
ans =
dataelement: 'I am Foo'
newelement: 'I am even more Foo'
>> struct(bar)
??? Error using ==> bar at 48
Not enough input arguments.
>> help clear
...
CLEAR CLASSES is the same as CLEAR ALL except that class definitions
are also cleared. If any objects exist outside the workspace (say in
userdata or persistent in a locked m-file) a warning will be issued and
the class definition will not be cleared. CLEAR CLASSES must be used if
the number or names of fields in a class are changed.
...
--judd