How to prevent a figure window to automatically take focus
72 views (last 30 days)
Show older comments
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 Comment
Adam
on 18 Jun 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).
Answers (5)
Stephen23
on 18 Jun 2019
Edited: Stephen23
on 19 Jun 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.).
0 Comments
KSSV
on 18 Jun 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.
0 Comments
Martin Mössner
on 18 Jun 2019
1 Comment
Adam
on 18 Jun 2019
Edited: Adam
on 18 Jun 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.
raym
on 15 Dec 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;
0 Comments
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!