{"id":3831,"date":"2012-09-14T09:00:41","date_gmt":"2012-09-14T13:00:41","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=3831"},"modified":"2016-05-10T08:40:10","modified_gmt":"2016-05-10T12:40:10","slug":"uigetvariables-a-variable-selector-widget-for-building-guis-and-apps","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2012\/09\/14\/uigetvariables-a-variable-selector-widget-for-building-guis-and-apps\/","title":{"rendered":"UIGETVARIABLES: A variable selector widget for building GUIs and apps"},"content":{"rendered":"<div class=\"content\">\r\n\r\n<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/37679-uigetvariables--dialog-to-pass-variables-from-workspace-into-gui\">UIGETVARIABLES,<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/3777\">Scott Hirsch<\/a>.\r\n\r\nScott Hirsch (remember Scott? One of the original authors of this blog?) recently shared with me--and with the File Exchange\r\ncommunity at large--a function that I found extremely useful, and that others will want to consider as well.\r\n\r\n<strong>First, a quick bit of background...<\/strong>\r\n\r\nAs of this writing, the newest release of MATLAB (R2012b) is \"live,\" and downloadable by anyone with a current license. R2012b\r\nwas a <a href=\"https:\/\/www.mathworks.com\/products\/new_products\/latest_features.html?s_tid=hp_spot_r2012b_0912\">major release<\/a> for us, one that incorporates a new desktop design and many great new features. One of my favorites is the new MATLAB Toolstrip:\r\n\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/ToolstripScreenshot.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\nAnd as something of a GUI afficionado, I'm particularly fond of the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/creating_guis\/apps-overview.html\">\"Apps\" tab<\/a>, which not only provides ready access to a variety of MathWorks-provided GUI tools, but which allows you to customize your\r\nMATLAB environment by incorporating your favorite GUI tools. Not one to squander an opportunity like this, I quickly jumped\r\non the bandwagon to share as apps some of <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?utf8=%E2%9C%93&amp;sort=downloads_desc&amp;term=shoelson+image+type%3A%22app%22\">my own image-processing-centric GUIs<\/a>. (Installation is a breeze, by the way; just click the \"mlappinstall\" installer files packaged with the GUIs, and the GUIs\r\nautomagically appear in your list of apps.)\r\n\r\n<strong>So where does UIGETVARIABLES come into the picture?<\/strong>\r\n\r\nLots of users have downloaded, and hopefully found utility in, my <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23697-image-morphology\">MorphTool<\/a> GUI for interactively performing image morphology (dilations, erosions, closings, openings, etc.) But as originally written,\r\nMorpTool 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\r\nspecify the image in the call to the function. Clearly, to make it work as a Toolstrip-accessible application, I had to modify\r\nthe code to allow the user to import an image.\r\n\r\nUsing <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012b\/matlab\/ref\/uigetfile.html\"><tt>uigetfile<\/tt><\/a>, I could easily facilitate loading an image from a file. But what about loading an image that already existed as a variable\r\nin the MATLAB workspace? While I might have been able to cobble something together to do that, Scott's file saved me from\r\nhaving to do so! The syntax:\r\n\r\n<tt>varout = uigetvariables(prompts,intro,types,ndimensions)<\/tt>\r\n\r\nlets me specify the <em>type<\/em> and <em>size<\/em> of data that can be selected, and a GUI presenting the user with valid options is automatically created. For instance, if\r\nI wanted to prompt the user to pick a scalar, a vector, and a matrix, I need only write:\r\n<pre>tvar = uigetvariables({'Pick a scalar:','Pick a vector:','Pick a matrix:'},[],[],[0 1 2]);<\/pre>\r\nand <tt>uigetvariables<\/tt> will create a custom selection GUI that facilitates the selection:\r\n\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/uigetvariablesSceenshot1.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\nI can also specify more advanced criteria for selections; Scott's clever utility will tell the user which variables are acceptable,\r\nand will indicate when no workspace variables fit the bill.\r\n\r\nThe net result: it was really easy to modify my code to allow the user to pick an MxN or MxNx3 (RGB) image:\r\n<pre>validateimage = @(im) (isnumeric(im) || islogical(im)) &amp;&amp;(ndims(im)==2 &amp;&amp; ~any(size(im)==2) || (ndims(im)==3&amp;&amp;size(im,3)==3));\r\nimg = uigetvariables(prompts,inputstring,validateimage);<\/pre>\r\nVery nice, Scott!\r\n\r\nAs always, <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=3831#respond\">comments to this blog post<\/a> are welcome. Or leave a comment for Scott <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/37679-uigetvariables--dialog-to-pass-variables-from-workspace-into-gui#comments\">here<\/a>.\r\n\r\n<script type=\"text\/javascript\">\/\/ <![CDATA[\r\nfunction grabCode_b0c785f75f194808852fbe192ffd8b0e() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='b0c785f75f194808852fbe192ffd8b0e ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' b0c785f75f194808852fbe192ffd8b0e';\r\n\r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n\r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Brett Shoelson';\r\n        copyright = 'Copyright 2012 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('\r\n\r\n\r\n\r\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\r\n\r\n\r\n\r\n\\n');\r\n\r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }\r\n\/\/ ]]><\/script>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\"><a><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code\r\n<\/span><\/a><\/p>\r\n<noscript><\/noscript>Published with MATLAB\u00ae 8.0\r\n\r\n<\/div>\r\n&nbsp;","protected":false},"excerpt":{"rendered":"<p>\r\n\r\nBrett's Pick this week is the UIGETVARIABLES, by Scott Hirsch.\r\n\r\nScott Hirsch (remember Scott? One of the original authors of this blog?) recently shared with me--and with the File... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/09\/14\/uigetvariables-a-variable-selector-widget-for-building-guis-and-apps\/\">read more >><\/a><\/p>","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3831"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=3831"}],"version-history":[{"count":16,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3831\/revisions"}],"predecessor-version":[{"id":6924,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3831\/revisions\/6924"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=3831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=3831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=3831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}