How to prevent a figure window to automatically take focus
71 次查看(过去 30 天)
显示 更早的评论
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 个评论
Adam
2019-6-18
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).
回答(5 个)
Stephen23
2019-6-18
编辑:Stephen23
2019-6-19
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 个评论
KSSV
2019-6-18
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 个评论
Martin Mössner
2019-6-18
1 个评论
Adam
2019-6-18
编辑:Adam
2019-6-18
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
2021-12-15
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!