Reputation: 770
I am debugging some MATLAB code, and want to make sure that two references to an object are actually referring to the same object. Is there a way to obtain a unique identifier for the objects (such as a memory address)?
As far as I know I am not able to add my own IDs to the objects, as they are MATLAB random number streams.
Upvotes: 11
Views: 2876
Reputation: 112679
You can use the UserData
field, which is present in every graphical object, to store a unique identity generated by you. If working with a user-defined class, you can add a similar field in your class.
The identities can be kept unique by using a global counter to assign each new identity.
Upvotes: 0
Reputation: 24127
If the objects you're wanting to compare are MATLAB random number streams (i.e. they are of class RandStream
), then they are handle objects. In that case you don't need unique IDs: if you compare them using eq
or ==
and they are equal, then they are the same object.
As you say, you are not able to add your own properties to an object of class RandStream
, but if you really wanted to you could subclass RandStream
and add a property of your own to the subclass. You could store a unique identifier in the property, generated with char(java.util.UUID.randomUUID)
.
Upvotes: 6
Reputation: 3193
If you are using OOP then you could add a property ID
and set it during the construction of the object.
java.rmi.server.UID()
is a nice way to obtain unique ID's
However testing by ==
will check the actual handles, so this is more of a usability issue.
classdef yourClass < handle
properties
ID
end
methods
function obj = yourClass()
obj.ID = java.rmi.server.UID();
end
end
end
It will then be rather simple to check your objects.
Upvotes: 5