Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you obtain an unique identifier for a MATLAB object?

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.

like image 622
jgosmann Avatar asked Sep 04 '13 14:09

jgosmann


2 Answers

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.

like image 140
Nick Avatar answered Oct 11 '22 21:10

Nick


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).

like image 37
Sam Roberts Avatar answered Oct 11 '22 20:10

Sam Roberts