Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
THIS (or SELF) object in matlab
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
dario  
View profile  
 More options Nov 23 2005, 7:46 am
Newsgroups: comp.soft-sys.matlab
From: "dario" <giulia....@tiscali.it>
Date: 23 Nov 2005 04:46:39 -0800
Local: Wed, Nov 23 2005 7:46 am
Subject: THIS (or SELF) object in matlab
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Krauss  
View profile  
 More options Nov 23 2005, 8:56 am
Newsgroups: comp.soft-sys.matlab
From: Tom Krauss <krauss_NOS...@labs.remove.mot.com>
Date: Wed, 23 Nov 2005 07:56:48 -0600
Local: Wed, Nov 23 2005 8:56 am
Subject: Re: THIS (or SELF) object in matlab
In article <1132749999.638354.84...@o13g2000cwo.googlegroups.com>,

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Lord  
View profile  
 More options Nov 28 2005, 11:11 am
Newsgroups: comp.soft-sys.matlab
From: "Steven Lord" <sl...@mathworks.com>
Date: Mon, 28 Nov 2005 11:11:10 -0500
Local: Mon, Nov 28 2005 11:11 am
Subject: Re: THIS (or SELF) object in matlab

"dario" <giulia....@tiscali.it> wrote in message

news:1132749999.638354.84860@o13g2000cwo.googlegroups.com...

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »