Why does imshow() behave differently depending on timing?

I have this code, where I create 2 UIFigures, and use imshow to display an image in them. But, depending on whether I wait a bit before calling imshow, the figure properties are different.
im = imread('peppers.png');
uif1 = uifigure;
uiax1 = axes(uif1);
pause(5) % This is the only difference
imshow(im, 'Parent', uiax1)
uif2 = uifigure;
uiax2 = axes(uif2);
imshow(im, 'Parent', uiax2)
I wrote this snippet to compare the properties:
% Get properties for both axes
props1 = get(uiax1);
props2 = get(uiax2);
% Compare the value of each property between both axes
for f = fieldnames(props1)'
% Discriminate between char and numeric properties
if isnumeric(props1.(f{1}))
comp = ( props1.(f{1}) == props2.(f{1}) );
elseif ischar(props1.(f{1})) || isstring(props1.(f{1}))
comp = strcmp(props1.(f{1}), props2.(f{1}));
end
% If they are not the same, write the values in the console
if ~all(comp)
fprintf('Variable: %s \n', f{1})
if ischar(props1.(f{1}))
fprintf('Value in ax1: %s \nValue in ax2: %s \n' ,props1.(f{1}), props2.(f{1}))
else
fprintf('Value in ax1: %s \nValue in ax2: %s \n' ,mat2str(props1.(f{1})), mat2str(props2.(f{1})))
end
fprintf('\n')
end
end
This is the output. The CameraViewAngle is different, as well as the position properties.
% Variable: CameraViewAngle
% Value in ax1: 6.86722727382441
% Value in ax2: 9.14779804245545
%
% Variable: Position
% Value in ax1: [0.13 0.11 0.775 0.815]
% Value in ax2: [0.125510200992791 0.134870716465011 0.746355685131195 0.805031446540881]
%
% Variable: OuterPosition
% Value in ax1: [0 0 1 1]
% Value in ax2: [0.000315053809494203 0.0262161653981438 0.963039593717671 0.98776864606243]
%
% Variable: ActivePositionProperty
% Value in ax1: outerposition
% Value in ax2: position
%
% Variable: TightInset
% Value in ax1: [0.0391448012890748 0.0555975153927699 0 0]
% Value in ax2: [0.048765791400876 0.0554577329623624 0 0]
The plots look almost the same but you can tell they are not identical:
Now the interesting part, if I change the Position property right after calling imshow, now the paused image does not get the Position set correctly. In fact, nothing changes for it. The other one gets it right and fills the figure.
im = imread('peppers.png');
uif1 = uifigure;
uiax1 = axes(uif1);
pause(5)
imshow(im, 'Parent', uiax1)
uiax1.Position = [0 0 1 1];
uif2 = uifigure;
uiax2 = axes(uif2);
imshow(im, 'Parent', uiax2)
uiax2.Position = [0 0 1 1];
%% Properties
% Variable: Position
% Value in ax1: [0.13 0.11 0.775 0.815]
% Value in ax2: [0 0 1 1]
%
% Variable: OuterPosition
% Value in ax1: [0 0 1 1]
% Value in ax2: [-0.167741935483871 -0.134969325153374 1.29032258064516 1.22699386503067]
%
% Variable: ActivePositionProperty
% Value in ax1: outerposition
% Value in ax2: position
%
% Variable: TightInset
% Value in ax1: [0.0391448012890748 0.0555975153927699 0 0]
% Value in ax2: [0.0141302230428502 0.0580573341881453 0 0]
I discovered that actually, right after using imshow both axes get ActivePositionProperty set to 'position', but the axes that was paused reverts to 'outerposition' shortly after. This code shows it.
im = imread('peppers.png');
uif1 = uifigure;
uiax1 = axes(uif1);
uiax1.ActivePositionProperty
pause(5)
uiax1.ActivePositionProperty
imshow(im, 'Parent', uiax1)
uiax1.ActivePositionProperty
pause(5)
uiax1.ActivePositionProperty
Maybe that is the reason setting the Position right after imshow does not seem to work (because it gets reset afterwards anyway).
I am sure there are a bunch of unpredicted behaviors derived out of this. And I suspect some problems I have faced while designing a GUI have this kind of weird axes layout behavior at the root.
Can someone reproduce this and propose an explanation/fix?

2 Comments

Which version of matlab (please fill in the required field on the right of the page below the tags) and which OS?
On my machine, R2019a (version 9.6.0.1072779), Windows, you script doesn't find any difference between the two figures.
Sorry Guillaume, I thought I had chosen the version. Here is the complete version info, I am at R2018b, and I can reproduce the described behaviour with a freshly opened instance of Matlab
>> ver
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.5.0.944444 (R2018b)
MATLAB License Number: 920805
Operating System: Microsoft Windows 10 Home Version 10.0 (Build 17763)
Java Version: Java 1.8.0_152-b16 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
MATLAB Version 9.5 (R2018b)
Simulink Version 9.2 (R2018b)
Bioinformatics Toolbox Version 4.11 (R2018b)
Computer Vision System Toolbox Version 8.2 (R2018b)
Control System Toolbox Version 10.5 (R2018b)
Curve Fitting Toolbox Version 3.5.8 (R2018b)
DSP System Toolbox Version 9.7 (R2018b)
Data Acquisition Toolbox Version 3.14 (R2018b)
Image Processing Toolbox Version 10.3 (R2018b)
Instrument Control Toolbox Version 3.14 (R2018b)
Optimization Toolbox Version 8.2 (R2018b)
Parallel Computing Toolbox Version 6.13 (R2018b)
Signal Processing Toolbox Version 8.1 (R2018b)
SimBiology Version 5.8.1 (R2018b)
Simulink Control Design Version 5.2 (R2018b)
Statistics and Machine Learning Toolbox Version 11.4 (R2018b)
Symbolic Math Toolbox Version 8.2 (R2018b)
I also happen to have R2019a installed (I started this project in R2018b and so I haven't changed). I tried the same thing in R2019a and indeed, the behavior is not reproducible in that version. Here's the version info for that one
>> ver
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.6.0.1072779 (R2019a)
MATLAB License Number: 920805
Operating System: Microsoft Windows 10 Home Version 10.0 (Build 17763)
Java Version: Java 1.8.0_181-b13 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
MATLAB Version 9.6 (R2019a)
Simulink Version 9.3 (R2019a)
Control System Toolbox Version 10.6 (R2019a)
DSP System Toolbox Version 9.8 (R2019a)
Data Acquisition Toolbox Version 4.0 (R2019a)
Image Processing Toolbox Version 10.4 (R2019a)
Instrument Control Toolbox Version 4.0 (R2019a)
Optimization Toolbox Version 8.3 (R2019a)
Parallel Computing Toolbox Version 7.0 (R2019a)
Signal Processing Toolbox Version 8.2 (R2019a)
Simulink Control Design Version 5.3 (R2019a)
Statistics and Machine Learning Toolbox Version 11.5 (R2019a)
Symbolic Math Toolbox Version 8.3 (R2019a)

Sign in to comment.

Answers (0)

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!