File Exchange Pick of the Week

UIGETVARIABLES: A variable selector widget for building GUIs and apps 2

Posted by Brett Shoelson,

Brett's Pick this week is the UIGETVARIABLES, by Scott Hirsch.

Scott Hirsch (remember Scott? One of the original authors of this blog?) recently shared with me--and with the File Exchange community at large--a function that I found extremely useful, and that others will want to consider as well.

First, a quick bit of background...

As of this writing, the newest release of MATLAB (R2012b) is "live," and downloadable by anyone with a current license. R2012b was a major release for us, one that incorporates a new desktop design and many great new features. One of my favorites is the new MATLAB Toolstrip:

And as something of a GUI afficionado, I'm particularly fond of the "Apps" tab, which not only provides ready access to a variety of MathWorks-provided GUI tools, but which allows you to customize your MATLAB environment by incorporating your favorite GUI tools. Not one to squander an opportunity like this, I quickly jumped on the bandwagon to share as apps some of my own image-processing-centric GUIs. (Installation is a breeze, by the way; just click the "mlappinstall" installer files packaged with the GUIs, and the GUIs automagically appear in your list of apps.)

So where does UIGETVARIABLES come into the picture?

Lots of users have downloaded, and hopefully found utility in, my MorphTool GUI for interactively performing image morphology (dilations, erosions, closings, openings, etc.) But as originally written, MorpTool was not useful as an app because I didn't provide a way to load an image from within the GUI; instead, you had to specify the image in the call to the function. Clearly, to make it work as a Toolstrip-accessible application, I had to modify the code to allow the user to import an image.

Using uigetfile, I could easily facilitate loading an image from a file. But what about loading an image that already existed as a variable in the MATLAB workspace? While I might have been able to cobble something together to do that, Scott's file saved me from having to do so! The syntax:

varout = uigetvariables(prompts,intro,types,ndimensions)

lets me specify the type and size of data that can be selected, and a GUI presenting the user with valid options is automatically created. For instance, if I wanted to prompt the user to pick a scalar, a vector, and a matrix, I need only write:

tvar = uigetvariables({'Pick a scalar:','Pick a vector:','Pick a matrix:'},[],[],[0 1 2]);

and uigetvariables will create a custom selection GUI that facilitates the selection:

I can also specify more advanced criteria for selections; Scott's clever utility will tell the user which variables are acceptable, and will indicate when no workspace variables fit the bill.

The net result: it was really easy to modify my code to allow the user to pick an MxN or MxNx3 (RGB) image:

validateimage = @(im) (isnumeric(im) || islogical(im)) &&(ndims(im)==2 && ~any(size(im)==2) || (ndims(im)==3&&size(im,3)==3));
img = uigetvariables(prompts,inputstring,validateimage);

Very nice, Scott!

As always, comments to this blog post are welcome. Or leave a comment for Scott here.

Get the MATLAB code

Published with MATLAB® 8.0

2 CommentsOldest to Newest

Yes, Aurelien. Scott (occasionally) still writes some code. :)
Thanks for steering us back to John’s file–a previous Pick of the Week (!). I had forgotten about it.

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