Linkprop not working as I'd expect

9 views (last 30 days)
davidnhutch
davidnhutch on 23 Dec 2015
Commented: davidnhutch on 28 Dec 2015
I have an image with two axes (because I want one scale on the left and one scale on the right).
I read on this page that you can link two axes so when you resize the figure, both axes go along for the ride. But when I try it, it does nothing.
To see what I mean, try to run the code below. Then resize the figure window manually and you'll see that the axes do not track each other. By "track each other" I mean that the left and right axes are 'stuck' to the side of the image always, and if you resize the figure window then the image resizes, but both y axes resize appropriately also.
How can I have two axes that exactly track each other even when the figure window is resized? In other words, the black axis and the red axis in the code below should behave identically when the window is resized.
clear;
close all;
figure;
imshow('coins.png');
ax1 = gca;
set(ax1,...
'XColor','r',...
'YColor','r');
axis on;
ax2 = axes(...
'Position',ax1.Position,...
'YAxisLocation','Right',...
'Color','none',...
'XTick',[],...
'YLim',[0,10]);
all_axes = findall(gcf,'type','axes');
linkprop(all_axes,'position');
Thanks!

Answers (1)

Arnab Sen
Arnab Sen on 28 Dec 2015
From the description I understand that when you resize the figure the right Y axes is getting resized while the left one is not and you would like to have both of them behave identically. The reason the issue you are facing is you are showing the image first then set the 'gca' to the variable 'ax1'. At that point the 'gca' is the axes of image itself. On the other hand the axes 'ax2' is independent from the image. Tht is why one is resized while other is not. As a workaround you can make both the axes independent from the image in which case both of them behave identically when the figure gets resized. The following code snippet illustrates the above:
>> clear;
>> close all;
>> figure;
>> imshow('coins.png');
>> ax1 = axes(...
>> 'YAxisLocation','left',...
>> 'Color','none',...
>> 'XTick',[],...
>> 'YLim',[0,20]);
>> ax2 = axes(...
'Position',ax1.Position,...
'YAxisLocation','Right',...
'Color','none',...
'XTick',[],...
'YLim',[0,10]);
  1 Comment
davidnhutch
davidnhutch on 28 Dec 2015
That does indeed make the two axes track each other, however it does not solve the problem. The axes are intended to also track the image itself, in the same way that the red axis perfectly tracks the image in the original example. Why? Because the axes provide a 'scale' for the image.
In other words, in the original example I hope to have the black axis behave exactly like the red axis. Black axis is wrong, red axis is right; and I hope to have both black and red axes simultaneously.
I have tried many other things, and am just about to give up as I don't believe this is possible using Matlab. :-( Too bad

Sign in to comment.

Categories

Find more on Visual Exploration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!