- Undocumented Matlab - https://undocumentedmatlab.com -

Working with non-standard DPI displays

Posted By Yair Altman On November 9, 2016 | 6 Comments

With high-density displays becoming increasingly popular, some users set their display’s DPI to a higher-than-standard (i.e., >100%) value, in order to compensate for the increased pixel density to achieve readable interfaces. This OS setting tells the running applications that there are fewer visible screen pixels, and these are spread over a larger number of physical pixels. This works well for most cases (at least on recent OSes, it was a bit buggy in non-recet ones). Unfortunately, in some cases we might actually want to know the screen size in physical, rather than logical, pixels. Apparently, Matlab root’s ScreenSize property only reports the logical (scaled) pixel size, not the physical (unscaled) one:

>> get(0,'ScreenSize')   % with 100% DPI (unscaled standard)
ans =
        1       1      1366       768
>> get(0,'ScreenSize')   % with 125% DPI (scaled)
ans =
        1       1      1092.8     614.4

The same phenomenon also affects other related properties, for example MonitorPositions.
Raimund Schlüßler, a reader on this blog, was kind enough to point me to this problem and its workaround, which I thought worthy to share here: To get the physical screen-size, use the following builtin Java command:

>> jScreenSize = java.awt.Toolkit.getDefaultToolkit.getScreenSize
jScreenSize =
java.awt.Dimension[width=1366,height=768]
>> width = jScreenSize.getWidth
width =
        1366
>> height = jScreenSize.getHeight
height =
        768

Also see the related recent article on an issue with the DPI-aware feature starting with R2015b [1].

Upcoming travels – London/Belfast, Zürich & Geneva

I will shortly be traveling to consult some clients in Belfast (via London), Zürich and Geneva. If you are in the area and wish to meet me to discuss how I could bring value to your work, then please email me (altmany at gmail):

  • Belfast: Nov 28 – Dec 1 (flying via London)
  • Zürich: Dec 11-12
  • Geneva: Dec 13-15

Categories: Desktop, Figure window, GUI, Java, Low risk of breaking in future versions, Undocumented feature


6 Comments (Open | Close)

6 Comments To "Working with non-standard DPI displays"

#1 Comment By Thomas Pieper On November 17, 2016 @ 14:17

If you feel more as a Microsoft family member you might want to use .NET commands. Try

NET.addAssembly('System.Windows.Forms');
System.Windows.Forms.Screen.PrimaryScreen.Bounds

or

System.Windows.Forms.SystemInformation.PrimaryMonitorSize

and you get something like

ans = 

  System.Drawing.Rectangle
  Package: System.Drawing

  Properties:
    Location: [1x1 System.Drawing.Point]
        Size: [1x1 System.Drawing.Size]
           X: 0
           Y: 0
       Width: 1920
      Height: 1080
        Left: 0
         Top: 0
       Right: 1920
      Bottom: 1080
     IsEmpty: 0
       Empty: [1x1 System.Drawing.Rectangle]

Greetings,

Thomas

#2 Comment By Yair Altman On November 17, 2016 @ 14:47

@Thomas – the problem with your .NET solution is that it only works on Windows; the Java-based solution in my post works on all Matlab platforms, including Mac and Linux.

#3 Comment By Matthias Kirchhoff On November 24, 2016 @ 16:09

As for me the solution using java.awt.Toolkit.getDefaultToolkit.getScreenSize does not work correctly when using more than one monitor.
Better use:

ge = javaObjectEDT(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment());
gs = ge.getScreenDevices();
jmonitorposition = [];
if ~isempty(gs)
  for iRun_ = 1:size(gs,1)
     dm = gs(iRun_).getDisplayMode();
     jmonitorposition(iRun_,:) = [1 1 dm.getWidth dm.getHeight];
  end
end

Best regards
Matthias

#4 Comment By Yaroslav On November 28, 2016 @ 10:59

Hi Yair,

Suppose I have two monitors. How can I tell a figure to appear on the secondary one, if it exists? I can do something like

hFig = figure('Units','normalized','Position',[1.10 0.10 0.80 0.70]);

but it doesn’t tell me whether the second screen exists or not (if not, it will effectively hide the figure).

Best regards

#5 Comment By marco On November 29, 2016 @ 16:12

Hi,
maybe this can be helpful:

>> monitorPos = get(0,'MonitorPositions')
monitorPos =
           1           1        1600         900
        1601           1        3200         900

then you can play with ‘pixels’ positions

#6 Comment By Yaroslav On November 29, 2016 @ 21:28

Hi @Marco,

Thank you for your comment. Unfortunately, your solution tells me only how many monitors I can have. What I need is to know how many monitors I currently have.

To be more precise, MonitorPositions tells me how many monitors I had at the time of Matlab’s startup. It does not change if I attach a monitor after startup. This is the behaviour in my Windows 8.1 laptop.

I can do a little trick to circumvent this problem. By using the PointerLocation, I can check on which monitor the pointer is. For example

% get root handle
hRoot = groot;
% check if (a) there are at least two monitors
%          (b) pointer's x location is larger than 2nd monitor's x0 position
if     size(hRoot.MonitorPositions,1) >= 2 ...
    && hRoot.PointerLocation(1) > hRoot.MonitorPositions(2,1)
    % put figure on the second monitor
else
    % put figure on the first monitor
end

However, as you can see, it assumes you have control over the cursor’s position during execution. I wish there was an option to tell how many active monitors are there.


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/working-with-non-standard-dpi-displays

URLs in this post:

[1] an issue with the DPI-aware feature starting with R2015b: http://undocumentedmatlab.com/blog/graphic-sizing-in-matlab-r2015b

[2] Customizing the standard figure toolbar, menubar : https://undocumentedmatlab.com/articles/customizing-standard-figure-toolbar-menubar

[3] Customizing figure toolbar background : https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background

[4] Faster findjobj : https://undocumentedmatlab.com/articles/faster-findjobj

[5] FindJObj – find a Matlab component's underlying Java object : https://undocumentedmatlab.com/articles/findjobj-find-underlying-java-object

[6] FindJObj GUI – display container hierarchy : https://undocumentedmatlab.com/articles/findjobj-gui-display-container-hierarchy

[7] Blurred Matlab figure window : https://undocumentedmatlab.com/articles/blurred-matlab-figure-window

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.