How to prevent a figure window to automatically take focus

69 visualizaciones (últimos 30 días)
Martin Mössner
Martin Mössner el 18 de Jun. de 2019
Respondida: raym el 15 de Dic. de 2021
I have written a Matlab program that does a lot of calculations (hours). During the calculations it does some plotting to document the progress. These plots are created with a figure command and finally are saved to a file for further processing, etc.
Approximately every minute the program creates a new plot.
During these calculations I want to do something else on the computer, e.g. do some text processing.
But, everytime when a new plot is started the focus is stolen and I have to click to the text processing program to continue the text editing. Is it possible to prevent Matlab from catching the focus, when it begins a new figure. This is very anoying.
The only solution to this problem I have is to do the calculations over night, when I do not work.
Sincerely, Martin
  1 comentario
Adam
Adam el 18 de Jun. de 2019
I suppose you could create all figures as invisible, then make them visible at the end (having kept hold of all their handles, of course, so you can easily do it in a single instruction).

Iniciar sesión para comentar.

Respuestas (5)

Stephen23
Stephen23 el 18 de Jun. de 2019
Editada: Stephen23 el 19 de Jun. de 2019
The solution requires a few trivial changes to your code:
  • Create any figures before the loop, adding any placeholder axes/lines/patches/...
  • Inside the loop replace the axes contents or update the line data / patch data / ...
Then your figure will stay where you put it, e.g. minimized or hidden under other windows, even though you are updating the data in its children.
Use explicit graphics object handles to make this easier: always specify the parent of any object (e.g. when plotting, etc.).

KSSV
KSSV el 18 de Jun. de 2019
We should look into code for straight solution. Check the below code:
for i = 1:10000
figure(1)
plot(rand(1,100));
end
In the above, the figure takes control always...if such lines are ther ein your code, comment them. Also have a look is drawnow present in the code, if so comment it.

Martin Mössner
Martin Mössner el 18 de Jun. de 2019
make the figures invisible works in my case!
I inserted the line
set(0,'DefaultFigureVisible','off') at the begin of the code and
set(0,'DefaultFigureVisible','on') an the end.
:)
coment 1: I open new figures with figure(1), figure(2), ...
I think if one opens a figure with figure(1) and later in the code one says again figure(1) then
the invisibility for figure 1 gets lost.
coment 2: another solution
set(0,'DefaultFigureWindowState','minimized') showed curious effects. I guess if one opens a minimized figure, then the initial statement gets obsolete.
  1 comentario
Adam
Adam el 18 de Jun. de 2019
Editada: Adam el 18 de Jun. de 2019
Don't keep calling
figure(1)
Once you have created it assign its handle to a variable and use that as Stephen Cobeldick mentions in his answer.
Convenience function overloads that Mathworks provide (i.e. a lot of plotting functions which do not use the explicit 'Name', 'Value' pair syntax for all inputs) quite often have side effects which are supposed to be part of the convenience for what people would most want, but when you don't want all these things you are better working with explicit instructions and the raw function syntaxes (i.e. those specifying everything as 'Name', 'Value' pairs). The convenience functions are really just wrappers for a group of functionality based around, in this case, bringing figure 1 to be the current figure. In a general use case it is evidently considered that making it visible, if it isn't already, as well as bringing it to the front, are desirable if you are about to plot on it.

Iniciar sesión para comentar.


Martin Mössner
Martin Mössner el 19 de Jun. de 2019
Usually I use figure(1) just once at the time I want to create the figure.
In cases when I use figure(1) the second time this usually is unintended.
my programs usually have lots of functions and figure calls are in some subfunction at the place where the data to be plotted were calculated. I usually, too, save this data to a file and so plotting could be done at a later time or with a separate program but if I do so I would not get any information during the calcualtion wheter the program works fine or not.
So what I say the plots should stay where they are.
However, my inital question deals with the case when a program is fully developed. The program calculates somesthing, writed results to data files and makes plots of the results and saves these plots to files. In that case the program should fully operate in the background and should not request focus at any time.
For this case, it seems, making the plots invisible is the best way to do it. :)

raym
raym el 15 de Dic. de 2021
This is how I solve the problem:
focusHwnd = WindowAPI(0,'GetFocusHWnd');
% ... do the figure, plot, etc.
WindowAPI(focusHwnd,'Front');
A insertion to WindowAPI.c before recompile:
} else if (STRCMP_NI(Command, "GetFocusHWnd", 12) == 0) { // ----------------------
//get the hwnd that has current focus
Reply = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
aForeGround = GetForegroundWindow();
*(uint64_T *) mxGetData(Reply) = (uint64_T) aForeGround;

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by