On Jan 3, 2013, at 9:22 PM, Michael
Goffioul wrote:
> On Thu, Jan 3, 2013 at 9:11 PM, Ben Abbott <
address@hidden>
wrote:
> On Jan 3, 2013, at 8:05 PM, Michael Goffioul wrote:
>
> > Hi,
> >
> > Could someone test something I'm wondering
about classdef reload in MATLAB. Let's say there's a
class defined as:
> >
> > classdef ClassA < handle
> > properties
> > p1 = 0;
> > end
> > end
> >
> > At the prompt, create an object of ClassA:
> >
> > x = ClassA()
> >
> > Then, without closing MATLAB, edit the
classdef file and a second property, the class now looks
like this:
> >
> > classdef ClassA < handle
> > properties
> > p1 = 0;
> > p2 = 1;
> > end
> > end
> >
> > At the prompt, create a second object of
ClassA:
> >
> > y = ClassA()
> >
> > The question is: what are the properties of x
and y?
> >
> > A similar question occurs for methods. If the
initial definition of ClassA contains a method m1. After
creation of x, a second method m2 is added and another
object is created. Is method m2 valid for object x? That
is, does this succeeds (assuming m2 does not take any
argument):
> >
> > x.m2()
> >
> > Michael.
>
> (I used the wrong email account the first time, so
I'm resending)
>
> My first impression was that the objects were
registered with the class ... but they apparently are
not(?)
>
> "clear x y" did not allow the modified class to be
used. I had to "clear all" before the modified class
could be used. I assume that implies that a class
cannot be over-ridden once is has been loaded, unless it
is cleared first.
>
> In any event, the command you asked for and some
more are below.
>
> x = ClassA ()
> x =
>
> ClassA handle
>
> Properties:
> p1: 0
>
> Methods, Events, Superclasses
>
> y = ClassA ()
> Warning: The class file for 'ClassA' has been
changed, but the change cannot be applied because
objects based on the old class file still exist. If you
use those
> objects, you might get unexpected results. You can
use the 'clear' command to remove those objects. See
'help clear' for information on how to remove those
> objects.
>
> y =
>
> ClassA handle
>
> Properties:
> p1: 0
>
> Methods, Events, Superclasses
>
> clear y
> x = ClassA
> Warning: The class file for 'ClassA' has been
changed, but the change cannot be applied because
objects based on the old class file still exist. If you
use those
> objects, you might get unexpected results. You can
use the 'clear' command to remove those objects. See
'help clear' for information on how to remove those
> objects.
>
> x =
>
> ClassA handle
>
> Properties:
> p1: 0
>
> Methods, Events, Superclasses
>
> clear x y
> x = ClassA
> Warning: The class file for 'ClassA' has been
changed, but the change cannot be applied because
objects based on the old class file still exist. If you
use those
> objects, you might get unexpected results. You can
use the 'clear' command to remove those objects. See
'help clear' for information on how to remove those
> objects.
>
> x =
>
> ClassA handle
>
> Properties:
> p1: 0
>
> Methods, Events, Superclasses
>
> >> clear all
> >> x = ClassA
>
> x =
>
> ClassA handle
>
> Properties:
> p1: 0
> p2: 0
>
> Methods, Events, Superclasses
>
> Ben
>
> Interesting, thanks. I suppose the result is the
same with methods? And does it matter if ClassA does not
inherit from handle?
>
> Also does MATLAB reload methods that are defined in
separate files? To test this, you need to define the
following files, using a @-folder:
>
> @ClassA/ClassA.m
> @ClassA/m1.m
>
> with the respective content:
>
> @ClassA/ClassA.m:
>
> classdef ClassA < handle
> methods
> result = m1 (obj)
> end
> end
>
> @ClassA/m1.m:
>
> function result = m1 (obj)
> result = 1;
> end
>