How to get the identifier of the MATLAB processing object?

The problem arises when I try to use MATLAB to process objects as key values ​​in MATLAB. Map .

ld( h1, h2 ) defines a linear order for descriptor objects; therefore, there should be no restriction on the use of descriptor objects as key values ​​for cards; however, only integer or string types are allowed.

A workaround for this problem could be to extract the actual identifiers (addresses) of the handle objects (which are mostly compared in the ld function).

So the question is: how do I get the handle object identifier?


It has been found that a workaround can be made using constant variables in static member functions.

In this case, you should inherit all your classes from the base class as follows.

 classdef object < handle properties ( GetAccess = 'public', SetAccess = 'private' ) id end methods ( Access = 'protected' ) function obj = object() obj.id = object.increment(); end end methods ( Static, Access = 'private' ) function result = increment() persistent stamp; if isempty( stamp ) stamp = 0; end stamp = stamp + uint32(1); result = stamp; end end 

end

+4
source share
1 answer

I have never heard of something like a HashCode object in Java / C # applied to MATLAB OO. If you get the address of the MATLAB object ( format debug type in the command window), it is still impractical to use it, because it will not remain the same as in C ++, but will be moved by the system (managed memory).

You can manually implement the getHashCode() interface for your MATLAB objects. Unlike the traditional hash code, you have to make sure that your hash code is always different for different objects - not an easy task!

The default comparison function MATLAB sort seems to use the hashcode object internally, but that will not help you here - comparing objects is actually the orthogonal concept of their hash code.

+1
source

All Articles