Maximize a figure on creation - matlab

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.

Related

How to plot figures but keeping them minimized in the taskbar?

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.

Colorbar not appearing in (surface) plot - LaTeX interpreter issue

As the title states, the colorbar in surface plots does not appear when the default interpreter is set to 'latex'. This occurs in MATLAB 2012b and 2013a and on two different machines.
Precisely, the colorbar object is created, can be clicked when editing the plot, can be edited using the interactive colorbar editor but is not visible at all.
It does not appear when saved as a figure and reopened, saved as a PNG, exported in .eps format or saved as a .pdf.
After searching around, I found the following post from 2011, concerning MATLAB 7:
http://mathforum.org/kb/message.jspa?messageID=7518470
Specifically, the interpreter appears to be at fault, when it is set to 'latex', the colorbar will not display. When set to the default, it does.
Here is the smallest demonstrating example.
set(0,'defaulttextinterpreter','none');
figure;
surf(peaks(100)); colorbar
set(0,'defaulttextinterpreter','latex');
figure
surf(peaks(100)); colorbar
The two figures are identical except that the colorbar is visible only in the first figure.
I use a lot of special characters and sub/superscripts in my plots so in startup.m I set the default interpreter to 'latex'. I could surround all calls to colorbar with:
set(0,'defaulttextinterpreter','none');
colorbar;
set(0,'defaulttextinterpreter','latex');
But this is probably the least elegant solution possible. Can anyone shed some light on this issue which appears to be extant for over 5 years and multiple editions of MATLAB?
This behaviour is gone in Matlab R2014b, which uses an entirely new graphics engine, hg2. The plots look different (most of the time in a better way), but instead of old, documented bugs, there are now new, undocumented bugs...
Earlier versions of Matlab support somewhat experimental stages of hg2. You can enable these by running Matlab with the switch "-hgVersion 2". You can do this, for example, by editing the Desktop shortcut to point to something like "C:\Program Files\MATLAB\R2013b\bin\matlab.exe" -hgVersion 2.
Unfortunately, with the new graphics engine being the default in Matlab2014b, the old bugs are less likely to be fixed in the future. I wish I could help you in a better way, but the workaround you posted seems like a good solution, especially if you wrap it in a function called robust_colorbar or so.
I can reproduce the problem on my system (R2010b, Windows Vista 32 bits) . It seems to be solved by changing the 'Renderer' property of the figure from the default 'OpenGL' to either 'painters' or 'zbuffer'. So, you can either change the renderer when creating the figure:
set(0, 'defaulttextinterpreter', 'latex');
figure('Renderer', 'zbuffer') %// this line changed
surf(peaks(100)); colorbar
or change the default renderer to be used for all figures (so you don't need to change it in every figure):
set(0, 'DefaultFigureRenderer', 'zbuffer'); %// this line added
set(0, 'defaulttextinterpreter', 'latex');
figure
surf(peaks(100)); colorbar
Using a renderer other than 'OpenGL' may affect features such as transparency or drawing speed. Here's some information about pros and cons of each renderer.

Disallow MATLAB to take focus automatically [duplicate]

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"

Figure legend doubles Matlab code execution time

I noticed that if I have Matlab code where my figure has to display a legend, the running time increases significantly.
Here's an example:
clear all
close all
clc
% legend test
x = [0:1:100];
y = x.^(3.123);
figure('Name', 'X-Y and X-X plot')
plot(x,y)
hold all
plot(x,x)
legend('1', '232')
Gives a running time of 1.1 seconds. Same code without the legend('1', '232') line has an execution time of 0.4 seconds. I find it very odd that a simple legend increases the running time this much.
With the profiler I found that the function mainly responsible for the time increase is called graphics/private/texmex. It has a self-time of 0.12 seconds and is called 4 times. When I don't create a legend this function is not called.
Is there a way to speed up my code, while still generating a legend in a figure?
I'm running 64-bit Matlab 2012b on Mac OS 10.8.3.
When I run the code in the example with set(0, DefaultTextInterpreter, 'none') the texmex function is called by tex>localCallTeXParser, which is called by scribe.legend.methods>strsize, etc...:
graphics/private/texmex
tex>localCallTeXParser
scribe.legend.methods>strsize
scribe.legend.methods>getsizeinfo
scribe.legend.methods>getsize
scribe.legend.methods
scribe.legend.legend
I had the same issue with a project of my own. I wanted to dynamically update some line plots quickly using a slider, but noticed that the having a legend active really killed the performance I was getting.
I found the solution to my problem here - http://undocumentedmatlab.com/blog/plot-performance/.
From the second listed performance hack, I added the lines
set(gca,'LegendColorbarListeners',[]);
setappdata(gca,'LegendColorbarManualSpace',1);
setappdata(gca,'LegendColorbarReclaimSpace',1);
to my code. I got an error message for the first line of code that was mentioned, so I struck it out above. Regardless though, the other two lines of code made my plots update just as quickly with a legend as they did without the legend being present.
Sounds like legend is using a TeX interpreter (at least, that's what texmex sounds like). In that case, you could try
legend({'1', '232'}, 'Interpreter', 'none');
This will disable the TeX interpreter and therefore may improve the performance. I should probably note that I've never experienced any trouble with the speed of the legend function, so it's probably something specific to your plots and/or MATLAB installation/version.
Edit: I have the feeling that the above will draw the legend with the TeX interpreter first, then disable it and draw it again. Try doing the following before drawing the legend or perhaps before drawing the figure (not sure at which point MATLAB will promote the default properties to an actual figure / axes / legend):
set(0, 'DefaultTextInterpreter', 'none');

Inhibit Matlab Window Focus Stealing

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