Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Custom (non-graphics) Handle Objects; Address or Numeric ID

Subject: Custom (non-graphics) Handle Objects; Address or Numeric ID

From: Felipe Orihuela-Espina

Date: 19 Nov, 2009 12:59:06

Message: 1 of 11

In as similar fashion to:

>> h=figure

h =

     1

...I want to retrieve the numeric ID (preferable) or the address pointed to by the handle, for custom (non-graphics) Handle objects and its subclasses. Any suggestions? Thanks in advance

Subject: Custom (non-graphics) Handle Objects; Address or Numeric ID

From: Doug Schwarz

Date: 19 Nov, 2009 13:54:51

Message: 2 of 11

In article <he3feq$5as$1@fred.mathworks.com>,
 "Felipe Orihuela-Espina" <f.orihuela-espina@imperial.ac.uk> wrote:

> In as similar fashion to:
>
> >> h=figure
>
> h =
>
> 1
>
> ...I want to retrieve the numeric ID (preferable) or the address pointed to
> by the handle, for custom (non-graphics) Handle objects and its subclasses.
> Any suggestions? Thanks in advance


You can customize what gets displayed to the command window by defining
a display method for your class.

To actually retrieve some value from your object other than the object
itself you'll have to reference a property or use another method:

  ID = h.ID;

or

  ID = getID(h);

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: Custom (non-graphics) Handle Objects; Address or Numeric ID

From: Felipe Orihuela-Espina

Date: 19 Nov, 2009 14:19:21

Message: 3 of 11

Doug Schwarz <see@sig.for.address.edu> wrote in message <see-B4A11A.08545119112009@news.frontiernet.net>...
> You can customize what gets displayed to the command window by defining
> a display method for your class.
>
> To actually retrieve some value from your object other than the object
> itself you'll have to reference a property or use another method:
>
> ID = h.ID;
>
> or
>
> ID = getID(h);
>
> --
> Doug Schwarz

I do have a display method already, where I can display any properties of the object itself. But the unique numerical identifier (or the adress in memory) of the handle is not a property. Hence, it is not as simple as to access myobject.myProperty.

In the example of the figure in my original post, the call to h=figure does not visualize any property of the figure, but the unique numeric identifier assigned to the handle of the graphic object. I want precisely the equivalent to this for my handle subclasses!

Attempting to call display@handle in the display function doesn't help either...

Thanks for the reply anyway.

Subject: Custom (non-graphics) Handle Objects; Address or Numeric ID

From: Doug Schwarz

Date: 19 Nov, 2009 17:15:54

Message: 4 of 11

In article <he3k59$2te$1@fred.mathworks.com>,
 "Felipe Orihuela-Espina" <f.orihuela-espina@imperial.ac.uk> wrote:

> Doug Schwarz <see@sig.for.address.edu> wrote in message
> <see-B4A11A.08545119112009@news.frontiernet.net>...
> > You can customize what gets displayed to the command window by defining
> > a display method for your class.
> >
> > To actually retrieve some value from your object other than the object
> > itself you'll have to reference a property or use another method:
> >
> > ID = h.ID;
> >
> > or
> >
> > ID = getID(h);
> >
> > --
> > Doug Schwarz
>
> I do have a display method already, where I can display any properties of the
> object itself. But the unique numerical identifier (or the adress in memory)
> of the handle is not a property. Hence, it is not as simple as to access
> myobject.myProperty.
>
> In the example of the figure in my original post, the call to h=figure does
> not visualize any property of the figure, but the unique numeric identifier
> assigned to the handle of the graphic object. I want precisely the equivalent
> to this for my handle subclasses!
>
> Attempting to call display@handle in the display function doesn't help
> either...
>
> Thanks for the reply anyway.

Maybe you could explain a little more about why you want to do this.
What, exactly, are you planning on doing with this number?

Also, I'm still not sure whether you want to display this number to the
command line or you need its value in a variable. If the latter then
why is it not sufficient to put the object itself in that variable? If
the former then you could generate a random number in the constructor
and save it as an object property and then use that.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: Custom (non-graphics) Handle Objects; Address or Numeric ID

From: Felipe Orihuela-Espina

Date: 19 Nov, 2009 18:21:18

Message: 5 of 11

> Maybe you could explain a little more about why you want to do this.
> What, exactly, are you planning on doing with this number?
>

I have a classA (subclass of handle) with a property of classB (also subclass of handle) which in turn has a property of classA. They participate in a n to m relation, i.e. the same object A can be referenced by several object B, and viceversa, the same object B can be referenced by several A. Now, if I need to display either the whole object information or the property in particular pointing to the complementary object, I need to display either the numeric ID of the handle or the address, since attempting to visualize the pointed object may end up in an infinite loop, and any other information will be insufficient.

So basically I'm planning to visualize the number but not necessarily from the command line.

> Also, I'm still not sure whether you want to display this number to the
> command line or you need its value in a variable. If the latter then
> why is it not sufficient to put the object itself in that variable? If
> the former then you could generate a random number in the constructor
> and save it as an object property and then use that.

BOTH! I need the handle (numeric ID) in a variable, AND I need to visualize the handle value without visualizing the object it refers to!

Sorry! I'm not sure I do understand why do you suggest that I generate a random number. What I want is NOT a random number, but the unique ID generated by MATLAB to specifically refer to the object.

Subject: Custom (non-graphics) Handle Objects; Address or Numeric ID

From: Steven Lord

Date: 19 Nov, 2009 18:35:42

Message: 6 of 11


"Felipe Orihuela-Espina" <f.orihuela-espina@imperial.ac.uk> wrote in message
news:he42au$rfu$1@fred.mathworks.com...
>> Maybe you could explain a little more about why you want to do this.
>> What, exactly, are you planning on doing with this number?
>>
>
> I have a classA (subclass of handle) with a property of classB (also
> subclass of handle) which in turn has a property of classA. They
> participate in a n to m relation, i.e. the same object A can be referenced
> by several object B, and viceversa, the same object B can be referenced by
> several A. Now, if I need to display either the whole object information
> or the property in particular pointing to the complementary object, I need
> to display either the numeric ID of the handle or the address, since
> attempting to visualize the pointed object may end up in an infinite loop,
> and any other information will be insufficient.
>
> So basically I'm planning to visualize the number but not necessarily from
> the command line.

Why not give each class a static method that the constructors call to
generate an identifier unique to that instance, and have those static
methods (via a PERSISTENT variable) keep track of which identifiers have
been assigned? Then you could have the B object say that it is "linked to"
A object id1, id2, and id73.

>> Also, I'm still not sure whether you want to display this number to the
>> command line or you need its value in a variable. If the latter then
>> why is it not sufficient to put the object itself in that variable? If
>> the former then you could generate a random number in the constructor
>> and save it as an object property and then use that.
>
> BOTH! I need the handle (numeric ID) in a variable, AND I need to
> visualize the handle value without visualizing the object it refers to!
>
> Sorry! I'm not sure I do understand why do you suggest that I generate a
> random number. What I want is NOT a random number, but the unique ID
> generated by MATLAB to specifically refer to the object.

Handle Graphics object (root, figure, axes, uicontrol, etc.) and objects of
a class that subclass from handle are both objects with handle semantics,
but I don't believe there is any equivalent for objects of a handle class to
the numeric "handle value" returned by functions that create Handle Graphics
objects.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Custom (non-graphics) Handle Objects; Address or Numeric ID

From: Felipe Orihuela-Espina

Date: 19 Nov, 2009 18:57:21

Message: 7 of 11

> Why not give each class a static method that the constructors call to
> generate an identifier unique to that instance, and have those static
> methods (via a PERSISTENT variable) keep track of which identifiers have
> been assigned? Then you could have the B object say that it is "linked to"
> A object id1, id2, and id73.

Nice thought! But these generated numbers won't be the real identifiers generated by MATLAB, so not sure of the utility, since my problem is not to keep track of the objects themselves, or access to the referenced objects, but to visualize the handle, i.e. the MATLAB-generated identifier/address.

> Handle Graphics object (root, figure, axes, uicontrol, etc.) and objects of
> a class that subclass from handle are both objects with handle semantics,
> but I don't believe there is any equivalent for objects of a handle class to
> the numeric "handle value" returned by functions that create Handle Graphics
> objects.
>

If MATLAB does not generate an identifier, then what does MATLAB put in the handle itself? It is for certain that it is NOT the object itself, because then it will be a Value Class object. Thus, the handle is a reference to the object. Does MATLAB stored in the handle the physical address in memory in which the object is in a pointer-like manner? I do not think this is the case. Of course, I do not know the guts of MATLAB, but I guess thatMATLAB uses the handle for persistence of objects, and in that case the handle must be a unique identifier-like value, i.e. the one I'm trying to retrieve!.

Subject: Custom (non-graphics) Handle Objects; Address or Numeric ID

From: Doug Schwarz

Date: 19 Nov, 2009 22:23:55

Message: 8 of 11

In article <he44eh$7mu$1@fred.mathworks.com>,
 "Felipe Orihuela-Espina" <f.orihuela-espina@imperial.ac.uk> wrote:

> > Why not give each class a static method that the constructors call to
> > generate an identifier unique to that instance, and have those static
> > methods (via a PERSISTENT variable) keep track of which identifiers have
> > been assigned? Then you could have the B object say that it is "linked to"
> > A object id1, id2, and id73.
>
> Nice thought! But these generated numbers won't be the real identifiers
> generated by MATLAB, so not sure of the utility, since my problem is not to
> keep track of the objects themselves, or access to the referenced objects,
> but to visualize the handle, i.e. the MATLAB-generated identifier/address.
>
> > Handle Graphics object (root, figure, axes, uicontrol, etc.) and objects of
> > a class that subclass from handle are both objects with handle semantics,
> > but I don't believe there is any equivalent for objects of a handle class
> > to
> > the numeric "handle value" returned by functions that create Handle
> > Graphics
> > objects.
> >
>
> If MATLAB does not generate an identifier, then what does MATLAB put in the
> handle itself? It is for certain that it is NOT the object itself, because
> then it will be a Value Class object. Thus, the handle is a reference to the
> object. Does MATLAB stored in the handle the physical address in memory in
> which the object is in a pointer-like manner? I do not think this is the
> case. Of course, I do not know the guts of MATLAB, but I guess thatMATLAB
> uses the handle for persistence of objects, and in that case the handle must
> be a unique identifier-like value, i.e. the one I'm trying to retrieve!.


Internally, MATLAB must simply store a pointer to the object.
Unfortunately, other than by writing a mex function (overkill for this)
there is no way to obtain this pointer. Besides, you don't really want
to have to look at a couple of 32 bit numbers to see if they're the same
or different.

What Steve suggested is to generate a "serial number" for each object
generated and then use that.

I suggested using a random number which is the same as a pointer value
for your purposes (assuming you don't generate the same random number
twice -- unlikely). The advantage is that it's easier to code as you
don't need a persistent variable.

Or you could just use a time stamp of when the object was created. Just
make sure you use all available significant figures in case you generate
two objects one right after the other.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: Custom (non-graphics) Handle Objects; Address or Numeric ID

From: Yair Altman

Date: 20 Nov, 2009 09:33:11

Message: 9 of 11

"Felipe Orihuela-Espina" <f.orihuela-espina@imperial.ac.uk> wrote in message <he3feq$5as$1@fred.mathworks.com>...
> In as similar fashion to:
>
> >> h=figure
>
> h =
>
> 1
>
> ...I want to retrieve the numeric ID (preferable) or the address pointed to by the handle, for custom (non-graphics) Handle objects and its subclasses. Any suggestions? Thanks in advance

<snip long discussion>

If your class inherits from Matlab's handle, you can use the undocumented handle() function as in myRef = handle(myClassObject);
This returns a reference to the object You can even create an array of such handles.

Yair Altman
http://UndocumentedMatlab.com
 

Subject: Custom (non-graphics) Handle Objects; Address or Numeric ID

From: Felipe Orihuela-Espina

Date: 20 Nov, 2009 09:57:02

Message: 10 of 11

> If your class inherits from Matlab's handle, you can use the undocumented handle() function as in myRef = handle(myClassObject);
> This returns a reference to the object You can even create an array of such handles.
>
> Yair Altman
> http://UndocumentedMatlab.com
>

Not really;

>> a =classA; %classA inherits from handle
>> myRef = handle(a)
??? Error using ==> handle
Cannot convert to handle.

Subject: Custom (non-graphics) Handle Objects; Address or Numeric ID

From: Felipe Orihuela-Espina

Date: 20 Nov, 2009 09:58:04

Message: 11 of 11


> Internally, MATLAB must simply store a pointer to the object.
> Unfortunately, other than by writing a mex function (overkill for this)
> there is no way to obtain this pointer. Besides, you don't really want
> to have to look at a couple of 32 bit numbers to see if they're the same
> or different.

Okis!

> What Steve suggested is to generate a "serial number" for each object
> generated and then use that.
>
> I suggested using a random number which is the same as a pointer value
> for your purposes (assuming you don't generate the same random number
> twice -- unlikely). The advantage is that it's easier to code as you
> don't need a persistent variable.
>
> Or you could just use a time stamp of when the object was created. Just
> make sure you use all available significant figures in case you generate
> two objects one right after the other.
>

Aha! That is starting to make a lot of more sense to me now! Thanks!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us