Defining a MATLAB Monitor in a Multi-Monitor Configuration

I often move from a company website to another. Any day, I can only have my laptop or as many as four monitors. With multiple monitors, I don’t know which monitor I will use for the MATLAB main GUI (the main GUI starts when you double-click matlab.exe). It depends on the resolutions of the available monitors.

I use scripts that use software-generated graphical interfaces (not GUIDE), and it seems that MATLAB always pops up on the first monitor. I did a bit of work and found to find graphical interfaces for selecting a monitor using the commands p = get(gcf, 'Position') , set(0, 'DefaultFigurePosition', p) and movegui , but this will only work if I know in advance which monitor I want to use.

Is there any way to find out on which monitor the main GUI of MATLAB is located, and other small graphical interfaces appear on the same monitor?

+5
source share
1 answer

We can use some Java tricks to get the current monitor; see code with comments below:

 function mon = q37705169 %% Get monitor list: monitors = get(groot,'MonitorPositions'); % also get(0,'MonitorPositions'); %% Get the position of the main MATLAB screen: pt = com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.getComponent.getRootPane.getLocationOnScreen; matlabScreenPos = [pt.x pt.y]+1; % "+1" is to shift origin for "pixel" units. %% Find the screen in which matlabScreenPos falls: mon = 0; nMons = size(monitors,1); if nMons == 1 mon = 1; else for ind1 = 1:nMons mon = mon + ind1*(... matlabScreenPos(1) >= monitors(ind1,1) && matlabScreenPos(1) < sum(monitors(ind1,[1 3])) && ... matlabScreenPos(2) >= monitors(ind1,2) && matlabScreenPos(2) < sum(monitors(ind1,[2 4])) ); end end 

A few notes:

  • Documentation of root properties .
  • The output value of "0" means that something is wrong.
  • Perhaps there is an easier way to get "RootPane"; I used a method with which I have good experience.
  • Only one of the monitors will recognize this if your MATLAB window spans multiple monitors. If this functionality is needed, you can use com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.getComponent.getRootPane.getWidth etc. to find other corners of the MATLAB window and run the same tests with them.
  • I did not bother to break out of the loop after the first valid monitor was found, as it is assumed that: 1) Only one monitor is valid. 2) The total number of monitors that the cycle should process is small.
  • For the brave, you can test using polygons (i.e., inpolygon ).
+4
source

All Articles