Is there a way to tell Matlab not to steal window focus (from an external editor) such as Emacs) upon graphical commands such as figure
and plot
. This would increase my productivity a lot because I often want to continue code development during data (re-)processing.

- 11,838
- 10
- 52
- 99
-
14VERY good question. It's so annoying. – Oli Dec 13 '11 at 13:06
2 Answers
It is possible, the trick is to not use the figure statement, but to change the current figure directly. This will change the active plot without changing the focus. Typically I do something like this:
function change_current_figure(h)
set(0,'CurrentFigure',h)
Then, all of the figure(h) statements need to be changed to change_curent_figure(h).
Note, this is included in the matlab documentation.
It should be noted, this only works if the figure is already created. If new figures are going to be periodically created, one could create the figures as the very first few lines of code, save the handles, do the processing, and then plot to them. This example would work. Note, the drawnow command will flush the event buffer, making sure all figures are plotted.
I've seen this work from 2007-2010, not sure if the latest or earlier versions support this, although I have no reason to suspect they don't.
fig1=figure;
fig2=figure;
drawnow;
[a b]=do_complex_processing;
change_current_figure(fig1)
plot(a);
change_current_figure(fig2)
plot(b);

- 38,970
- 17
- 111
- 142
-
1
-
2Doesn't help much though since `plot()` steals (my) window focus anyway and raise figure window. Can we prevent this too? I'm sitting on Ubuntu 11.10. – Nordlöw Dec 14 '11 at 10:36
-
1@Nordlow: I'll edit my answer, but there is a few tricks to how to manage it. The trick I listed only works if the figure is already created. New figures always to go the front focus. – PearsonArtPhoto Dec 14 '11 at 15:05
-
1Does this also work for `imagesc` command? It seemed a bit trickier to me. – Vesnog May 22 '15 at 15:03
-
There is a script on file exchange that addresses this same problem: [Let me work, figure!](http://www.mathworks.com/matlabcentral/fileexchange/33987-let-me-work--figure-#comments) – Brett Sep 27 '16 at 19:33
-
1Modern MATLAB would use `set(groot,'CurrentFigure',h)`. `groot` is the handle object for the root of the graphics system (i.e. handle number 0). – Cris Luengo Jun 11 '19 at 20:13
I've got the same question, with the additional complexity that the code creating figures came from an external supplier, and I didn't want to modify it. Here are two possibilities (identified with the help of MathWorks support) tested on Matlab 2014b:
1. Generate the figures without showing them, then show them after the code completion
set(0, 'DefaultFigureVisible', 'off');
for i = 1:10
fprintf('i: %g\n', i)
figure;
pause(1);
end
set(0, 'DefaultFigureVisible', 'on');
figHandles = findall(0, 'Type', 'figure');
set(figHandles(:), 'visible', 'on')
This code does exactly what's needed, but the added inconvenience is that you cannot see any progress of your code run, thus being unable to interrupt a long run if something goes wrong.
2. Dock the figures

- 3,959
- 4
- 42
- 75
-
I presume 2 works for you. I implemented it and the whole matlab window still pops up and steals focus... – Brett Sep 27 '16 at 17:01
-
@bizzy, which Matlab version are you using? As I wrote, I tested it with Matlab 2014b. – texnic Sep 27 '16 at 17:32
-
1@bizzy, sorry, cannot comment on that version. Please consider asking Mathworks support, and posting the result here. – texnic Sep 27 '16 at 19:18
-
1
-
Instead of setting the default figure visibility, you can create figures with `figure('Visible','off')`. – Cris Luengo Jun 11 '19 at 20:15
-