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.
"Smart"/Silent Figure by Daniel Eaton.
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.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With