matlab - 在多监视器配置中确定 MATLAB 的监视器

标签 matlab position matlab-figure multiple-monitors matlab-gui

我经常从一个公司站点转到另一个站点。在任何一天,我可能只有笔记本电脑或多达四台显示器。对于多个监视器,我不知道我会选择哪个监视器用于 MATLAB 主 GUI(双击 matlab.exe 时启动的主 GUI)。这取决于可用显示器的分辨率。

我使用的脚本利用以编程方式生成的 GUI(不是通过 GUIDE),而且 MATLAB 似乎总是在第一台显示器上弹出它们。我进行了一些研究,发现可以使用 p = get(gcf, 'Position'), set(0, 'DefaultFigurePosition', p 将 GUI 定位到所选监视器)movegui 命令,但这只有在我事先知道我想使用哪个监视器时才有效。

有没有办法找出主 MATLAB GUI 在哪个监视器上显示,并在同一监视器上弹出其他小 GUI?

最佳答案

我们可以使用一些 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

一些注意事项:

  • Root properties documentation .
  • 输出值为“0”表示出现问题。
  • 获取“RootPane”可能有更简单的方法;我使用了一种我有很好经验的方法。
  • 如果您的 MATLAB 窗口跨越多个监视器,这将只识别其中一个监视器。如果需要此功能,您可以使用 com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.getComponent.getRootPane.getWidth 等找到 MATLAB 窗口的其他角并用它们进行相同的测试.
  • 在找到第一个有效监视器后,我没有费心打破循环,因为假设:1) 只有一个监视器是有效的。 2) 循环必须处理的监视器总量很小。
  • 勇敢者可以使用多边形进行检查(即 inpolygon )。

关于matlab - 在多监视器配置中确定 MATLAB 的监视器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37705169/

相关文章:

matlab - MATLAB 图像顶部和底部的标题

css - 滚动时使具有相对位置的 Div 位于页脚 div 下方

jquery - 追加后无法更改 .CSS 位置

matlab - 如何在不显示的情况下编辑保存在 .fig 文件中的图形的属性

excel - 相同的公式,但在 Matlab 和 Excel 中的结果不同

css - 将 DIV 置于绝对 DIVS 下方(页脚)

matlab - 当有多个轴时,如何从一个 y 轴上删除值?

windows - 使用 mingw-w64 编译 mex 的链接器错误

matlab - 并行与非并行matlab的脚本——parfor的使用

matlab - 奇怪的单元阵列行为