Search code examples
matlabuser-interfacematlab-guidematlab-gui

How to move a window created using MATLAB app designer to the center of screen?


How can a window created by the MATLAB app designer be moved to the center of the screen?

Currently, I'm using app.my_fig_main.Position to update the location. However, this function can only set the following attributes [left bottom width height].

When running the app on a screen with a different resolution, I should have some sort of movegui function that sets its location to center.

Unfortunately, movegui doesn't work in MATLAB's app designer environment.

Is there any way to do this in app designer?


Solution

  • Not sure if I misunderstood your question, but you can obtain the current resolution using the figposition function. e.g. on my laptop:

    >> figposition([0, 0, 100, 100])
    ans =
      0   0   1366  768
    

    indicating a resolution of 1366x768

    You can then set(gcf,'position', ... ) to the position you want, such that it's central.

    You could even use figposition directly in there, in fact, to set the figure's position using percentages directly.


    ** EDIT: ** an example, as per request:

    % Create Figure Window (e.g. by app designer; it's still a normal figure)
      MyGuiWindow = figure('name', 'My Gui Figure Window');
    
    % Desired Window width and height
      GuiWidth = 500;
      GuiHeight = 500;
    
    % Find Screen Resolution
      temp = figposition([0,0,100,100]);
      ScreenWidth = temp(3);
      ScreenHeight = temp(4);
    
    % Position window in center of screen, and set the desired width and height
      set (MyGuiWindow, 'position', [ScreenWidth/2 - GuiWidth/2, ScreenHeight/2 - GuiHeight/2, GuiWidth, GuiHeight]);