Is it possible to viewing the "figure" window on second display?

I have two displays connected to my PC. Is it possible to define which of to displays will be viewed "figure" window?

 Akzeptierte Antwort

Jan
Jan am 25 Sep. 2011
Bearbeitet: Jan am 2 Sep. 2020
You could create a function, which open figures on the 2nd monitor if it is available, otherwise the 1st monitor is used:
% [EDITED, 2018-06-05, typos fixed]
function FigHandle = figure2(varargin)
MP = get(0, 'MonitorPositions');
if size(MP, 1) == 1 % Single monitor
FigH = figure(varargin{:});
else % Multiple monitors
% Catch creation of figure with disabled visibility:
indexVisible = find(strncmpi(varargin(1:2:end), 'Vis', 3));
if ~isempty(indexVisible)
paramVisible = varargin(indexVisible(end) + 1);
else
paramVisible = get(0, 'DefaultFigureVisible');
end
%
Shift = MP(2, 1:2);
FigH = figure(varargin{:}, 'Visible', 'off');
drawnow;
set(FigH, 'Units', 'pixels');
pos = get(FigH, 'Position');
pause(0.02); % See Stefan Glasauer's comment
set(FigH, 'Position', [pos(1:2) + Shift, pos(3:4)], ...
'Visible', paramVisible);
end
if nargout ~= 0
FigHandle = FigH;
end
Another tool to move a (visible) figure to another monitor - under Windows only: FEX: WindowAPI

17 Kommentare

Mahabir
Mahabir am 8 Jan. 2017
Bearbeitet: Mahabir am 8 Jan. 2017
Hi Jan Simon, How to call your function, i.e. input parameter function FigHandle = figure2(varargin). Without input, it gives errors.
It appears to me that it is intended that figure2() be a drop-in replacement for figure() so call it with the same arguments you would call figure()
Just be advised that you must have opened MATLAB after plugging in the second monitor, otherwise the
get(0,'MonitorPositions')
will not have the information for the second monitor.
No longer works with Matlab 2017B
@Richard: Please use flags only to inform admins and editors about messages, which might conflict with the terms of use of this forum, e.g. by rudeness or spam. Thanks.
Please explain what "does not work" means. Otherwise I cannot guess how to fix the suggestion. I do not have R2017b, so it matters if you tell, what you observe.
Jan
Jan am 5 Jun. 2018
Bearbeitet: Jan am 5 Jun. 2018
[MOVED from flags] Michael Madelaire wrote:
I just found this function, smart. But cannot seem to get it to work. Missing parenthesis in line 7. I do not know how to fix it. And it seems as thought a ' is missing in line 18
@Michael Madelaire: Please use flags only to inform admins or editors about inappropriate content like spam or rudeness. I've mentioned this already in the former comment...
Thanks for finding the typos in my code. I've fixed them now. This is the problem of typing some code in the forum, while I do not have access to Matlab. I'm using WindowAPI for this job, therefore I did not test this code. Does the code work, if you add the parenthesis and the quote?
Peter
Peter am 6 Jun. 2018
Bearbeitet: Peter am 6 Jun. 2018
Hi Jan,
for me it works, if you change to ~isempty(indexVisible) in line 9 and "Shift" to "posShift" in line 19.
Thanks!
Thanks, Peter. Further typos fixed.
What is the function input supposed to be?
I get this error:
Numeric figure handles not supported with parameter-value pairs.
in line:
FigH = figure(varargin{:}, 'Visible', 'off');
The function input is the same as the usual figure function, except you replace 'figure' by 'figure2'.
Numeric figure handles not supported with parameter-value pairs.
That is a misleading error message.
figure() permits you to pass the handle of an existing figure as its only parameter. If the handle is numeric, then figure() either creates or raises the figure; if the handle is a figure object, then figure() raises the figure.
figure() also permits you to pass in name/value pairs. However, it only permits that if you did not pass in a handle.
FigH = figure(varargin{:}, 'Visible', 'off');
is an attempt to create (or activate) the figure designated by varargin{:}, but with visibility turned off. In current versions of MATLAB, that will not work if you passed in a figure handle, and you would have to use two steps.
The easiest work-around is not to pass in a figure handle (or figure number) to this figure2() code, and to instead just permit figure() to create whatever figure number it wanted.
This works well, thanks Jan!
I've tried this on a MacBook Pro with 2019b and it first didn't work ... but after a bit of testing I found a solution: adding a brief pause just before moving the figure helped.
MP=get(0,'MonitorPositions');
fig=figure;
if size(MP,1)>1
pos=get(fig,'Position');
pause(0.01); % this seems sometimes necessary on a Mac
set(fig,'Position',[pos(1,2)+MP(2,1:2) pos(3:4)]);
end
To allow passing of numbers I've tried commenting out the visibility options
FigH = figure(varargin{:});%, 'Visible', 'off');
and [it works just fine, it seems: it only shows the figure for a brief moment in the first screen.]
EDIT: Nope, forget that. If done like that, the figure seems to stop working: it cannot be refocused with figure2() nor figure() nor by mouse.
on my macbook running R2018b this code does not work. The figure still opens on the main monitor, just now it is moved to the top right side of it. size(MP,1) = 2, when I checked
I found a way to get it to work for an existing script with out changing all of the 'figure' calls to 'figure2'.
figure = @figure2
It seems as though you can overload the figure function with a function handle. It doesn't seem to affect the "standard" figure call inside the function (different memory space?).
However it only works if the figure call has parameters like...
figure('position',[300,50,600,700])
it doesn't work if there is just "figure" all by itself.
@Jan What does varargin refer to?
https://www.mathworks.com/help/matlab/ref/varargin.html
varargin allows you to receive a variable number of function arguments by using cell array syntax.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

the cyclist
the cyclist am 24 Sep. 2011
Yes, effectively. One way to do it is by defining the 'Position' property of the figure. If you create your figure, then type
>> get(gcf)
You will see the current properties of the figure, including the Position property. You can then set that position to be whatever you want, e.g.
>> set(gcf,'Position',[500 500 400 300])
The arrangement of your second display will determine where you need to "push" the figure to. For example, with my setup, I actually had to make the x-position (first value in the vector) a negative value to push it onto my second display.

2 Kommentare

Hi.
Thank you very much for your answer.
Extremely easy!
Regards, Jonatan.
Thanks, it perfectly works for me too. Very easy.
Cheers,
Guillaume

Melden Sie sich an, um zu kommentieren.

thalia
thalia am 17 Sep. 2019
Bearbeitet: dpb am 28 Feb. 2022
Simplifying Adan's script...
close all
f=figure('Units','normalized')
%place it wherever you want in the 1st monitor
f
%check units
%retreive location info
pos1 = f.Position
close all
f=figure('Units','normalized')
%place it wherever you want in the 1st monitor
pos2 = f.Position
close all
%Evaluate the following code or add it in the startup.m for permanent settings
%in case you want to add it permanently, replace the pos1,2 with the values (hard-coded)
MP = get(0, 'MonitorPositions');
if size(MP, 1) == 1 % 1 monitor
set(0, 'defaultFigureUnits', 'normalized','defaultFigurePosition', pos1)
else %2nd monitor
set(0, 'defaultFigureUnits', 'normalized','defaultFigurePosition', pos2)
end
Take your pick of answers:
  1. Yes, but you would not like the result; OR
  2. No, but you can make it work fairly well
The respective methods:
1. set(0,'DefaultFigurePosition', [x y w h])
where x, y, w, and h are fixed x and y coordinates and w and h are fixed width and height. You can work out the coordinates to use by examining
get(0,'MonitorPositions')
which is documented over here.
The reason you probably wouldn't like this is that all figures will appear at that exact position and size (on top of whatever was there before) unless the code overrides the position. And there are a bunch of things that are actually figures. Including all GUI elements and including all dialog boxes.
2. Instead of setting the default figure position for everything, either right when you create the figure or right afterwards, set its Position property to be where you want it (and the size you want) on the second monitor, having used the MonitorPositions root property to find the proper range.
Warning Either way, it is not possible to getframe() a figure that is on the second monitor. This may affect your ability to saveas() or print(). Some routines detect the situation and temporarily move the figure on to the main screen and move it back afterwards, but other routines just complain.

5 Kommentare

Does the PRINT problem concerns the pixel formats only, such that Yair's FEX: ScreenCapture can replace GETFRAME? Of does printing to an EPS using the Painters renderer fail also?
Isn't set(0,'DefaultFigurePosition', [x y w h]) exactly what hgrc.m does?
Walter Roberson
Walter Roberson am 25 Sep. 2011
Bearbeitet: Walter Roberson am 22 Mär. 2017
hgrc.m does not appear to be documented, except in this solution: http://www.mathworks.com/support/solutions/en/data/1-VD9YT/index.html?product=ML&solution=1-VD9YT
hgrc.m is going to set the default to be on the first monitor; the user can set the position to be on the second monitor instead.
However, since MATLAB uses "figures" for GUIs and dialog boxes as well as "plots", adjusting the default to be on the second monitor risks having most everything appear on the second monitor, not just "plots"
hgrc is called by matlabrc. You are correct that it doesn't appear documented. My figures, both in Linux and Windows appear on top of each other in the exact position and size. Most dialog boxes, and I cannot think of any that don't, take on a different size, and often a different position. Often they are located in about the center of the screen. My guess is if you change the default figure position to the second monitor some of the the dialog boxes would follow, but not all.
Unfortunately I cannot test dual monitors.
There is a private function used by the dialog boxes, something about "getnicedialogposition"; my access to my server is down again today so I cannot read the source at this time.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Environment and Settings finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by