Inhibit Matlab Window Focus Stealing

MatlabFocusPlotWindow Management

Matlab Problem Overview


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.

Matlab Solutions


Solution 1 - Matlab

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);

Solution 2 - Matlab

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.

  1. Dock the figures

  1. Create a new figure:

     figure
    
  2. Dock it:

    enter image description here

This will put the figure into the Matlab IDE window.

  1. Make new figures docked and run the code:

     set(0, 'DefaultFigureWindowStyle', 'docked');
     
     for i = 1:10
         fprintf('i: %g\n', i)
         figure;
         pause(1);
     end
     
     set(0, 'DefaultFigureWindowStyle', 'normal');
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNordlöwView Question on Stackoverflow
Solution 1 - MatlabPearsonArtPhotoView Answer on Stackoverflow
Solution 2 - MatlabtexnicView Answer on Stackoverflow