Linkprop not persisting after changing scope

6 views (last 30 days)
Guillermo Ibanez
Guillermo Ibanez on 1 Jul 2018
Answered: Peter on 23 Dec 2019
Hi there,
I am building a GUI with two patch plots. I want these two plots to rotate simultaneously, so I wrote a function in which I use linkprop to link their camera rotation. This works but only within the scope of the function. When the scope is changed the plots are no longer linked.
Here you have a code example of the problem. You can check that the rotation link only works within the 'foo' function.
a = 1;
foo();
b = 2;
function foo()
figure
ax1 = subplot(2,1,1);
[X1,Y1,Z1] = peaks;
surf(X1,Y1,Z1)
ax2 = subplot(2,1,2);
[X2,Y2,Z2] = peaks(10);
surf(X2,Y2,Z2)
hlink = linkprop([ax1,ax2],{'CameraPosition','CameraUpVector'});
rotate3d on
end
My question is then if it is possible to make 'linkprop' persist after changing the scope or if there is another alternative.
Cheers, Guillermo

Answers (1)

Peter
Peter on 23 Dec 2019
Making sure the hlink object is available outside the scope of your function foo by making it an output argument probably solves your problem. it worked for me:
About Link Objects
The link object that linkprop returns stores the mechanism that links the properties of different graphics objects. Therefore, the link object must exist within the context where you want property linking to occur (such as in the base workspace if users are to interact with the objects from the command line or figure tools).
The following list describes ways to maintain a reference to the link object.
  • Return the link object as an output argument from a function and keep it in the base workspace while interacting with the linked objects.
  • Make the hlink variable global.
  • Store the hlink variable in an object's UserData property or in application data

Community Treasure Hunt

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

Start Hunting!