Get screen DPI inside Matlab (2017b).

11 ビュー (過去 30 日間)
Mitchell van Zuijlen
Mitchell van Zuijlen 2017 年 11 月 16 日
編集済み: Robert U 2019 年 12 月 3 日
I'm trying to get the exact size of my monitor in inches or centimeters. Regretfully, using the conventional method doesn't work. I assume this is because of DPI scaling issues.
I run this code:
>> set(0,'units','centimeters');
>> screensize = get( groot, 'Screensize' )
screensize =
0 0 50.8000 28.5750
Unless I'm mistaken, this claims by screen is 50x28 centimeters. In reality, my screen is 29.4x16.6 centimeters.
As such, I wish to convert my screen size in pixels to cm, but for this I need my screens DPI. I currently use the following code to get the correct screen size in pixels.
>> ge = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment;
>> gd = ge.getDefaultScreenDevice;
>> screensize = [gd.getDisplayMode.getWidth gd.getDisplayMode.getHeight]
>> screensize
screensize =
3840 2160
Now I just need to get my DPI.
n.b. I actually know my DPI, but I want to retrieve it via Matlab.

回答 (1 件)

Robert U
Robert U 2017 年 11 月 16 日
編集済み: Robert U 2017 年 11 月 16 日
Hello Mitchell van Zuijlen:
Since "groot" contains DPI information my first try would be to call
myDPI = get(groot,'ScreenPixelsPerInch');
Check with
ScreenSize = get(groot,'Screensize');
cmScreenSize = ScreenSize ./ myDPI * 2.54
Kind regards,
Robert
  2 件のコメント
adams13
adams13 2019 年 11 月 29 日
Nope! "get(groot,'ScreenPixelsPerInch')" is wrong.
>> myDPI = get(groot,'ScreenPixelsPerInch')
myDPI =
96
However, my screen(s) are 3840 x 2160, with windows setting of 150% that is 144dpi. Original way with java to get the resolution works correctly. "ge.getXResolution" and "ge.getYResolution" give also 144.
Matlab R2019b Update 1 under Windows 7.
Robert U
Robert U 2019 年 12 月 3 日
編集済み: Robert U 2019 年 12 月 3 日
Hi adams13,
There is a hint in the documentation that on high-res systems resolution value might be incorrect: https://de.mathworks.com/help/matlab/creating_guis/dpi-aware-behavior-in-matlab.html
The ScreenSize property of the root object might not match the display size reported by high-DPI Windows systems. Specifically, the values do not match when the Units property of the root object is set to 'pixels'. MATLAB reports the value of the ScreenSize property based on device-independent pixels, not the size of the actual pixels on the screen.
If I have a look in the root properties documentation I find a constant value of 96 DPI set for Windows systems. I guess the command does not check system properties.
The described solution produces results that do not even fit with my non-high-res Laptop screen (even using java-methods I cannot reproduce my screen size in cm). Using Matlab 2016b.
Kind regards,
Robert

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB Mobile Fundamentals についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by