Old code:
topAxsRatio = photoAxsRatio; topAxsRatio(2) = photoAxsRatio(2)/2.4; % I want to get rid of this number! set(topAxs,'PlotBoxAspectRatio', topAxsRatio)
New code:
photoratio = photoAxs.PlotBoxAspectRatio(1)/photoAxs.PlotBoxAspectRatio(2); ratio = photoratio * photoAxs.Position(4)/topAxs.Position(4); topAxs.PlotBoxAspectRatio = [ratio, 1, 1];
Result:

A little explanation:
When the numbers are first displayed, you will notice that only the heights are different, although you can clearly see that the width is also different.
I am not 100% sure why Matlab does this, but this is my hunch.
Usually, two properties, width and height, are enough to determine the size of a two-dimensional figure, but Matlab introduces an additional PlotBoxAspectRatio property to control the size. To avoid conflict, Matlab decides to set the width property to a fixed number when the shape is first created. However, the actual width is calculated using height*PlotBoxAspectRatio .
Therefore, we have:
TopAxis.width = TopAxis.height * TopAxis.ratio Photo.width = Photo.height * Photo.ratio
To preserve the initial height of TopAxis, we can only change the aspect ratio.
Let be
TopAxis.width = Photo.width
We have
TopAxis.height * TopAxis.ratio = Photo.height * Photo.ratio TopAixs.ratio = Photo.ratio * Photo.height / TopAxis.height
And the Matlab code equivalent is the new code proposed.
source share