9

I have the following problem: in my MATLAB code I use statements like

figure(1)

to change destination figure for some data. The problem is that after this MATLAB take system focus on the window with this figure.

When I run a big script in the background and try to do something else on my computer, MATLAB always takes focus and I can't do something normally.

Is there a way to disallow MATLAB to do this? I'm working in Linux Ubuntu.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
drsealks
  • 2,282
  • 1
  • 17
  • 34

4 Answers4

6

"Smart"/Silent Figure by Daniel Eaton.

slitvinov
  • 5,693
  • 20
  • 31
2

You could do this by making the figure invisible (visible off) at creation, and only making it visible when you want to show it.

For example:

f = figure('Visible', 'off'); %create an invisible figure
plot(rand(1,15)); %plot some stuff to it.

saveas(f, 'test.png', 'png'); %write out the image as a png
close(f); %destroy the figure

Alternatively: set(f, 'Visible', 'on'); %display a previously invisible figure

Note, if you save the figure as a Matlab .fig file, it will also save the fact that it is invisible, which can be a bit confusing.

Shaun314
  • 3,191
  • 4
  • 22
  • 27
Alan
  • 3,307
  • 1
  • 19
  • 22
1

This is untested, but based on the link to the smart figure, it looks like all you need to do to make your figure isn't stealing focus is this:

set(0, 'CurrentFigure', h);

And by the way, if you didn't know, the 0 is meaning "root"

Shaun314
  • 3,191
  • 4
  • 22
  • 27
1

In R2018a, the figure property "WindowState" was introduced, see https://blogs.mathworks.com/pick/2018/07/13/maximize-your-figures/

Using this, you can do

set(0, 'DefaultFigureWindowState', 'minimized');

before running the actual script, and this will cause all "standard plots" to not steal focus and be opened in minimized state.

There are functions that still steal focus. I did not investigate in detail, but I believe it's mainly automatic plotting functions such as psd, hist etc. without output arguments. If you call plot yourself you should be fine.

Eike P.
  • 3,333
  • 1
  • 27
  • 38