MATLAB tip of the day: Toggling figure visibility

The Problem

Usually I like to see MATLAB figures as they are produced, but if I'm running a long script, visible figures can clutter the screen and hog the available memory. I wanted a way to change visibility quickly, easily, and without having to remember how I did it.

My Solution

I wrote a small script that toggles the DefaultFigureVisible state, and popped that into my Shortcuts bar. Now I can just click the shortcut whenever I want to change the state.

Here's the code:

% Toggle figure visibility
state = get(0, 'DefaultFigureVisible');
switch state
    case 'on'
        set(0, 'DefaultFigureVisible', 'off')
        disp('Figures are off.')
    case 'off'
        set(0, 'DefaultFigureVisible', 'on')
        disp('Figures are on.')
    otherwise %In case something goes completely nuts 
        disp('No change made.')
        disp(state)
end