How can I make sure that two variables I create are unique objects before using linkdata to avoid this issue?

2 vues (au cours des 30 derniers jours)
I've been using Matlab for years and I've never run into this issue before, but it appears that an optimization issue is happening when I do the following:
N = 20;
x_curr = zeros(N,1);
y_curr = zeros(N,1);
scatter(x_curr,y_curr,'.');
linkdata on, linkdata showdialog
x_curr=x_curr+rand(N,1); %substituting y_curr here will cause nothing to happen
Now when using "linkdata showdialog" it will show that both the objects in the figure are in fact x_curr.
Weirdly enough, this seems dependent on whether I use showdialog before or after trying to change x_curr. The above will treat both variables in the plot like x_curr, but the below will ignore the change and treat them both like y_curr:
N = 20;
x_curr = zeros(N,1);
y_curr = zeros(N,1);
scatter(x_curr,y_curr,'.');
linkdata on
x_curr=x_curr+rand(N,1);
linkdata showdialog
What fixes the issue seems to be making sure that the two arrays are distinct.
N = 20;
x_curr = zeros(N,1);
y_curr = zeros(N,1)+1;
scatter(x_curr,y_curr,'.');
linkdata on
y_curr = y_curr -1;
linkdata showdialog
x_curr=x_curr+rand(N,1);
Success! OK, so I get what happens, I think - Matlab does some behind-the-scenes optimization, figuring that the two arrays are the same and can therefore be treated interchangeably for the time being. And that causes linkdata to fail to identify them as distinct objects and therefore it loses track of them. I've seen similar issues quite a few times. While I've figured out workarounds, I'd like to have some robust ways of catching this issue before it occurs, and when necessary stopping Matlab from doing these kinds of optimizations.
So, first, is there any way I can access an "object ID", the way you can access, say, a pointer in C? Or does this concept not work for the way Matlab handles data?
Second, is there a way to force Matlab to create a distinct, unique, and persistent array that has a one-to-one relationship with its variable name, or in other words, to avoid these kinds of optimizations I didn't ask for? I know Python (which is what I use most of the time) has ways of dealing with these things, but I don't know of and haven't found any in Matlab (it seems to not be a commonly posed question).
Third, are there any issues related to this I should be aware of when using linkdata? And am I correct in my understanding that some hidden optimization causes issues like this?
Thanks!

Réponse acceptée

Walter Roberson
Walter Roberson le 28 Jan 2022
The official method to do what you want is to call
refreshdata()
  3 commentaires
Walter Roberson
Walter Roberson le 29 Jan 2022
Modifié(e) : Walter Roberson le 29 Jan 2022
So, first, is there any way I can access an "object ID"
If you are running at the MATLAB command line, or in traditional MATLAB, then you can use commands directly; if you are running in LiveScript (which this online Answers facility uses) then you need tricks like evalc() or running a .m file because LiveScript will not print out the debugging information.
cmd = sprintf('format debug\nx=[1 2 3]\ny=x\nx,y\nx=x+1\nx\ny\n')
cmd =
'format debug x=[1 2 3] y=x x,y x=x+1 x y '
evalc(cmd)
ans =
' x = Structure address = 7f60067bdd20 m = 1 n = 3 pr = 7f5ffab46520 1 2 3 y = Structure address = 7f60067bdd20 m = 1 n = 3 pr = 7f5ffab46520 1 2 3 x = Structure address = 7f60067bdd20 m = 1 n = 3 pr = 7f5ffab46520 1 2 3 y = Structure address = 7f60067bdd20 m = 1 n = 3 pr = 7f5ffab46520 1 2 3 x = Structure address = 7f60067a96c0 m = 1 n = 3 pr = 7f60000955c0 2 3 4 x = Structure address = 7f60067a96c0 m = 1 n = 3 pr = 7f60000955c0 2 3 4 y = Structure address = 7f60067bdd20 m = 1 n = 3 pr = 7f5ffab46520 1 2 3 '
Keys here:
  • use the undocumented "format debug"
  • You cannot normally get at the information given by format debug without using some mex(), so unfortunately you need tricks like evalc() if you want to examine it by using a program
  • pr is the data block, which can be shared in some cases such as using reshape()
  • Structure address can be the same even if identifier is not the same. The structure holds the dimension information and type information, and pointer to the actual memory
  • It should not be possible to have two different pr for the same structure address... at least not with both of them still being current information
Leonard Nielsen
Leonard Nielsen le 29 Jan 2022
Thanks a lot, this is useful to know as someone who is more used to working in C and Fortran! This seems pretty similar to how Python does it, and I'm guessing the problem I had before was with linkdata() working by some kind of guesswork rather than by any formal data links. I'm a bit surprised it's not considered deprecated since refreshdata is indeed a lot better.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by