Getting screen resolution on Windows 11

Are there other functions I can use to get the monitor resolution on Windows 11? I use this code:
set(0, 'units', 'pixels'); % Sets the units of your root object (screen) to pixels
Pix_SS = get(0, 'screensize'); % Obtains the pixel information
And all I get is 1920 x 1080? This Dell U4320Q is 3840 x 2160 as shown in this Settings > Display resolution:

4 个评论

I found this code which works very well, for those who didn't find it :
ScreenPixelsPerInch = java.awt.Toolkit.getDefaultToolkit().getScreenResolution()
ScreenDevices = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
MainScreen = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getScreen()+1;
MainBounds = ScreenDevices(MainScreen).getDefaultConfiguration().getBounds();
MonitorPositions = zeros(numel(ScreenDevices),4);
First of all, I'm susprised to find native support for Java in Matlab, which opens up all kinds of interesting possibilities.
Second of all, even if I get the true physiical screen resolution from Java, it still does not solve my problem? Which is, how I force a figure or an axis to a "real" physical size? If I try to set a figure to be 800x600, for example :
set(FigureH, 'color', 'w', 'MenuBar', 'none', 'Position', [500 200 800 600]);
In actuality, the figure size is "twice" that much or 1600x1200.
May not seem like a problem to you but my algorithms are based on the 'sizes' I have access to from regular Matlab code, such as "MouseMove Fcn" callback :
function mouseMove (source, eventdata)
ax1 = gca; % ancestor(source, 'axes');
% CurrentPoint is relative to ax.InnerPosition
CP = round(get (ax1, 'CurrentPoint'));
x = CP(1,1);
y = CP(1,2);
There is no way I could "trust" the CurrentPoint vector information if the code is ran on a 4K monitor?
Is there a way I can call for all "pixel units" to be on 1920 x 1080 scale?
Even if the interface ends up looking "tiny" on a 4K monitor?
Sorry for these kinds of "mundane" questions which are not very scientific-related...
I just remember tampering with the system "scaling" setting :
I set the scale back to 100% and Matlab's figure size became exactly what I called for, 800x600.
Sadly, straight 4K resolution is unusable for my eyes on this monitor. So I went back to 200% scale.
I guess the question becomes, is there a way, on Windows 11, to find what "scaling" is the going system value?
From what I recall (from discussions when display scaling was new in windows), this parameter is poorly communicated to programs.
But it everything is off by the same factor, is there a problem for your application?
An additional note: Java will be removed from Matlab somewhere in a future release. It is also generally undocumented, meaning it may change release to release without warnings in the release notes.
@Rik: While MATLAB will no longer include Java by default, it will still be usable from within MATLAB, but the user will be responsible for providing their own Java. Using Java within MATLAB is fully documented. For example, this documentation page discusses use of your own Java classes within MATLAB: Call Java from MATLAB

请先登录,再进行评论。

回答(1 个)

Note that this is undocumented and not guaranteed to work indefinitely, but there is a property on all figures called ScreenPixelsPerInch that is similar to the property of the same name on groot, but while the value on groot takes into account the display scaling, the value on Figure does not. You can use this to get a conversion factor for the ScreenSize or MonitorPositions properties on groot.
For example:
f = figure;
scaledDPI = groot().ScreenPixelsPerInch % On my machine this is 96
unscaledDPI = f.ScreenPixelsPerInch % On my machine this is 144 because I have 150% scaling.
scaleFactor = unscaledDPI./scaledDPI % On my machine this is 1.5 because I have 150% scaling.
scaledMonitorPositions = groot().MonitorPositions % On my machine this is [1 1 2560 1440; -2559 -5 2560 1440]
% When doing the conversion, the first two values (left and bottom) are
% 1-based, so you need to subtract 1, do the scaling, then add 1.
unscaledMonitorPositions = (scaledMonitorPositions-[1 1 0 0]).*scaleFactor+[1 1 0 0]
On my machine (which runs Windows with 150% display scaling), the Figure's ScreenPixelsPerInch reports 144, while groot reports 96, which reflects the 150% scaling. Using the code above, I get the correct actual resolution of my two monitors (3840 x 2160).

类别

帮助中心File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

产品

版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by