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.
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.
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With