set(gcf,'Position') not working?
Mostra commenti meno recenti
Matlab does not seem to be letting me set my figure position in some cases, for no apparent reason. Code:
scrsz = get(0,'ScreenSize');
figure('Visible','off','color',[1,1,1]); %background to white
set(gcf,'MenuBar','none');
set(gcf,'NumberTitle','off');
set(gcf,'Position',scrsz + [0 0 0 50]);
axis([-scrsz(3),scrsz(3),-scrsz(4),scrsz(4)+200]); %Changes the data pixel size of the workspace.
set(gca, 'Position', get(gca, 'OuterPosition')); %removes annoying outer white boundary box
set(gca,'xcolor',get(gcf,'color')); %these next four lines make the actual axis lines and their tick marks be white and thus invisible.
set(gca,'ycolor',get(gcf,'color'));
set(gca,'ytick',[]);
set(gca,'xtick',[]);
set(gcf,'Visible','on');
In line #5 there, I have the "+ [0 0 0 50]" which should nudge the entire figure to be 50 pixels taller than before (thus getting the menu bar off the screen so I have a pure white display).
But it does nothing. Even when I isolate just that by typing out get(gcf,'Position') set(gcf,'Position',[1 1 1920 1107]) get(gcf,'Position')
It will display [1 1 1920 1057] and then again [1 1 1920 1057] So it is just straight up ignoring my instructions.
If I add +[0 50 0 0] instead, though, it is perfectly happy to push the menu bar off the screen (though it leaves a non-figure area below). But it refuses to do it with stretching?
I don't get it.
Risposte (7)
Andre
il 3 Apr 2015
I had the same problem. The figure flashed at the position I wanted it then moved back to the original place. It stopped doing that when I set Resize to off. Just set:
set(gcf,'Resize','off')
Before redefining position. Hopefully it works for you too.
Michael Thursby
il 2 Giu 2022
2 voti
I just updated to 2022a and now my code does the same thing. I think we need matlab staff to weigh in on this real problem. I depend on being able to position my windows as needed not as matlab likes. I organize eight windows on a second screen for visibility and now it won’t do it !!!
4 Commenti
Brian Derstine
il 23 Feb 2023
Modificato: Brian Derstine
il 23 Feb 2023
still happening in 2022b... so annoying.
Three monitor setup on Mac OS 13.1. Main (laptop) monitor on far right, two larger monitors (extended) on the left.
>> get(0,'MonitorPositions')
ans =
1 1 1440 900
-3839 1 1920 1080
-1919 1 1920 1080
>> set(0, 'DefaultFigurePosition', [-1920 1 1920 1004])
>> f=figure
f =
Figure (3) with properties:
Number: 3
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [-1920 1 1920 1004]
Units: 'pixels'
Show all properties
>> f.Position
ans =
1 61 1440 736
Change the main monitor to the far left monitor and all the figure windows automagically show up on my far right (laptop) monitor.
>> get(0,'MonitorPositions')
ans =
1 1 1920 1080
3841 1 1440 900
1921 1 1920 1080
>> set(0, 'DefaultFigurePosition', [1920 1 1920 1004])
>> f=figure
f =
Figure (3) with properties:
Number: 3
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [1920 1 1920 1004]
Units: 'pixels'
Show all properties
>> f.Position
ans =
3841 1 1440 796
Notice how regardless of which screen is the main screen, the figure is always shown on the far right screen. Something somewhere is resetting the leftmost Position coordinate of the figure... which is quite irritating.
I also tried the above after running set(gcf,'Resize','off')which has no apparent effect, the figure window is still shown on the far right monitor. Any other coordinate change will be allowed, for example:
% small positive coordinates are fine
>> set(0, 'DefaultFigurePosition', [100 100 100 100])
>> f=figure
f =
Figure (5) with properties:
Number: 5
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [100 100 100 100]
Units: 'pixels'
Show all properties
>> f.Position
ans =
100 100 100 100
% negative left coordinate is ignored:
>> set(0, 'DefaultFigurePosition', [-100 100 100 100])
>> f=figure
f =
Figure (6) with properties:
Number: 6
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [-100 100 100 100]
Units: 'pixels'
Show all properties
>> f.Position
ans =
1 100 100 100
Brian Derstine
il 24 Feb 2023
There's definitely something weird going on behind the scenes. Watch the Position values change seemingly at random:
>> get(0,'MonitorPositions')
ans =
1 1 1440 900
-3839 1 1920 1080
-1919 1 1920 1080
% set the default
>> set(0, 'DefaultFigurePosition', [-1920 120 1440 796])
% it SAYS its at the default position
>> f=figure
f =
Figure (2) with properties:
Number: 2
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [-1920 120 1440 796]
Units: 'pixels'
Show all properties
% in reality its on the right-most screen
>> f.Position
ans =
1 61 1440 736
% try to move it
>> set(f,'Position',get(0, 'DefaultFigurePosition'))
% its on the correct (middle) screen but in the wrong place
>> f.Position
ans =
-1399 61 1440 736
% try to move it again
>> set(f,'Position',get(0, 'DefaultFigurePosition'))
>> f.Position
ans =
-1920 120 1440 796
% NOW its in the requested position...
But why do I have to tell it three times, is it a 5 year old?
I'm hitting the same problem with 2022a. I just want to make a lot graphs that are the same size so I can print them to identically sized .png files. The figures are not anywhere near an edge, but MATLAB tweaks the height and width several pixels anyway. My workaround is to issue all the plotting commands, then drawnow, then set the figure position.
MathWorks, please fix this!
Massimo Ciacci
il 25 Mar 2023
The best approach to date is to restart Matlab and give up using anything else than get(0,'MonitorPositions'). This is because when you apply set(gcf,'Position') it's what Matlab thinks the screen size is that places the figure.
Even if you manage using undocumented code to get the true screen size, you will be fiddling for hours to no avail, unless there is some undocumented Matlab to set the position.
I actually see this happening to all my colleagues as well, since several years, as soon as they attach a second monitor, I tell them they need to restart Matlab. No other solution to this unfortunatley.
Jan
il 3 Giu 2013
1 voto
Yes, Matlab sets the figure position stubbornly when it exceeds the visible area of the primary screen at least sind version 5.3. Sometimes setting the position twice helps (!).
Because I needed full-screen windows, I've created a Mex function for resizing the window without calling Matlab's internal resize function, see FEX: WindowAPI. Even from inside the Mex, the Windows API function SetWindowPos() requires the SWP_NOSENDCHANGING flag to avoid Matlab's smart independent decision to move the figure where it wants the user to want it.
A similar effect happens for the minimal horizontal size of a figure.
Please send an enhancement request to TMW and ask them not to set the figure position magically, because this has no benefit.
1 Commento
Brian Derstine
il 23 Feb 2023
this is awful.
Dennis Dunn
il 16 Nov 2017
Modificato: Dennis Dunn
il 16 Nov 2017
I'm having the same problem using Matlab R2017a on Ubuntu 17.04, where calling for example:
figure();
set(gcf, 'Position', [0 0 800 400])
get(gcf, 'Position')
returns [31 11 800 400] or something similar. I tried using
set(gcf,'Resize','off')
but it's still auto-resizing afterward, but only sometimes randomly. I think it's because the new figure is being placed slightly off-screen, where each successive call places new figures slightly lower than the predecessor (I was generating animation frames). I solved the problem by reusing my figures to avoid creating new figures whenever possible. This solved it. Note that this problem only happens on Ubuntu, not on Windows or MacOS.
Sean de Wolski
il 30 Mar 2012
0 voti
Sounds like the axes' property DataAspectRatio is superseding the set of your position.
Myhan PARK
il 3 Giu 2013
0 voti
I had the same problem (version R2013a). Dual monitor size was returned correctly, but resizing my figures did not work correctly. It seemed that this was caused because the second monitor was connected while Matlab was already open. Restarting Matlab solved the problem ...
Steven Lord
il 25 Mar 2023
0 voti
This was not an option when the original question was asked, but if you want the figure to cover the entire screen change its WindowState property to 'fullscreen' rather than trying to manipulate the Position property. You may also need or want to set the MenuBar and/or ToolBar properties to 'none' as well.
Categorie
Scopri di più su Graphics Performance in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!