MATLAB clear current figure - matlab

I want to clear MATLAB's global CurrentFigure property, because I need a plot that I make to not be overwritten if a careless user uses plot without opening a new figure. I tried
set(0, 'CurrentFigure', []);
But it doesn't seem to work. Is this impossible?

No, this doesn't work.
What does work is set(myFigureHandle,'HandleVisibility','off'). This way, the figure with the handle myFigureHandle will not become the current figure anymore.
To be really safe, you may want to set the axes' handle visibility to 'off', which will hide them from gca. In order to plot into these axes, you'll have to use plot(myAxesHandle,...), though, i.e. you have to explicitly point to the axes if you want to plot there.

Related

Set figure to not be target for next plot

I'm working on a custom progress monitor with some graphs. I've noticed that Matlab's waitbar creates a figure with some special properties so that if you do
plot(rand(100,1));
wb = waitbar(0);
plot(rand(100,1));
the second plot ends up replacing the first plot and not in wb. Is there a property I can set so that when I create my progress monitor and then plot something afterwards, the graph doesn't end up in my figure?
To be clear, I'm trying to have
plot(rand(100,1));
temp = MyProgressBar();
plot(rand(100,1));
create a figure for the first plot, create a different figure in the second line, then plot a new graph in the third line.
To protect your progress bar figure against subsequent plotting operations, I would set the 'HandleVisibility' property of its axes to 'off'. That should prevent it ever becoming the current axes, thus keeping subsequent plotting commands from modifying or adding to it. It's a good practice for stand-alone figures/GUIs in general that you turn off the handle visibility of all objects (figure, uicontrols, etc.) in this way to insulate them against being modified by outside code. This is almost certainly what is done in the code for waitbar.
As an additional aside, it's good practice to target your plots to a given axes by passing the axes handle as the first argument. You also have to make sure that, if you want new plots to be added to existing plots, you use things like the hold command first. Here's how I'd rework your example, assuming you want the two plots to appear on the same axes:
plot(rand(100,1)); % Creates new figure and axes
hAxes = gca; % Get the axes handle
hold on; % Allow subsequent plots to be added
temp = MyProgressBar();
plot(hAxes, rand(100,1)); % Will be added to the first plot axes

How to make an axes current which is inside a figure created by GUIDE?

I created a bank figure using GUIDE and put an axes object inside it and saved the figure. Now I want to load the figure and set its axes as current axes object. This is my code:
close all; clear all; clc;
fh = openfig('test.fig');
ah = findobj(fh, 'tag', 'axes1');
figure(fh);
axes(ah);
plot(rand(10, 1));
But plot creates a new figure and plots in it! Am I missing something?
I know that I can solve it with plot(ah, ...), but I want to make gca to return this new axes. I have a lot of plotting codes that I want to be drawn in this new axes.
By default, HandleVisibility of GUIDE figures is set such that they aren't automatically detected. For example if you load the figure and then call gcf, you'll also create a new figure.
To have the plot be placed within the axes, you can specify the axes explicitly as the parent of the plot command.
plot(rand(10, 1), 'Parent', ah)
Alternately, you could specify that the HandleVisibility of the figure is 'on'. And then plot will be able to find it. This could be done by either setting the value of HandleVisibility using the property editor in GUIDE or calling the set function:
set(fh, 'HandleVisibility', 'on')
I recommend the first option as explicitly specifying the parent axes is always better than implicit.

setting axes off for all figures using 'set' and 'gca'

Since I am working on images, I do not want to have axes for all my axes.
I can do by inserting 'axis off' for every figure. But I was wondering if anyone could propose a method to set the axis off for all figure using 'set' and 'gca'.
Check out the list of default properties you can set for your graphics here: http://www.mathworks.com/matlabcentral/newsreader/view_thread/161797
You can accomplish what you need by setting
set(0,'DefaultAxesVisible','off')
before you begin plotting. If you want to do this on a plot-by-plot basis, you can also try
set(gca(), 'Visible', 'off')

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.

Why won't this axes object display correctly in MATLAB?

I am writing two small psychoacoustic testing applications in MATLAB. The first one works without problems but the second one doesn't, and I just can't figure out why.
Here is the problem: the axes object is created, but it is empty.
failed_axis http://dl.getdropbox.com/u/98854/help.png
Here is the code that creates this figure and axes:
hFig = figure('dockcontrols','off','menubar','none', ...
'name','choose the better sounding file', ...
'numbertitle','off','position',[0,0,700,500], ...
'resize','off','toolbar','none','units','normalized', ...
'color',[.8,.8,.8]);
progress_idc = axes('position',[.1,.8,.8,.05],'box','on','parent',hFig,...
'xlim',[-.03,1.03],'xtickmode','manual','xtick',[], ...
'xticklabelmode','manual','xticklabel',[], ...
'ylim',[-1,1],'ytickmode','manual','ytick',[], ...
'yticklabelmode','manual','yticklabel',[], ...
'nextplot','add');
And here is the code that plots in this axes (the function is called regularly by a timer):
function replot(varargin) % this is a nested function
cla;
% plot start_indicator
plot([x_start,x_start],[-.7,.7],'k','linewidth',2);
fill([x_start,x_start-.02,x_start-.02],[0,-.7,.7],[0,0,0]);
% plot stop_indicator
plot([x_stop,x_stop],[-.7,.7],'k','linewidth',2);
fill([x_stop,x_stop+.02,x_stop+.02],[0,-.7,.7],[0,0,0]);
% plot play_position
plot([x_play,x_play],[-1,1],'r');
drawnow;
end
This is what it looks like if it works:
proper_axis http://dl.getdropbox.com/u/98854/help2.png
Do you have any idea what is going wrong here?
I ran the code you included above and got the correct output.
If I had to take a wild guess as to what the problem is, I'd guess that you may be creating other axes in your application that you are not listing above, or that you may have other axes not related to the application open at the time the application is running. When you plot your objects in the function replot, you are by default plotting them to the currently active axes. If you have multiple axes open, the plotting may therefore be going on in the wrong set of axes.
One suggestion I would make is to explicitly specify what the parent axes object should be in your calls to PLOT and FILL. If you add the arguments ...,'Parent',progress_idc); to your plotting calls, it will ensure that the correct axes is always used. I make a habit of always specifying the parent axes object instead of assuming that the currently active axes will always be the one I need it to be.
I finally found the (dumb) answer. The title accidentally had the same position as the plot axis. Due to some rendering-details of Matlab, it obscures the whole axis except for the rightmost and bottommost line of pixels, which makes the axis look "empty".
Oh, what a dumb error.