I am looking for a matlab function that has the same functionality as python's id()
, i.e. something unique for an object, and constant through its lifetime..
Asked
Active
Viewed 171 times
0

Shai
- 111,146
- 38
- 238
- 371

shittywizard
- 35
- 7
-
what kind of matlab's object do you need an `id()` for? – Shai Jun 25 '13 at 07:04
-
http://www.mathworks.com/matlabcentral/answers/3314 might help – dfb Jun 25 '13 at 07:08
-
@dfb: Thanks, I'm checking it out. – shittywizard Jun 25 '13 at 07:36
-
why do you need this functionality for? you can compare matlab arrays using `isequal` – Shai Jun 25 '13 at 07:43
-
@Shai:I need to perform some sort of caching. – shittywizard Jun 25 '13 at 08:12
-
1please provide a borader context so we can provide better answers. – Shai Jun 25 '13 at 08:16
-
1@Shai: I am transcribing code for a machine learning algorithm (BAHSIC) from python 2.6 into Matlab (R2008a). There is a bunch of Kernels that needs to be implemented, and requires pre-processing the input data and then caching it as a part of the kernel object. The way it has been done in python is cache[id(x)] = (x**2).sum(axis=1). The variable cache here is a part of the kernel object. I need to do the same in matlab. – shittywizard Jun 25 '13 at 08:41
-
@ktulsyan: [Caching result of pre-computed function in Matlab](http://stackoverflow.com/questions/11178367/caching-result-of-pre-computed-function-in-matlab) (also see the links provided in there) – Amro Jun 25 '13 at 09:30
-
you could also use the undocumented [`mxSerialize`](http://stackoverflow.com/a/6261884/97160) to serialize any variable in MATLAB and generate a hash from the byte stream – Amro Jun 25 '13 at 09:34
-
Another alternative is to create your own struct consisting of a unique ID and the data, though this might be tedious to implement – dfb Jun 25 '13 at 15:43
1 Answers
3
For all handle-like objects the identifier analogue to id() is probably the double-converted value:
>> h = uicontrol
h =
9.7656e-004
>> handle(h)
ans =
uicontrol
For non-handle type objects such a thing probably doesn't exists, since Matlab usually uses call-by-value instead of call-by-reference. So in general, upon changing a variable's value, there's no guarantee that the underlying object remains the same.

sebastian
- 9,526
- 26
- 54