MATLAB: MatLab returns false figure size in script

figureheightMATLABposition;size;uifigurewidthwindowsize

Hello,
I'm using the following script. If I run each line individually, it gives the right Position so I can get the Width and Height of the figure. But when I use it in a script it keeps giving me the position from before maximizing it.
When asking for the Position after running the script, it gives the right Position too.
is there any way I can resolve that problem?
%% Creating GUI
GUI=uifigure('Name','Spectrograms',...
'NumberTitle','off',...
'WindowState','maximized');
% get window size
Position=get(GUI, 'Position');
GUI_Width=Position(3);
GUI_Height=Position(4);

Best Answer

Add a call to drawnow after creating the figure
%% Creating GUI
GUI=uifigure('Name','Spectrograms',...
'NumberTitle','off',...
'WindowState','maximized');
% Update figures and process callbacks
drawnow;
% get window size
Position=get(GUI, 'Position');
GUI_Width=Position(3);
GUI_Height=Position(4);