Cant define figure position on second monitor with Mac OsX Using R2022b Update 3

3 次查看(过去 30 天)
I have posed this question before and not received a useable answer.
I am trying to define the position of a new figure on a second monitor. When I define the position as in :
fig1h=figure(1);
set(fig1h,'Position',[238 1457 1197 420]);
when I execute this code segement the figure appears on the home screen not on the screen defined by the position. I get the position by calling:
get(fig1h)
after the function has executed,
If I execute:
set(fig1h,'Position',[238 1457 1197 420]);
from the command line it moves the figure to the desired location.
Has anyone else experienced this problem and more importantly is there a fix?
MT
  1 个评论
John O'Hair
John O'Hair 2023-8-5
移动:Voss 2023-8-5
I have the same problem in 2023a
g = groot;
f = figure('Position',g.MonitorPositions(2,:));
will place the figure at the edge of the primary monitor closest to the second monitor.
I've tried to use set(f,'Position',g.MonitorPositions(2,:)) and f.Position = g.MonitorPositions(2,:) with and without a following drawnow command.
It works in the command line but not from within the script.

请先登录,再进行评论。

回答(3 个)

Walter Roberson
Walter Roberson 2022-12-30
If a graphics command does not work while executed inside a function or script, but the same command works at the command line, then there are two common causes for that:
  1. The command might contain a reference to a variable that lives in the base workspace that is not in scope inside the function or script (which is obviously not your situation here); OR
  2. You might need a drawnow() to trigger graphics update (possibly the solution here)
Note: we recommend against hard-coding off-screen positions. We recommend instead that you
locations = get(groot, 'MonitorPositions');
new_position = [locations(2,1) + x_offset, locations(2,2) + y_offset, figure_width, figure_height];

John O'Hair
John O'Hair 2023-8-5
I've tried
g = groot;
f = figure;
f.Position = [g.MonitorPositions(2,1)+10, g.MonitorPositions(2,2)+10, g.MonitorPositions(2,3)-20, g.MonitorPositions(2,4)-20];
% which is what is suggested
f.WindowState = "maximized";
All that does is maximize the figure in the primary monitor. This is clearly a bug on the mac since doing the same commands in the command line always provides the expected result.
  1 个评论
John O'Hair
John O'Hair 2023-8-5
This seems to work...
g = groot;
f = figure;
f.Position = [g.MonitorPositions(2,1)+10, g.MonitorPositions(2,2)+10, g.MonitorPositions(2,3)-20, g.MonitorPositions(2,4)-20];
drawnow;
set(f,'Position',g.MonitorPositions(2,:));
drawnow;
f.WindowState = "maximized";
drawnow;
It appears the drawnow command must be repeated at least twice. The third one may not be necessary, but on my Mac in 2023a, this is the most stable method to force the figure into the second monitor.
It's a solution...

请先登录,再进行评论。


Vincenzo
Vincenzo 2024-1-17
I found a workaround:
monitorNumber = 2; % Number of the monitor as identified by the OS
monitors = get(0, "MonitorPositions"); % Get the position of the monitors extrema (each row is a monitor)
fig = figure(); % Create a new figure
get(fig); % Get figure information
pause(0.5); % <------ This pause let Matlab draw the figure on the main monitor
set(fig, "Position", monitors(monitorNumber, :)); % Move and resize the figure to desired monitor
This code specifically resizes the figure so that it spans the entire chosen monitor, but it can be easily modified.

类别

Help CenterFile Exchange 中查找有关 App Building 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by