File Exchange Pick of the Week

Manage the (Automatic) Naming of Your Figure Windows 2

Posted by Brett Shoelson,

Brett's Pick this week is "setname", by Andrew Bliss.

Some of you may be familiar with my own togglefig function; it allows one to recall and reuse a figure by specifying its name. I find it extremely practical, and I use it almost on a daily basis. (It's particularly useful when you're iterating on a block of code separated into "sections." Andrew's setname makes a very nice companion to togglefig.)

As of this writing, Andrew has 10 files on the Exchange, which have collectively been downloaded more than 500 times in the past 30 days. (Not too shabby!) Nonetheless, I don't believe any of his submissions have previously been recognized as a Pick of the Week. Until now.

Ironically, the file of Andrew's that I am most enamored of is his least-downloaded.

So what does Andrew's function do? If you've previously indicated a title for the axes in your figure, setname automatically renames the figure to match axes name. If you haven't titled your graphic, the function extracts a string from the x- and y- labels of the axes to construct a figure name.

Consider, for example, this snippet of code:

t = 0:pi/64:8*pi;
plot(t,sin(t)+rand(size(t)))
xlabel('Time')
ylabel('Noisy Sine')
title('Noisy Sinusoid')

This creates a figure not-so-usefully named "Untitled" (with a number appended to differentiate it from the other n "Untitled" figures you've previously created.)

Now issue the setname command (no arguments needed--it works by default on the current axes, though you may specify a non-default axes.) Now the figure is renamed to "Noisy Sinusoid," and you can readily pick it out from all the other open figures.

Alternatively, if you didn't explicitly provide a title, setname would automatically name the figure "Time vs Noisy Sine." Now not only can I find the figure, I can easily reuse it:

% Recall the previous figure...
togglefig('Noisy Sinusoid')
plot(t,cos(t)+rand(size(t)))
title('Noisy Cosine')
% ...and then rename the figure!
setname

This is brilliant, Andrew...thanks! One suggestion: it would be really nice if I could pass to setname an argument ('all'?) that tells the function to rename all open figures. (For those times when I've lazily created 20 different 'Untitled' figures.)

As always, I welcome your thoughts and comments. Or leave feedback for Andrew here.


Get the MATLAB code

Published with MATLAB® R2013a

2 CommentsOldest to Newest

To make sure that all my figures are named, I have an alternative “figure” command (a modified version of “dfig” from the File Exchange) that reads the last command from the command history. So if I call
dfig,plot(time,intensity(:,3))
the figure’s title will be “plot(time,intensity(:,3))”. This way, I always remember what was plotted, or which thresholds I used when tuning an image segmentation.

Jonas…also nice! I like the capability of renaming figures after the fact–not based on the most recently executed command. But I can see how your figure-naming scheme could be useful, too.

These postings are the author's and don't necessarily represent the opinions of MathWorks.