How can I keep figure window maximized when showing an image?

72 views (last 30 days)
I am building a programmatic GUI for an experiment where a test participant will view a series of images, and after each image, respond with a rating for the image.
I want to keep the window maximized, so desktop clutter etc. will not disturb the visual impression. An image will be displayed for a few seconds, then removed, and some sliders will appear for the rating. Next, the sliders will be hidden, and a new image will appear, etc.
What happens is, it starts out fine with the maximized figure window, right until I load an image and display it, using imshow or image command - then the figure window resizes to fit the image. I could maximize it back, but that causes some disturbing motion of the window frame.
How can I keep the window maximized, and display an image at 1:1 ratio (not scaled to fit a maximized window), so one pixel in the image fits no more/less than one "pixel" on screen?
Below is an example of what I have now (using Matlab R2013a):
screenSize = get(0,'screensize');
screenWidth = screenSize(3);
screenHeight = screenSize(4);
% create figure window, keeping it invisible while adding UI controls, etc.
hFig = figure('Name','APP',...
'Numbertitle','off',...
'Position', [0 0 screenWidth screenHeight],...
'WindowStyle','modal',...
'Color',[0.5 0.5 0.5],...
'Toolbar','none',...
'Visible','off');
% make the figure window visible
set(hFig,'Visible','on');
% maximize the figure window, using WindowAPI
WindowAPI(hFig, 'Position', 'work');
% pause (in the full version of this script, this would instead be
% a part where some UI elements are shown and later hidden...
pause(1.0);
% creating a handle for imshow, hiding it for now
img = imread('musik.png');
hImshow = imshow(img);
set(hImshow,'Visible','off');
% Show the image
% This is where Matlab decides to modify the figure window,
% so it fits the image rather than staying maximized.
set(hImshow,'Visible','on');
% How can I display an image in an existing-and-maximized figure
% window, so that the window remains maximized, and the image
% is displayed at actual size (not scaled), so one pixel in the
% image is displayed by one "pixel" on the monitor?
Best regards, Christian

Accepted Answer

C
C on 15 Nov 2013
Edited: C on 15 Nov 2013
A kind user over at stackoverflow.com helped solve this issue:
In short, here's the solution:
hFig = figure('Name','APP',...
'Numbertitle','off',...
'Position', [0 0 screenWidth screenHeight],...
'WindowStyle','modal',...
'Color',[0.5 0.5 0.5],...
'Toolbar','none');
img = imread('someImage.png');
fpos = get(hFig,'Position')
axOffset = (fpos(3:4)-[size(img,2) size(img,1)])/2;
ha = axes('Parent',hFig,'Units','pixels',...
'Position',[axOffset size(img,2) size(img,1)]);
hImshow = imshow(img,'Parent',ha);
By first creating the figure window, then axes of proper size and position, and then displaying the image with imshow, using the axes as parent, the window resizing is avoided.
Cheers, Christian

More Answers (2)

Image Analyst
Image Analyst on 14 Nov 2013
Edited: Image Analyst on 3 Aug 2022
Issue this command right after you call imshow():
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
You can now (after R2014b I believe) do it this way instead:
g = gcf;
g.WindowState = 'maximized';

C
C on 14 Nov 2013
Thank you for the quick response.
I tried the suggestion, but it causes a "flicker" of the window (it is noticeable that the window is being resized quickly), and the displayed image is scaled to fill out the window.
I was hoping for flicker-free and not-scaled...
Best regards, Christian

Categories

Find more on Environment and Settings 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!