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.
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
If you feel more as a Microsoft family member you might want to use .NET commands. Try
or
and you get something like
Greetings,
Thomas
@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.
As for me the solution using
java.awt.Toolkit.getDefaultToolkit.getScreenSize
does not work correctly when using more than one monitor.Better use:
Best regards
Matthias
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
but it doesn’t tell me whether the second screen exists or not (if not, it will effectively hide the figure).
Best regards
Hi,
maybe this can be helpful:
then you can play with ‘pixels’ positions
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 exampleHowever, 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.