When 'imshow' is called, it rearranges a few of the axes properties of app.UIAxes. It changes the direction of the y-axis ('YDir'), the modes of both the aspect ratios ('PlotBoxAspectRatioMode' and 'DataAspectRatioMode'), and the values of the aspect ratios ('PlotBoxAspectRatio' and 'DataAspectRatio'). We will need to preserve the original values and reassign them after 'imshow' is called.
The first row of pixels within an image is normally at the top. By default, the 'imshow' function inverts the y-axis direction when the image is displayed on an axes by setting the 'YDir' property to 'reverse.' To revert the y-axis direction after calling 'imshow', set the 'YDir' property to 'normal'. Then, to preserve the orientation of the image, flip the image using 'flip'.
Next, the 'imshow' function sets the 'DataAspectRatioMode' property of app.UIAxes to 'manual' and the 'PlotBoxAspectRatioMode' to 'auto'. The 'DataAspectRatio' property of app.PlotLegend is then rearranged to match the ratio of the image, and the 'PlotBoxAspectRatioMode' modifies itself accordingly.
In the code below, the original 'PlotBoxAspectRatio' is preserved, then the 'PlotBoxAspectRatioMode' is set to 'manual' while the 'DataAspectRatioMode' is set to 'auto'. Finally, the 'PlotBoxAspectRatio' is reassigned to the property's original value and the 'DataAspectRatio' property modifies itself accordingly.
function ButtonPushed(app, event)
v = [60,0; 75,0; 75,2; 60,2];
f = [1, 2, 3, 4, 1];
sqHandle = patch(app.UIAxes,'vertices', v, 'faces', f, ...
'FaceColor', 'k', 'LineStyle', 'none');
txtHandle = text(app.UIAxes, 80, 0, 'test text','Fontsize',12,'VerticalAlignment', 'bottom');
hold(app.PlotLegend,'on');
pbarOriginal = pbaspect(app.UIAxes);
I = imread('myImage.jpg');
I = flip(I);
imshow(I,'Parent',app.UIAxes,'XData',[90,95],'YData',[0,2]);
app.UIAxes.YDir = 'normal';
app.UIAxes.PlotBoxAspectRatioMode = 'manual';
app.UIAxes.DataAspectRatioMode = 'auto';
app.UIAxes.PlotBoxAspectRatio = pbarOriginal;
end
Best Answer