Get screen DPI inside Matlab (2017b).

조회 수: 21 (최근 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 CenterFile Exchange에서 MATLAB Mobile Fundamentals에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by