Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

THIS (or SELF) object in matlab

3 views
Skip to first unread message

dario

unread,
Nov 23, 2005, 7:46:39 AM11/23/05
to
Hi all,
there is a way to refers to the THIS object ( or SELF for the
smalltalkers?)

Specifically,
in MyGUI_1 I define a value and a callback
..
handles.myValue= someValue;
guidata(hObject, handles);
...


test_cb(val )
.... %something
-----------------------------------------------

When I call
MyGUI_1 ('test_cb',val)
I can use the 'val' value, [from inside the test_cb callback],
but I cannot use the myValue value, nor the handles.myValue .

If I define

test_cb(hObject, eventdata, handles,val )

and I call
MyGUI_1 ('test_cb',hObject, eventdata, handles,val)
I can't solve my problem, because 'handles' refers to the CALLING
OBJECT
So,what is the way to use the handles.myValue value?

For example,

THIS.myValue

or similar...

Thanks, and sorry for my poor English

Tom Krauss

unread,
Nov 23, 2005, 8:56:48 AM11/23/05
to
In article <1132749999....@o13g2000cwo.googlegroups.com>,
"dario" <giuli...@tiscali.it> wrote:

Dario,

After a great deal of thought, I came up with a way to obtain a "this"
pointer in MATLAB. It is in the context of "nested functions", a new
kind of programming capability introduced in MATLAB 7.

Nested functions make a lot of sense for GUIs. The simplest nested
function GUI is as follows: (note, "guide" does not yet create a nested
function as its M-file output, unfortunately)

function MyGui
u=uicontrol('style','pushbutton','string',...
'push me','callback',@buttonPushed);
x=0; % some data or "state" of the GUI

function buttonPushed(h,dum)
x=x+1;
fprintf('You pushed the button! x=%g\n',x)
end
end

Note the use of "end" at the end of the functions - the "buttonPushed"
function is "nested" inside the function "MyGui." Also note that the
button's callback is set to a function handle pointing to the nested
function. When MyGui completes, and buttonPushed is called, x from its
calling workspace is preserved!

[side note - the arguments to a function handle callback seem to be the
handle of the object, which I've called "h," and an empty variable I've
called "dum".]

One nice thing is that you can now start multiple such GUIs and MATLAB
keeps them around, each with its own "state" (i.e. workspace variables).
I like to think of them as "objects."

So how do you get a "this" pointer to a nested function object? I use a
MATLAB 5 style class to do this, called "obj". An obj object is a
nested function itself. It keeps a structure in its base workspace,
that can be manipulated (read and written) with "get" and "set" nested
functions; further-more, subsref and subsasgn allow the use of the
typical "." syntax to be overloaded to call these functions. Here's a
boiled down version of the class:

----- @obj/obj.m -----
function objOut = obj
% Objects of this type are nested function objects with some
% nice functionality for accessing variables in the nested
% function's workspace, calling methods, and supporting
% inheritance and polymorphism.
%

data = [];

objOut.get = @get;
objOut.set = @set;

objOut = class(objOut,'obj');

function out=get(varName)
out = data.(varName);
end

function set(varName,in)
data.(varName) = in;
end

end

----- @obj/subsref.m -----
function varargout=subsref(x,s)

switch s(1).type
case '()'
error('() reference not allowed')
case '{}'
error('{} reference not allowed')
case '.'
temp = x.get(s(1).subs);
if length(s)>1
if isa(temp,'function_handle')
[varargout{1:nargout}]=feval(temp,s(2).subs{:});
else
[varargout{1:nargout}]=subsref(temp,s(2:end));
end
else
varargout{1} = temp;
end
end

----- @obj/subsasgn.m -----
function a=subsasgn(a,s,b)

switch s(1).type
case '()'
error('() reference not allowed for assignment')
case '{}'
error('{} reference not allowed for assignment')
case '.'
if length(s)>1
temp = a.get(s(1).subs);
temp = subsasgn(temp,s(2:end),b);
a.set(s(1).subs,temp);
else
a.set(s(1).subs,b);
end
end

-----

You would use the obj object like this:

function this=MyGui
this=obj;
u=uicontrol('style','pushbutton','string',...
'push me','callback',@buttonPushed);
x=0; % some data or "state" of the GUI
this.y=1; % some "public" data
this.buttonPushed = @buttonPushed; % a public function

function buttonPushed(h,dum)
x=x+1;
this.y=1/x;
fprintf('You pushed the button! x=%g,y=%g\n',x,this.y)
end
end

So, do you really need a "this" pointer? With a stand-alone nested
function object, probably not. But if you would like to make such an
object that can inherit a base class and specialize its behavior, and/or
objects with public data members (instead of just public functions),
then you may find the "obj" object helpful.

Best regards,
Tom

Steven Lord

unread,
Nov 28, 2005, 11:11:10 AM11/28/05
to

"dario" <giuli...@tiscali.it> wrote in message
news:1132749999....@o13g2000cwo.googlegroups.com...

> Hi all,
> there is a way to refers to the THIS object ( or SELF for the
> smalltalkers?)
>
> Specifically,
> in MyGUI_1 I define a value and a callback
> ..
> handles.myValue= someValue;
> guidata(hObject, handles);
> ...
>
>
> test_cb(val )
> .... %something
> -----------------------------------------------
>
> When I call
> MyGUI_1 ('test_cb',val)
> I can use the 'val' value, [from inside the test_cb callback],
> but I cannot use the myValue value, nor the handles.myValue .

If test_cb is the callback for a Handle Graphics object and the "THIS"
object you want to retrieve is that Handle Graphics object, look at GCBO or
write your callback so it accepts two input arguments -- the first will be
the handle to the object whose callback is executing.

> If I define
>
> test_cb(hObject, eventdata, handles,val )
>
> and I call
> MyGUI_1 ('test_cb',hObject, eventdata, handles,val)
> I can't solve my problem, because 'handles' refers to the CALLING
> OBJECT
> So,what is the way to use the handles.myValue value?

What is hObject in this case?

If this doesn't answer your question, you'll need to post a small snippet of
code and explain in more detail to which specific object you want to get a
handle.

--
Steve Lord
sl...@mathworks.com


0 new messages