Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disallow MATLAB to take focus automatically [duplicate]

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.

like image 662
drsealks Avatar asked Jun 28 '13 10:06

drsealks


4 Answers

"Smart"/Silent Figure by Daniel Eaton.

like image 139
slitvinov Avatar answered Nov 16 '22 01:11

slitvinov


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.

like image 24
Alan Avatar answered Nov 16 '22 02:11

Alan


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.

like image 25
jhin Avatar answered Nov 16 '22 02:11

jhin


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"

like image 44
Shaun314 Avatar answered Nov 16 '22 02:11

Shaun314