I have structure array, let's say, like this:
s.var1 = 10; s.var2 = rand(3,3); s.var3 = 'my structure';
I want to associate a unique identifier to structure s. That unique identifier should change whenever I change either the structure or the values of its fields.
Thanks
No products are associated with this question.
You can use FEX: DataHash to get a fingerprint for the contents of a struct.
Walter's suggestion of a "dirty" flag would be a clear and clean solution, which is applied freuqently.
5 Comments
Direct link to this comment:
http://www.mathworks.com/matlabcentral/answers/55833#comment_115559
That cannot be done automatically with a structure array. Period. It can be mimicked with a class.
Direct link to this comment:
http://www.mathworks.com/matlabcentral/answers/55833#comment_115560
I'm not sure what it would mean to change the structure without changing the value of its fields? Do you mean, for example, that if you assign
that s1 should automatically be given a new identifier?
Direct link to this comment:
http://www.mathworks.com/matlabcentral/answers/55833#comment_115572
@isakson: thanks ... do you know how can I mimic it with a class?
@Robeson: By changing the structure I meant I add a new field like:
or I change the value of an already existing variable:
Thanks for the help guys.
Direct link to this comment:
http://www.mathworks.com/matlabcentral/answers/55833#comment_115575
My understanding is that you want to define a new variable (like a pointer in C) that when you change that variable, it changes s? I've never heard of this capability in MATLAB.
Direct link to this comment:
http://www.mathworks.com/matlabcentral/answers/55833#comment_115576
Does it need a new identifier every time it changes? Or does it just need to be a different identifier for each distinct structure?
For example, if after you do
you then, without otherwise using s, do
then does the second change need to be have a different identifier than was the case after the first change?
If you are needing this for structure uniqueness, then the identifier would only have to change if there was another copy of the structure. And that is something that can be detected at a lower level by looking at the internal pointers, due to the way that copy-on-write happens in MATLAB.
But if you are instead trying to easily track whether the structure has changed somehow relative to when something last looked at it, then there can be other methods.
In the simple case you can set a "dirty" flag in the structure when you make a change to it, and then the referencing code can just examine the "dirty" flag to see if it needs to update something, clearing the "dirty" flag afterwards. In more complex cases, sometimes it is enough to keep a "generation number" for the structure; I have used that technique.