Set figure to not be target for next plot - matlab

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

Related

Change Matlab default x-limits behavior

If I run
plot(1:1001)
Matlab automatically sets the x range to be 1:1200. I am aware that xlim([1,1001]) exists, but I don't want to add this line every single time I use plot.
Is there a way to change Matlab's behavior so that plot sets the x-limits equal to the range of the data by default instead of having to do it manually every time? (For example with a flag I can set at the top of my script).
Or a parameter I can pass to plot to make this happen?
You could set your default XlimSpec property for Axes.
set(0,'defaultAxesXLimSpec', 'tight')
Then when you plot it will use X axes tight setting for every new plot.
This will revert back to normal after you close & restart Matlab. However, you could add that to your startup script to always apply it.
This meets some of your requirements I saw you mentioned in the comments about not using a wrapper and trying to minimize running extra commands after call plot
Matlab uses an internal algorithm to determine the best interval of axis ticks. I doubt you can manipulate it and, even if it was possible, I recommend you not to do it. Such operation would change the behavior of your own Matlab installation, but everybody else using your code would stumble upon a different axis labelling probably not well fitting with your purposes.
Keep on defining your ticks manually or implement your own generalized logics into a separate function that you can release together with your scripts. This code shows how to implement one, and can represent a good starting point for you.
If you want to make things simpler, create a wrapper of the plot function as follows:
h = plot_wrapper(true,1:101,1:101);
function varargout = plot_wrapper(fix_limits,varargin)
han = plot(varargin{:});
if (fix_limits)
x = get(han,'XData');
xlim(gca,[min(x) max(x)]);
y = get(han,'YData');
ylim(gca,[min(y) max(y)]);
end
if (nargout)
varargout{1} = han;
end
end
You want the x-axis limits to tightly fit your data. If you don't mind the y-axis also having that behaviour, you can simply use
plot(1:1001)
axis tight
From the documentation,
axis tight sets the axis limits to the range of the data
If you want only the x-axis to be tight, you can do it manually as follows:
h = plot(1:1001); % h is a line object
h.Parent.XLim = [min(h.XData) max(h.XData)]; % set x-axis limits as per the line data
Note that the second line uses dot notation, which is available since R2014b. For older Matlab versions use set/get .
Or you can do it automatically by setting the seemingly undocumented 'XLimSpec' property of the axis to 'tight':
plot(1:1001)
set(gca, 'XLimSpec', 'tight')
which is what axis tight internally does (for the x-, y- and z-axis), at least in R2017a.

Hide MATLAB legend entries for some graphical objects in plots

MATLAB legends list everything in a plot, including guidelines that you have put on a plot.
A fudge to get around that is to do
*Plot
*Add legend
*Add guidelines
However, MATLAB puts the most recent lines in the front, meaning the guidelines then sit over the displayed data; ugly and distracting.
Similar problems occur any time you build up a complicated plot, legend freaks out and grabs everything, and workarounds with plotting order can be ugly
Example code:
%**** Optional guidelines
figure(1)
plot([2 2],[0,1],'k--'); hold on
%**** DATA
N = 4;
y=rand(5,N);
x=1:1:5;
for plotLoop=1:N;
%* Plot
figure(1)
plot(x,y(plotLoop,:));
hold on
end
%*****LEGEND
hLegend = legend(LegTxt,...
'interpreter','latex',...
'location','eastoutside')
(move the code block order to replicate the situations mentioned above)
How to reasonably fix this?
If you want a certain graphics object to not produce a legend (and that will work even if you toggle the legend off and on again), you can modify the LegendInformation:
%# plot something that shouldn't show up as legend
handleWithoutLegend = plot(something);
%# modify the LegendInformation of the Annotation-Property of the graphical object
set(get(get(handleWithoutLegend,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','off');
%# toggle legend on and off at will, and never see the something-object appear
If you try to turn off the legend on an array of handles, the best way is just to loop over them, with a try-wrapper for graphical objects that cannot produce a legend:
for h = listOfHandles(:)'
try
set(get(get(h,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','off');
end
end
Craft a custom handle that you feed into the legend. Plot handles can be concatenated to form an object that legend is happy to accept as input.
The required code isn't pretty, but it does work.
%**** Optional guidelines for periodicity
figure(1)
plot([2 2],[0,1],'k--'); hold on
%**** DATA
N = 4;
y=rand(5,N);
x=1:1:5;
for plotLoop=1:N;
LegTxt{plotLoop} = num2str(plotLoop);
%* Plot
figure(1)
% if statement to construct a handle for the legend later
if plotLoop==1
htot=plot(x,y(plotLoop,:));
else
h=plot(x,y(plotLoop,:));
% Append this info to the figure handle
htot= [htot, h];
end
hold on
end
%*****LEGEND
hLegend = legend(htot,LegTxt,...
'interpreter','latex','FontSize',16,...
'location','eastoutside')
For the pedantic or curious, the loop for plotLoop=1:N; is here because I extracted the example from some rather complex code where the data is extracted from cell arrays. Obviously you could eliminate that loop for a lot of usage scenarios, I just decided to keep the code in its most flexible format!
You can also hide plot from legend in another way. Here's the sample:
figure(1)
hold on
x=1:10;
y1=x;
y2=x.^2/10;
y3=x.^3/100;
plot(x,y1);
plot(x,y2,'HandleVisibility','off');
plot(x,y3);
legend('x','x^3')
You just need to put 'HandleVisibility', 'off' to your plot that you don't want to show up in legend. That's how result looks like:
HandleVisibility is a line property so it might now work if you create plot in some other way. But for most use cases its enough and it is much simpler.

How to refreshing annotations and subplots accross function calls in MATLAB

I am using MATLAB for my data analysis. In my scripts I create figures with fit results so that I can quickly play with fit parameters and see how they change my end results.
My question is whether there is a simple way to be able to just refresh my figures along with subplots and annotations without losing the positions and sizes of the subplots and annotations.
I.e.: I would like to be able manually position my figures on my workspace (I use Linux), manually adjust figure size/position, subplot sizes/positions and annotation sizes/positions and then have their content update, when I rerun the script that does my fitting.
I do realize that the command figure(...) does this nicely and it works, but I am having the problem, that when I resize/move subplots and move annotations, that they're sizes/positions get lost, when I rerun the script.
I am aware that I probably need to use the subplot/annotation handles for this but the question is, what the most elegant and simple way is to do this? Since I need the code to also work when run the first time (i.e. no figures/subplot/annoations yet existent), will I need a lot of if-clause, to check if the handles already exist?
I've been using MATLAB for quite some time and for almost equally long it's been bothering me that I don't know an elegant way to do this!
I had two ideas:
use the "File > Generate Code..." functionality. MATLAB will create
a function that recreates the figure with any modification you made
interactively.
manually retrieve the properties of interest for the objects manipulated
and apply them again when you rerun your scripts. You could either
maintain a list of handles for those graphics objects, or even use the
'Tag' in combination with the FINDOBJ function to locate such objects.
I will illustrate the latter idea with an example:
When the script is run for the first time, we give the user the chance to make changes to the figures interactively. Once done, we retrieve the 'Position' property of figures and all children components contained inside them. These values are then saved to a MAT-file.
Now the user adjusts some parameters and reruns the script. We check for the presence of the MAT-file. If it exists, we load the saved position values and apply them to the figures and their descendant objects, thus restoring the components to their last saved state.
This solution is rather simplistic, thus if changes are made to the script breaking the hierarchy of the graphics handles, you will have to delete the MAT-file, and run the script again.
%# close all figures
close all
%# your script which creates figures
figure, plot(rand(100,1))
figure
subplot(121), plot( cos(linspace(0,6*pi,100)) )
subplot(122), image, axis image, axis ij
%# check for MAT-file
if exist('script_prefs.mat','file')
%# load saved values
load script_prefs.mat
%# get opened figures, and find objects with a position property
fig = get(0, 'Children'); %# allchild(0)
obj = findobj(fig, '-property','position');
try
%# apply values to position property
set(fig, {'Position'},figPos);
set(obj, {'Position'},objPos);
catch
%# delete MAT-file
delete script_prefs.mat
end
else
%# get opened figures, and find objects with a position property
fig = get(0, 'Children');
obj = findobj(fig, '-property','position');
%# wait for the user to finish customizing
waitFig = figure('Menubar','none', 'Position',[200 200 200 40]);
h = uicontrol('Units','normalized', 'Position',[0 0 1 1], ...
'String','Done?', 'Callback','uiresume(gcbf)');
uiwait(waitFig);
close(waitFig);
%# get position property of figures and tagged objects
figPos = get(fig, 'Position');
objPos = get(obj, 'Position');
%# save values to file
save script_prefs.mat figPos objPos
end
I take it you mean you want to refresh the plots themselves, but not anything else.
When you perform a plot(), specify an output argument to retrieve a line handle. Then, when you want to plot different, data, manually adjust that line handles' XData and YData:
lh = plot(xdata,ydata);
%# do some calculations here
...
%# calculated new values: newX and newY
set(lh, 'XData', newx, 'YData', newy);
This is likewise for anything else you want to refresh but not recreate - get the handle corresponding to the graphics object and manually update its properties at a low level.

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.

How to create a new figure in MATLAB?

Usually when I plot in MATLAB, it always draws on the same figure. How do I make it draw in a new figure?
I know it is pretty elementary, but I'm not finding it using Google Search.
figure;
plot(something);
or
figure(2);
plot(something);
...
figure(3);
plot(something else);
...
etc.
While doing "figure(1), figure(2),..." will solve the problem in most cases, it will not solve them in all cases. Suppose you have a bunch of MATLAB figures on your desktop and how many you have open varies from time to time before you run your code. Using the answers provided, you will overwrite these figures, which you may not want. The easy workaround is to just use the command "figure" before you plot.
Example: you have five figures on your desktop from a previous script you ran and you use
figure(1);
plot(...)
figure(2);
plot(...)
You just plotted over the figures on your desktop. However the code
figure;
plot(...)
figure;
plot(...)
just created figures 6 and 7 with your desired plots and left your previous plots 1-5 alone.
The other thing to be careful about, is to use the clf (clear figure) command when you are starting a fresh plot. Otherwise you may be plotting on a pre-existing figure (not possible with the figure command by itself, but if you do figure(2) there may already be a figure #2), with more than one axis, or an axis that is placed kinda funny. Use clf to ensure that you're starting from scratch:
figure(N);
clf;
plot(something);
...
As has already been said: figure will create a new figure for your next plots. While calling figure you can also configure it. Example:
figHandle = figure('Name', 'Name of Figure', 'OuterPosition',[1, 1, scrsz(3), scrsz(4)]);
The example sets the name for the window and the outer size of it in relation to the used screen.
Here figHandle is the handle to the resulting figure and can be used later to change appearance and content. Examples:
Dot notation:
figHandle.PaperOrientation = 'portrait';
figHandle.PaperUnits = 'centimeters';
Old Style:
set(figHandle, 'PaperOrientation', 'portrait', 'PaperUnits', 'centimeters');
Using the handle with dot notation or set, options for printing are configured here.
By keeping the handles for the figures with distinc names you can interact with multiple active figures. To set a existing figure as your active, call figure(figHandle). New plots will go there now.
Another common option is when you do want multiple plots in a single window
f = figure;
hold on
plot(x1,y1)
plot(x2,y2)
...
plots multiple data sets on the same (new) figure.
As simple as this-
figure, plot(yourfigure);