在多屏幕配置下确定MATLAB的监视器

7
我经常在不同公司的工作场所之间移动。每天我可能只有笔记本电脑,也可能有多达四个显示器。使用多个显示器时,我不知道要选择哪个显示器用于MATLAB主GUI(即双击matlab.exe时启动的主GUI)。这取决于可用显示器的分辨率。
我使用编程生成的GUI脚本(而不是GUIDE),似乎MATLAB总是在第一个显示器上弹出它们。我进行了一些研究,并发现可以通过使用p = get(gcf,'Position')set(0,'DefaultFigurePosition',p)movegui命令将GUI定位到所选的显示器,但前提是我预先知道要使用哪个显示器。
有没有办法找出主MATLAB GUI所在的显示器,让其他小GUI出现在同一显示器上?
2个回答

6
我们可以使用一些Java技巧来获取当前监视器;请参阅下面带有注释的代码。
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

一些建议:

  • 根属性文档
  • "0" 的输出值意味着有问题。
  • 可能有更简单的方法来获取 "RootPane";我使用了一种我有丰富经验的方法。
  • 如果您的 MATLAB 窗口跨越多个监视器,则只会识别其中一个监视器。如果需要此功能,可以使用 com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.getComponent.getRootPane.getWidth 等来查找 MATLAB 窗口的其他角落,并对它们进行相同的测试。
  • 在找到第一个有效监视器后,我没有打断循环,因为假设:1) 只有一个监视器是有效的。 2) 循环将处理的监视器总数较少。
  • 对于勇敢的人,可以使用多边形进行检查(即 inpolygon)。

更新后的链接:https://www.mathworks.com/help/matlab/ref/matlab.ui.root-properties.html - jacobsee

1

感谢Dev-iL,它几乎完美地工作了,我添加了一些边距来“捕捉”窗口,当它略微偏离屏幕时,或者像我的经验一样,只是最大化。 发布我的编辑:

   function mon = getMatlabMainScreen()
%% 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
    marginLimit = 100;
    margin =0;
    while ~mon
        for ind1 = 1:nMons
            mon = mon + ind1*(...
                matlabScreenPos(1) + margin >= monitors(ind1,1) && matlabScreenPos(1) < sum(monitors(ind1,[1 3])) + margin && ...
                matlabScreenPos(2) + margin >= monitors(ind1,2) && matlabScreenPos(2) < sum(monitors(ind1,[2 4])) + margin );
        end
        margin = margin + 1;
        if margin > marginLimit
            break;
        end
    end
end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接