## File Exchange Pick of the WeekOur best user submissions

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the Original version of the page.

# 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