# Toggle that Fig2

Posted by Jiro Doke,

Jiro's pick this week is togglefig by our own Brett Shoelson.

Brett and I, along with Bob, started writing for this blog about 5 years ago, back when Doug was still the owner of the blog. Since the beginning, we typically highlighted submissions that were written by the general public and not by one of us. But ultimately, we like to highlight files that can be of some value to our readers, and I must say that there's no shortage of such files in Brett's entries.

So what does togglefig do? Have you ever wanted to better manage your figure windows? I've often had a need to reuse my figure windows. Once, I was developing some piece of code that created multiple figures, and as I was iterating on my code, I kept creating the same number of windows each time, and I would end up with tens of figure windows open after a while. Of course I could "close all" of them at the beginning of my script, but I had some windows I needed open for other reasons. As a solution, I would create/select figures using some numbers, e.g.

figure(123)


This would always use the same figure window, or create one if it doesn't exist. But I would want to give a meaningful name to it, so then I would need to set the name property after getting the handle to the figure.

h = figure(123);
set(h, 'Name', 'Important Window');


It works, but it's just some extra lines of code that are not adding any value to the real purpose of my program.

Brett's togglefig addresses this in a very elegant manner. It would raise the figure with the specified name, or create one if it doesn't exist.

togglefig('More Important Window')


You can also pass in an optional argument to clear the figure window when it's given focus.

Thanks, Brett, for this extremely useful function that makes me more efficient in using MATLAB!

Get the MATLAB code

Published with MATLAB® R2013a

Sean de Wolski replied on : 1 of 2

Howdy Brett,

One thing I would like to see in togglefig() is an optional flag for setting the figure’s CloseRequestFcn to [] and back to closereq. I rarely leave figures open due to clutter and sometimes (too often) find that I’ve closed one I want. Setting it so that it doesn’t close resolves this.

Have a great weekend!

Julian replied on : 2 of 2

I will be checking out togglefig. Sounds like figure() but with names instead of numbers, which are generally easier for humans, even MATLABbers.

Some of Jiro’s remarks rang a bell with me: I too have suffered from a proliferation of programmatically generated figures cluttering my workstation. Windows Alt-Tab then just gives you a huge grid of identical figure icons! At the risk of banging my own drum, I made figDefaultKeyPress in response . This extends Windows Ctrl+W for closing a (figure) window with Shift+Ctrl+W to close all figure windows, Shift+Ctrl+E close all figure windows Except this one, Shift+Ctrl+A dock/undock All figure windows. I also get the normal shortcuts, Ctrl+0, etc. to take you to the command line, to continue working when a figure grabs focus.