From: T on
I've written a class with a method that I would like to use to change the value of several of an object's properties, rather than writing set functions for each property. Can I return a reference to the object which calls the method (the "this" pointer in C++), or is that not possible in MATLAB?

Sample class:

classdef MyClass
properties
a;
b;
c;
end % properties
methods
... constructor, etc. ...
function obj = changeAll(obj, aNew,bNew,cNew)
obj.a = aNew;
obj.b = bNew;
obj.c = cNew;
... do more stuff to obj with the new data ...
% --> here is where I want to put: "return this" <----
end % changeAll
end % methods
end % classdef

As written, this method can be used in the following way:
OBJECT = MyClass;
OBJECT = OBJECT.changeAll(1,2,3);

But I'm wondering if there's a way to do this more elegantly, i.e., so that

OBJECT.changeAll(1,2,3);

changes OBJECT directly. Any ideas?
From: Andy on
"T " <notarealaddress(a)nowhere.com> wrote in message <i3hmkg$am7$1(a)fred.mathworks.com>...
> I've written a class with a method that I would like to use to change the value of several of an object's properties, rather than writing set functions for each property. Can I return a reference to the object which calls the method (the "this" pointer in C++), or is that not possible in MATLAB?
>
> Sample class:
>
> classdef MyClass
> properties
> a;
> b;
> c;
> end % properties
> methods
> ... constructor, etc. ...
> function obj = changeAll(obj, aNew,bNew,cNew)
> obj.a = aNew;
> obj.b = bNew;
> obj.c = cNew;
> ... do more stuff to obj with the new data ...
> % --> here is where I want to put: "return this" <----
> end % changeAll
> end % methods
> end % classdef
>
> As written, this method can be used in the following way:
> OBJECT = MyClass;
> OBJECT = OBJECT.changeAll(1,2,3);
>
> But I'm wondering if there's a way to do this more elegantly, i.e., so that
>
> OBJECT.changeAll(1,2,3);
>
> changes OBJECT directly. Any ideas?

Perhaps you should make MyClass a subclass of the handle class?
From: T on
"Andy " <myfakeemailaddress(a)gmail.com> wrote in message <i3hms1$phc$1(a)fred.mathworks.com>...

> Perhaps you should make MyClass a subclass of the handle class?

How would that help? I'm ignorant of handle classes.
From: Andy on
"T " <notarealaddress(a)nowhere.com> wrote in message <i3hn7p$j2e$1(a)fred.mathworks.com>...
> "Andy " <myfakeemailaddress(a)gmail.com> wrote in message <i3hms1$phc$1(a)fred.mathworks.com>...
>
> > Perhaps you should make MyClass a subclass of the handle class?
>
> How would that help? I'm ignorant of handle classes.

Handle classes act as references. The prime examples are the GUI components used in MATLAB. Read the documentation on the handle class for more info and examples.
From: T on
"Andy " <myfakeemailaddress(a)gmail.com> wrote in message <i3hnvl$7rc$1(a)fred.mathworks.com>...
> "T " <notarealaddress(a)nowhere.com> wrote in message <i3hn7p$j2e$1(a)fred.mathworks.com>...
> > "Andy " <myfakeemailaddress(a)gmail.com> wrote in message <i3hms1$phc$1(a)fred.mathworks.com>...
> >
> > > Perhaps you should make MyClass a subclass of the handle class?
> >
> > How would that help? I'm ignorant of handle classes.
>
> Handle classes act as references. The prime examples are the GUI components used in MATLAB. Read the documentation on the handle class for more info and examples.

Excellent. Thanks. After a bit of reading, all I changed in the code is "MyClass < handle" and removed the "obj = " function returns. Now it works perfectly!
 |  Next  |  Last
Pages: 1 2
Prev: cepstrum
Next: Plotting questions