I move around from a company site to another a lot. At any given day, I might have just my laptop or as many as four monitors. With multiple monitors, I don't know which monitor I will choose to use for MATLAB main GUI (the main GUI launched when double-clicking matlab.exe). It depends on the resolutions of the available monitors.
I use scripts that utilize programmatically generated GUIs (not by GUIDE) and it seems that MATLAB pops them up always on the first monitor. I've researched a little bit and found to locate the GUIs to a monitor of choice by using p = get(gcf, 'Position')
, set(0, 'DefaultFigurePosition', p)
, and movegui
command, but this will only work if I know beforehand which monitor I want to use.
Is there a way to find out on which monitor the main MATLAB GUI is up and have other little GUIs pop up on the same monitor?
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
Few notes:
com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.getComponent.getRootPane.getWidth
etc. to find the other corners of the MATLAB window and do the same test with them.inpolygon
).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With