This question already has answers here:
Inhibit Matlab Window Focus Stealing
(2 answers)
Closed 3 years ago.
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"
Related
I'm using the below setting to maximize a Matlab Figure:
set(gcf,'units','normalized','outerposition',[0 0 1 1])
Although it is maximized, but it is still not fully maximized like the maximize icon in the label (below) is not on.
And this makes me loose parts of the figure when exporting it. So I'm wondering how I can fully maximize it as if I manually pressed the maximize button of the figure.
You can use some undocumented features to achieve what you want:
drawnow
jFig = get(handle(gcf), 'JavaFrame');
jFig.setMaximized(true);
The drawnow is not obvious, but essential as mentioned by Yair Altman in one of his comments how to avoid Java erros:
Another possible reason is due to EDT effects. The easiest solution is
to place a call to drawnow; pause(0.1); before you access the
JavaFrame functionality (setMaximized or any other Java function).
Tested with Matlab R2015a on Windows 8.1.
Is there a way to actually create figures in matlab and keep them minimized in the taskbar?
I know I can use
h=figure;
set(h, 'Visible', 'off');
but in this way in the taskbar there is no figure icon.
I simply like to plot something but keep it minimized in the taskbar: how can I do it?
Matlab doesn't have built-in functions to do this, so the second best thing to do would be to use Java.
This is plucked straight from Undocumented Matlab:
plot(1:10);
jFrame = get(handle(gcf),'JavaFrame');
pause(0.1) %//This is important
jFrame.setMinimized(true);
The pause is necessary because you'd otherwise get a NullPointerException because of the fact that the window hasn't been fully drawn yet.
i'm writting some code in Matlab editor, which has about 30 figures. So, when I publish it, it opens 30 figures windows, which is annoying. How do I keep it from opening the windows, but keeping the figures in the published window?
I've tried with close(figure), but then the figures don't show on the published window.
Thanks in advance
The simplest thing to do is close all when you are done with the figures. I'm not sure if that can be part of the script or if you have to run it manually after publishing.
At least the plot command has an option to control figure visibility. So you would write something like
h = plot(... , 'Visible', 'off');
I expect these exist for other graphics objects as well, I know it does for the figure associated with anova.
Edit: The above hides the plot but not the figure itself. To hide the figure immediately after it is created, do
set(gcf, 'Visible', 'off')
close function in matlab does what you want. Read the documentation for more details
To close all the plots at the same time, you could use
close all
To close a particular figure named 'fig5' (for example), you could use
fig5 = scatter(x, y);
close(fig5)
If you use just "close", only the recent figure will close.
Perhaps you want hold on which will plot all of the graphs to the same window?
You can Use subplot(m,n,p) to plot multiple graphs on same figure window.
to outline the solution,
first step is to plot using handler. Use figa=figure; where figa is now handler for figure. If you use multiple, like 30 you said, figures, then figa=figure;figb=figure.......figad=figure;
second step; use the figures for whatever you want to plot in;
it has to be done by revoking the figure, for example
figure(figa);hold on;plot(x1,y1)
figure(figb);hold on;plot(x2,y2)....so on for 30 plots
third set is to save all figures
saveas(figa,'1.fig');saveas(figb,'2.fig');.......so on for 30 plots;
fourth step is to close plots from your monitor
close all;
fifth step is to reopen those figures
openfig('1.fig');openfig('2.fig');.............so on for 30 figs
One suggestion: Use excel to create this long list of figure names and better use separate .m files to avoid bulking your matlab main code.
Running Matlab R2011b under Linux, I am generating a video using the VideoWriter. My code goes something like this:
h_fig = figure();
set(h_fig, 'Visible', 'on')
set(h_fig, 'Position', [300,200,898,720]);
h_axs = axes('Parent', h_fig);
set(h_axs,'nextplot','replacechildren');
vidObj = VideoWriter('leadfollow3.avi');
...
for i = 1:n
h_axs_a = subplot(3,2,[1 2]);
plot(h_axs_a, x, a_mag_lead, x, a_mag_follow, 'r');
...
The plot is composed of several subplots. The first subplot renders fine, but for the rest, the axes background remains grey instead of white. What's worse, for certain frames it switches to white, leading to annoying flicker in the video (see example video here). Individual plots outside the video writer loop don't display this artifact. What might be the best strategy to troubleshoot this?
Edit: after I got a chance to try this code on Windows, it seems that the problem is restricted to Linux. Nonetheless, I would still very much like to know a possible solution, since I do not have regular access to Windows boxes where I could do my computations.
You missed the crucial lines of code where you actually turn the figure into an image and send it to the VideoWriter, so it's difficult to be of much help. However, if you're not doing so already, I suggest you use export_fig for this task.
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.
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);
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
Create a new figure:
figure
Dock it:
This will put the figure into the Matlab IDE window.
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');