How to get the screen size where the current uifigure is located?

조회 수: 9 (최근 30일)
Shuai Xu
Shuai Xu 2021년 3월 3일
답변: Ganesh 2024년 5월 30일
I want to capture the current uifigure while running, and I have test the function with this demo code.
there is one issue should to be solved: the Y postion from the demo code is base on the left top corner, but the Y position get from uifigure.positon is based on the left bottom corner. So it need to be converted to suit for the code. So my question is how to get the screen size where the active uifigure is located in the multiple screens?
(get(0,'screensize') can't get the secondary screen size)
other answers which can capture the active uifigure is also welcome!

답변 (1개)

Ganesh
Ganesh 2024년 5월 30일
I understand that you intend to get screen size of the screen where the active "uifigure" is located. As you have pointed out already, the function "get(0,'screensize')" is intended to only get the screensize of the primary screen and not the secondary screens.
As a workaround, you can first find the positions of all the monitors and then use the monitor position and the "uifigure" position to locate which screen the "uifigure" has been plotted. Once you do that, you can retrieve the screensize by indexing the respective screen. You may follow the below steps to achieve the same:
  1. Use get(groot,'MonitorPositions') to get the monitor positions.
  2. Use uifigure.Position to get the position of the uifigure
  3. Use a for-loop to calculate which monitor the figure is being displayed on. A template of the code has been provided below.
for i = 1:size(monitorPositions, 1)
monitorLeft = monitorPositions(i, 1);
monitorBottom = monitorPositions(i, 2);
monitorRight = monitorLeft + monitorPositions(i, 3);
monitorTop = monitorBottom + monitorPositions(i, 4);
figLeft = figPos(1);
figBottom = figPos(2);
% Check if the uifigure is within this monitor's bounds
if figLeft >= monitorLeft && figLeft <= monitorRight && ...
figBottom >= monitorBottom && figBottom <= monitorTop
% Adjust the Y-coordinate for the capture based on the monitor's dimensions
adjustedBottom = monitorTop - (figBottom + figPos(4));
disp(['Figure is on Monitor ', num2str(i), ' with adjusted Y-position: ', num2str(adjustedBottom)]);
break;
end
end
Kindly adjust the code according to the required logic.
Hope this helps!

카테고리

Help CenterFile Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by