{"id":2115,"date":"2008-02-15T15:35:38","date_gmt":"2008-02-15T20:35:38","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2008\/02\/15\/gui-layout-part-1\/"},"modified":"2016-05-10T15:02:32","modified_gmt":"2016-05-10T19:02:32","slug":"gui-layout-part-1","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2008\/02\/15\/gui-layout-part-1\/","title":{"rendered":"GUI Layout (Part 1)"},"content":{"rendered":"<div class=\"content\">\n<p>This is a post from Jiro, one of our guest bloggers:<\/p>\n<p>One of my favorite things to do in MATLAB is to build GUIs. This is Part 1 of 3 Pick of the Week entries on GUIs.<\/p>\n<p>When I build GUIs, I think about not only the functionalities, but also the aesthetics of them. I like buttons and sliders to be a certain size, and I want them to resize appropriately when I change the size of the figure window.<\/p>\n<p>In MATLAB GUIs, the handle graphics objects have a <tt>Units<\/tt> property which specifies the units to use for positioning the objects. Using the <tt>Normalized<\/tt> option allows the objects to resize proportionally with the figure. In order to keep some components from becoming oversized or undersized, I often define a <tt>ResizeFcn<\/tt> for the figure to adjust the positions of the objects. For example, take a look at the following example:<\/p>\n<p>First, here's the GUI at a particular size:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/figure1.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<p>Next, here's the same GUI after resizing it to a smaller size:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/figure2.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<p><i>Notice how the size of the push button remained the same and the slider bar and the axes adjusted their size appropriately to fill the remaining space.<\/i><\/p>\n<p>&nbsp;<\/p>\n<pre> function myResizeFcn(varargin)\r\n   % Figure resize callback\r\n   %   Adjust object positions so that they maintain appropriate\r\n   %   proportions<\/pre>\n<pre>   fP = get(figH, 'Position');<\/pre>\n<pre>   set(sliderH, 'Position', [10       , 10, fP(3)-130, 25       ]);\r\n   set(buttonH, 'Position', [fP(3)-110, 10, 100      , 25       ]);\r\n   set(axesH  , 'Position', [50       , 85, fP(3)-100, fP(4)-130]);<\/pre>\n<pre> end<\/pre>\n<p>Every time the figure is resized, it determines the current figure size and then positions the objects accordingly.<\/p>\n<p>This has worked nicely for me, but I always find things out in the File Exchange bucket that would make my life easier. Stay tuned for <a href=\"https:\/\/blogs.mathworks.com\/pick\/2008\/02\/22\/gui-layout-part-2\/\">Part 2<\/a> and Part 3, in which I will introduce a couple of entries that I found useful for GUI layouts.<\/p>\n<p><b>Comments?<\/b><\/p>\n<p>Tell us about some of the things you do to make your GUI nicer (both functionally and aesthetically).<\/p>\n<p><i>Written by Jiro Doke, Senior Applications Engineer, The MathWorks, Inc.<\/i><\/p>\n<p><script>\/\/ <![CDATA[\nfunction grabCode_e591db32654e4ae1a2fd811e0d2062eb() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='e591db32654e4ae1a2fd811e0d2062eb ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' e591db32654e4ae1a2fd811e0d2062eb';\n\n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n\n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        author = 'Jiro Doke';\n        copyright = 'Copyright 2008 The Mathworks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n\n\n\n\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>\n\n\n\n\n\n\n\\n');\n\n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }\n\/\/ ]]><\/script><\/p>\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<br \/>\nthe MATLAB code<br \/>\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>Published with MATLAB\u00ae 7.5<\/p>\n<\/div>\n<p><!--\ne591db32654e4ae1a2fd811e0d2062eb ##### SOURCE BEGIN #####\n%%\n% One of my favorite things to do in MATLAB is to build GUIs. This is Part\n% 1 of 3 Pick of the Week entries on GUIs.\n%\n% When I build GUIs, I think about not only the functionalities, but also\n% the aesthetics of them. I like buttons and sliders to be a certain size,\n% and I want them to resize appropriately when I change the size of the\n% figure window.\n%\n% In MATLAB GUIs, the handle graphics objects have a |Units| property which\n% specifies the units to use for positioning the objects. Using the\n% |Normalized| option allows the objects to resize proportionally with the\n% figure. In order to keep some components from becoming oversized or\n% undersized, I often define a |ResizeFcn| for the figure to adjust the\n% positions of the objects. For example, take a look at the following\n% example:\n%\n% First, here's the GUI at a particular size:\n%\n% <\n<figure1.png>>\n%\n% Next, here's the same GUI after resizing it to a smaller size:\n%\n% <\n<figure2.png>>\n%\n% _Notice how the size of the push button remained the same and the slider\n% bar and the axes adjusted their size appropriately to fill the remaining\n% space. See an <https:\/\/www.mathworks.com\/matlabcentral\/files\/18769\/myGUI_screenshot.gif Animated GIF> of it._\n%\n% You can see how I did it here:\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=18769&#038;objectType=FILE Dynamic GUI Layout>.\n%\n% The key part of the function is this:\n%\n%   function myResizeFcn(varargin)\n%     % Figure resize callback\n%     %   Adjust object positions so that they maintain appropriate\n%     %   proportions\n%\n%     fP = get(figH, 'Position');\n%\n%     set(sliderH, 'Position', [10       , 10, fP(3)-130, 25       ]);\n%     set(buttonH, 'Position', [fP(3)-110, 10, 100      , 25       ]);\n%     set(axesH  , 'Position', [50       , 85, fP(3)-100, fP(4)-130]);\n%\n%   end\n%\n% Every time the figure is resized, it determines the current figure size\n% and then positions the objects accordingly.\n%\n% This has worked nicely for me, but I always find things out in the File\n% Exchange bucket that would make my life easier. Stay tuned for Part 2 and\n% Part 3, in which I will introduce a couple of entries that I found useful\n% for GUI layouts.\n%\n% *Comments?*\n%\n% Tell us about some of the things you do to make your GUI nicer (both\n% functionally and aesthetically).\n%\n% _Written by <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadAuthor.do?objectType=author&#038;objectId=1094142 Jiro Doke>, Senior Applications Engineer, The MathWorks, Inc._\n##### SOURCE END ##### e591db32654e4ae1a2fd811e0d2062eb\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\nThis is a post from Jiro, one of our guest bloggers:<br \/>\nOne of my favorite things to do in MATLAB is to build GUIs. This is Part 1 of 3 Pick of the Week entries on GUIs.<br \/>\nWhen I build GUIs, I think... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2008\/02\/15\/gui-layout-part-1\/\">read more >><\/a><\/p>\n","protected":false},"author":68,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2115"}],"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\/68"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2115"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2115\/revisions"}],"predecessor-version":[{"id":7652,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2115\/revisions\/7652"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}