5.0

5.0 | 2 ratings Rate this file 122 downloads (last 30 days) File Size: 2.14 KB File ID: #18220

TOGGLEFIG

by Brett Shoelson

 

04 Jan 2008 (Updated 26 Jun 2009)

Code covered by BSD License  

Finds and activates, or creates, figure with user-specified name.

Download Now | Watch this File

File Information
Description

Finds and activates, or creates, figure with user-specified name.
 
If no name is provided, creates figure named "untitledn" (where n is incremented to result in a unique name).
 
SYNTAX:
togglefig('My Figure');
If figure named 'My Figure' exists, it will be activated (brought to the front and shown). Otherwise, it will be created.
 
h = togglefig('My Figure');
     Also returns the handle to the specified or created figure.
 
togglefig;
     Creates and activates new figure named untitled1, untitled2, ...
     Note: You can subsequently activate these figures with, for instance, togglefig('untitled1').
  
OTHER EXAMPLES:
NOTE: This example requires the Image Processing Toolbox
im = imread('cameraman.tif');
for ii = 1:10
     thresh = ii/20;
     togglefig('Threshold');
     imshow(im2bw(im,thresh));
     title(sprintf('Threshold = %0.2f',thresh));
     pause(1)
end

Motivation:
    I've found this to be exceptionally useful in algorith-development
    mode, particularly when iterating on cells in the cell-mode editor. (I
    use this function in almost every mfile I write these days.)

MATLAB release MATLAB 7.5 (R2007b)
Zip File Content  
Other Files license.txt,
togglefig.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (7)
04 Jan 2008 John D'Errico

Its excellent, as I fully expect from Brett.

I don't know if this is being picky or not, but I'd add a couple of lines to the very end.

if nargout==0
  clear fig
end

I think this is the standard for functions like plot or figure. When called as a command or with no output arguments, they do not try to return an output. Thus I'd expect to be able to do

togglefig Fred

and have a figure named "Fred" created, but without the handle itself ever returned. You should only get a handle when you ask for it. As it is now, togglefig now returns

ans
   = 1

when I call it as a command.

I'll rate this a 5 anyway.

10 Jan 2008 Brett Shoelson

Thanks for the comment, John. My first thought was to implement your suggestion; it would be trivially easy. But on reflection, I like TOGGLEFIG's forced issuance of a handle. In my workflow, I do things like:

set(togglefig('FigureName'),'color','k');

This gives me painless (by-name) handling of my figures, and frees me from having to use the more unwieldy

set(findobj('type','figure','name','Figurename'),'color','k').

You can still do

togglefig Fred;

(With the semicolon to suppress the display of the handle.)

Alternatively, I leave it to the user to modify the function to delete the function handle if not requested as an output.

Cheers!

10 Jan 2008 Brett Shoelson

ALL: To further follow up on John's comment, note that John correctly pointed out (via private email) that suppressing the handle if nargout == 0 does not disallow the reference-by-name functionality I wanted. I'm reposting the file to implement the modest change he suggested.

10 Jan 2008 John D'Errico

All fixed.

17 Oct 2008 victor de lafuente

Thanks for this useful function.

I suggest a couple of extra lines to generate a unique figure number, so that other *.m files still using figure(1), figure(2), etc. do not overwrite the figures created by togglefig. It will pause for 10 ms each time a new figure is created, but this can be modified according to your computer platform (see 'pause' help).

...
if isempty(fig)
   pause(.01)
   FigNum = round(sum(1000*clock));
   fig = figure(FigNum);
   set(fig,'numbertitle','off',...
           'name',name);shg;
else
...

23 Jun 2009 Danilo

Great idea and nice implementation !!!! I've just downloaded and I'm already loving it... :-)

Just a small bug found, when called without any arguments, I get:

>> togglefig
??? Input argument "clearfig" is undefined.

The problem is solved by adding the line

 clearfig = 0;

just before

elseif nargin == 1

Cheers!

26 Jun 2009 Brett Shoelson

Danilo, thanks for finding the bug (and for suggesting a fix). New version addresses this!

Please login to add a comment or rating.
Updates
10 Jan 2008

Minor modification to suppress handle generation if no output is requested.

06 Jan 2009

I added a new (optional) input argument that triggers a CLF (clear-figure) command.

26 Jun 2009

Fixes a bug when togglefig is called with no input arguments. Thanks to Danilo for pointing it out!

Tag Activity for this File
Tag Applied By Date/Time
toggle figure algorithm development Brett Shoelson 22 Oct 2008 09:42:02
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com