MATLAB: App designer does not scale figure properly when WindowState is set in startupFcn

MATLAB

An app has a startup function which maximizes the window from the start but even if the resize option is selected, the components of the app are not properly scaled up and down.
Reproduction steps:
1. Create new app in App Designer
2. Add a scalable component like an axes
3. Add a startupFcn callback function
4. In the callback, set the WinddowState of the uifigure to be 'maximized':
app.UIFigure.WindowState = 'maximized';
5. Run the app and resize the window — the axes will not resize
6. Comment out/remove the window state line of code and retry — the axes will properly resize up and down

Best Answer

Root cause:
When WindowState is set to 'max', a Position changed event is sent to the client. However, the client code that is listening to those events is not yet created, so that event is lost.
Solution:
Add a "drawnow" right before setting WindowState to max