<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Undocumented Matlab &#187; GUIDE</title> <atom:link href="http://undocumentedmatlab.com/blog/tag/guide/feed/" rel="self" type="application/rss+xml" /><link>http://undocumentedmatlab.com</link> <description>Charting Matlab's unsupported hidden underbelly</description> <lastBuildDate>Thu, 17 May 2012 12:01:26 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.1</generator> <item><title>Common javacomponent problems</title><link>http://undocumentedmatlab.com/blog/common-javacomponent-problems/</link> <comments>http://undocumentedmatlab.com/blog/common-javacomponent-problems/#comments</comments> <pubDate>Wed, 07 Dec 2011 18:00:38 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[GUIDE]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2607</guid> <description><![CDATA[The javacomponent function is very useful for placing Java components on-screen, but has a few quirks.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/javacomponent/' rel='bookmark' title='The javacomponent function'>The javacomponent function</a> <small>Matlab's built-in javacomponent function can be used to display Java components in Matlab application - this article details its usages and limitations...</small></li><li><a
href='http://undocumentedmatlab.com/blog/javacomponent-background-color/' rel='bookmark' title='Javacomponent background color'>Javacomponent background color</a> <small>This article explains how to align Java component background color with a Matlab color....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes-2/' rel='bookmark' title='Customizing uitree nodes &#8211; part 2'>Customizing uitree nodes &#8211; part 2</a> <small>This article shows how Matlab GUI tree nodes can be customized with checkboxes and similar controls...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>The <i><b>javacomponent</b></i> function, which I described <a
target="_blank" href="http://UndocumentedMatlab.com/blog/javacomponent/">here</a> last year, is a very important built-in Matlab function that enables placing Java components in Matlab figure GUI. Using <i><b>javacomponent</b></i> is pretty straight-forward. However, there are a few quirks that users should be aware of. In today&#8217;s article I&#8217;ll try to highlight some of them and discuss workarounds.</p><h3 id="figure-vis">Figure visibility</h3><p>Java components can only be placed onscreen when their containing Matlab figure has been made visible. This means that calls to <i><b>javacomponent</b></i> cannot be placed at the GUIDE-created *_OpeningFcn() function, because that function is invoked <i>before</i> the figure window is made visible.</p><p>Of course, we can always force the figure to become visible by setting the figure&#8217;s <b>Visible</b> property to <code>'on'</code> in this function, before calling our <i><b>javacomponent</b></i>s. But IMHO, a better option would be to simply place the <i><b>javacomponent</b></i>s in the corresponding GUIDE-created *_OutputFcn() function, which is invoked <i>after</i> the figure window is made visible.</p><h3 id="parent-vis">Auto-hiding with parent container</h3><p>Java components are not automatically hidden with their ancestor container panel. This is also the root cause of the failure of Java components to disappear when switching tabs in a <i><b>uitab</b></i>.</p><p>One simple workaround for this that I often use is to link the <b>Visible</b> properties of the <i><b>javacomponent</b></i> container and the parent container:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jButton = javax.<span style="">swing</span>.<span style="">JButton</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'click me!'</span><span style="color: #080;">&#41;</span>;
<span style="color: #080;">&#91;</span>jhButton, hContainer<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span>jButton, <span style="color: #080;">&#91;</span><span style="color: #33f;">100</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">60</span>,<span style="color: #33f;">30</span><span style="color: #080;">&#93;</span>, hParent<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">setappdata</span><span style="color: #080;">&#40;</span>hParent, <span style="color:#A020F0;">'linked_props__'</span>, linkprop<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span>hParent,hContainer<span style="color: #080;">&#93;</span>,<span style="color:#A020F0;">'Visible'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>This has indeed been fixed in R2010b. If you ask me, this should have been standard behavior of <i><b>javacomponent</b></i> since the very beginning&#8230;</p><p>Although there is no need for the workaround in R2010b onward, I usually keep the workaround because it doesn&#8217;t hurt and enables backward compatibility for users who may have an older Matlab release.</p><h3 id="parent-types">Possible parent container types</h3><p><i><b>javacomponent</b></i> accepts parent handles that are figures, toolbars, <i><b>uipanel</b></i>s, or <a
target="_blank" href="http://UndocumentedMatlab.com/blog/matlab-layout-managers-uicontainer-and-relatives"><i><b>uicontainer</b></i>s</a> (some of these are not documented as possible parents in some Matlab releases, but they are). Since R2008a, parents of type <a
target="_blank" href="http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool/"><i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i></a> can also be used. Unfortunately, frames are not <i><b>uicontainer</b></i>s and, therefore, cannot be used as <i><b>javacomponent</b></i> parents.</p><p>Note: Due to a bug in R2007a, <i><b>javacomponent</b></i>s cannot be added to <i><b>uicontainer</b></i>s, since <i>javacomponent.m</i> checks if <code>isa(hParent,'uicontainer')</code> (and similarly for <code>'uiflowcontainer', 'uigridcontainer'</code>), instead of <code>isa(hParent,'hg.uicontainer')</code> (and similarly for the others). If we modify <i>javacomponent.m</i> accordingly (add &#8220;hg.&#8221; in lines 98-100), this bug will be fixed. Since R2007b, <code>isa(…,'hg.uicontainer')</code> is equivalent to <code>isa(…,'uicontainer')</code>, so this fix is unnecessary.</p><h3 id="inputs">Input parameters</h3><p>Unlike many other Matlab functions, <i><b>javacomponent</b></i> does not accept optional parameter-value (P-V) pairs. If we want to customize the appearance of the Java component, it is better to create it , customize it, and only then to present it onscreen using <i><b>javacomponent</b></i>. If we first present the component and then customize it, there might be all sorts of undesirable flicker effects while the component is changing its properties.</p><p>One of the parameters that unfortunately cannot be customized before calling <i><b>javacomponent</b></i> is the component&#8217;s position onscreen. <i><b>javacomponent</b></i> only accepts a position vector in pixel units. To modify the component to use normalized units, we need to modify the container&#8217;s properties:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jButton = javax.<span style="">swing</span>.<span style="">JButton</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'click me!'</span><span style="color: #080;">&#41;</span>;
<span style="color: #080;">&#91;</span>jhButton, hContainer<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span>jButton, <span style="color: #080;">&#91;</span><span style="color: #33f;">100</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">60</span>,<span style="color: #33f;">30</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hContainer, <span style="color:#A020F0;">'Units'</span>,<span style="color:#A020F0;">'norm'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Similarly, we can set the container&#8217;s <b>UserData</b> and <b>ApplicationData</b> only after the call to <i><b>javacomponent</b></i>.</p><h3 id="bg-color">Background color</h3><p>The default background color of <i><b>javacomponent</b></i>s is a slightly different shade of gray than the default <i><b>uicontrol</b></i> background color. Please refer to my recent <a
target="_blank" href="http://UndocumentedMatlab.com/blog/javacomponent-background-color/">article</a> for a detailed discussion of this issue.</p><h3 id="alignment">Vertical alignment</h3><p>Java components are slightly mis-aligned vertically with combo-box (<b>Style</b>=&#8217;popup&#8217;) <i><b>uicontrol</b></i>s, even when positioned using the same Y position. This is actually due to an apparent bug in Matlab&#8217;s implementation of the combo-box <i><b>uicontrol</b></i>, and not in the Java component&#8217;s: Apparently, the Matlab control does not obey its specified height and uses some other default height.</p><p>If we place <i><b>javacomponent</b></i>s side-by-side with a regular Matlab popup <i><b>uicontrol</b></i>s and give them all the same Y position, we can see this mis-alignment. It is only a few pixels, but the effect is visible and disturbing. To fix it, we need to add a small offset to the <i><b>javacomponent</b></i>&#8216;s container&#8217;s <b>Position</b> property to make both the initial Y position slightly lower, and the height value slightly higher than the values for the corresponding Matlab combo-box control.</p><h3 id="callbacks">Callbacks</h3><p>Unlike Matlab <i><b>uicontrol</b></i> callbacks, Java callbacks are activated even when the affected value does not change. Therefore, setting a value in the component&#8217;s callback could well cause an infinite loop of invoked callbacks.</p><p>Matlab programmers often use the practice of modifying component value within the callback function, as a means of fixing user-entered values to be within a certain data range, or in order to format the displayed value. This is relatively harmless in Matlab (if done correctly) since updating a Matlab <i><b>uicontrol</b></i>&#8216;s value to the current value does not retrigger the callback. But since this is not the case with Java callbacks, users should beware not to use the same practice there. In cases where this cannot be avoided, users should at least implement some sort of <a
target="_blank" href="http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy/">callback re-entrancy prevention logic</a>.</p><h3 id="EDT">EDT</h3><p>Java components typically need to use the independent Java Event processing Thread (EDT). EDT is very important for Matlab GUI, as explained in <a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/">this article</a>. Failure to use EDT properly in Matlab can lead to unexpected GUI behavior and even Matlab hangs or crashes.</p><p>If the <i><b>javacomponent</b></i> function is called in a very specific syntax format where the first input arg is a string (the name of the Java class to be created), then the newly-created component is placed on the EDT. However, this is not the normal manner in which <i><b>javacomponent</b></i> is used: A much more typical use-case is where <i><b>javacomponent</b></i> is passed a reference handle to a previously-created Java component. In such cases, it is the user&#8217;s responsibility to place the component on the EDT. Until R2008a this should be done using the <i><b>awtcreate</b></i> function; since R2008b, we can use the much simpler <i><b>javaObjectEDT</b></i> function (<i><b>javaObjectEDT</b></i> actually existed since R2008a, but was buggy on that release so I suggest using it only starting in R2008b; it became documented starting in R2009a). In fact, modern <i><b>javacomponent</b></i> saves us the trouble, by automatically using <i><b>javaObjectEDT</b></i> to auto-delegate the component onto the EDT, even if we happen to have created it on the MT.</p><p>The paragraph above may lead us to believe that we only need to worry about EDT in R2008a and earlier. Unfortunately, this is not the case. Modern GUI requires using many sub-components, that are attached to their main component (e.g., CellRenderers and CellEditors). <i><b>javacomponent</b></i> only bothers to place the main component on the EDT &#8211; not any of the sub-components. We need to handle these separately. Liberally auto-delegating components to the EDT seems like a safe and painless habit to have.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/javacomponent/' rel='bookmark' title='The javacomponent function'>The javacomponent function</a> <small>Matlab's built-in javacomponent function can be used to display Java components in Matlab application - this article details its usages and limitations...</small></li><li><a
href='http://undocumentedmatlab.com/blog/javacomponent-background-color/' rel='bookmark' title='Javacomponent background color'>Javacomponent background color</a> <small>This article explains how to align Java component background color with a Matlab color....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes-2/' rel='bookmark' title='Customizing uitree nodes &#8211; part 2'>Customizing uitree nodes &#8211; part 2</a> <small>This article shows how Matlab GUI tree nodes can be customized with checkboxes and similar controls...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/common-javacomponent-problems/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>handle2struct, struct2handle &amp; Matlab 8.0</title><link>http://undocumentedmatlab.com/blog/handle2struct-struct2handle-and-matlab-8/</link> <comments>http://undocumentedmatlab.com/blog/handle2struct-struct2handle-and-matlab-8/#comments</comments> <pubDate>Wed, 29 Dec 2010 18:00:56 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[GUIDE]]></category> <category><![CDATA[HG2]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Menubar]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[Toolbar]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2023</guid> <description><![CDATA[This article explains how we can use a couple of undocumented functions in Matlab GUI, and what we can learn from this about Matlab's future.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/fig-files-format/' rel='bookmark' title='FIG files format'>FIG files format</a> <small>FIG files are actually MAT files in disguise. This article explains how this can be useful in Matlab applications....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-layout-managers-uicontainer-and-relatives/' rel='bookmark' title='Matlab layout managers: uicontainer and relatives'>Matlab layout managers: uicontainer and relatives</a> <small>Matlab contains a few undocumented GUI layout managers, which greatly facilitate handling GUI components in dynamically-changing figures....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/' rel='bookmark' title='getundoc &#8211; get undocumented object properties'>getundoc &#8211; get undocumented object properties</a> <small>getundoc is a very simple utility that displays the hidden (undocumented) properties of a specified handle object....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Last week I explained that <a
target="_blank" href="http://undocumentedmatlab.com/blog/fig-files-format/">FIG files are simply MAT files in disguise</a>. Today, we look under the hood of Matlab&#8217;s <i><b>hgsave</b></i> function, which is used to save FIG files. We shall see that this is both useful and illuminating vis-a-vis Matlab&#8217;s future.</p><h3 id="handle2struct">handle2struct</h3><p>Under the hood, <i><b>hgsave</b></i> uses the <a
target="_blank" href="http://undocumentedmatlab.com/blog/legend-semi-documented-feature/#Semi-documented">semi-documented</a> built-in <i><b>handle2struct</b></i> function to convert the figure handle into a Matlab <i><b>struct</b></i> that is then stored with a simple <i><b>save</b></i> (the same function that saves MAT files) function call.</p><p>The fact that <i><b>handle2struct</b></i> is semi-documented means that the function is explained in a help comment (which can be seen via the <i><b>help</b></i> command), that is nonetheless not part of the official doc sections. It is an unsupported feature originally intended only for internal Matlab use (which of course doesn&#8217;t mean we can&#8217;t use it).</p><p><i><b>handle2struct</b></i> merits a dedicated mention, since I can envision several use-cases for storing only a specific GUI handle (for example, a <i><b>uipanel</b></i>, a specific graph, or a set of GUI controls&#8217; state). In this case, all we need to do is to call <i><b>handle2struct</b></i> with the requested parent handle, then <i><b>save</b></i> the returned structure. So simple, so powerful. <i><b>handle2struct</b></i> automatically returns all the non-default property information, recursively in all the handle&#8217;s children.</p><p>Note that features that are not properties of displayed handles (camera position, 3D rotation/pan/zoom states, annotations, axes-linking etc.) are not processed by <i><b>handle2struct</b></i>. For storing the states of these features, you need to use some specific handling &#8211; see the code within %matlabroot%\toolbox\matlab\graphics\private\hgsaveStructDbl.m for details. Basically, hgsaveStructDbl.m reads the state of all these features and temporarily stores them in the base handle&#8217;s <strong>ApplicationData</strong> property; <i><b>handle2struct</b></i> then reads them as any other regular handle property data, and then hgsaveStructDbl.m clears the temporary data from the handle&#8217;s <strong>ApplicationData</strong>. We can use the same trick for any other application state, of course.</p><h3 id="struct2handle">struct2handle</h3><p><i><b>handle2struct</b></i> has a reverse function &#8211; the semi-documented <i><b>struct2handle</b></i>. I use it for creating dynamic preference panels: As in Matlab&#8217;s Preferences window, I have a list of preference topics and a set of corresponding options panels. In my case, it was easy to design each panel as a separate FIG file using GUIDE. In run-time, I simply load the relevant panel from its FIG file as described above, and place it onscreen in a dedicated <i><b>uipanel</b></i> using <i><b>struct2handle</b></i>. This enables very easy maintenance of preference panels, without sacrificing any functionality.</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="Matlab's preferences panels" src="http://UndocumentedMatlab.com/images/FIG_MAT.png" title="Matlab's preferences panels" width="450" height="274" /><p
class="wp-caption-text">Matlab's preferences panels</p></div></center></p><p>Figure menus and toolbars are not normally stored by <i><b>hgsave</b></i>, unless you use the optional &#8216;all&#8217; parameter (and correspondingly in <i><b>hgload</b></i>, if you choose to use it). <i><b>handle2struct</b></i> and <i><b>handle2struct</b></i> accept the same optional &#8216;all&#8217; parameter as <i><b>hgsave</b></i> and <i><b>hgload</b></i>. Unfortunately, a warning message indicates that this option will be discontinued in some future Matlab version.</p><p>Which brings us to our final topic for today:</p><h3 id="Matlab8">Matlab 8: Boldly going where no FIG has gone before&#8230;</h3><p>Remember my post earlier this year about the <a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-hg2/">new HG2 mechanism</a>? I speculated that when MathWorks decides to release HG2, it will define this as a major Matlab release and label it Matlab 8.0.</p><p>The source code in hgsave.m appears to confirm my speculation. Here is the relevant code section (slightly edited for clarity), which speaks for itself:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Decide which save code path to use</span>
<span style="color: #0000FF;">if</span> ~feature<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'HGUsingMatlabClasses'</span><span style="color: #080;">&#41;</span>   <span style="color: #228B22;">% &lt;== existing HG</span>
    <span style="color: #228B22;">% Warn if user passed in 'all' flag</span>
    <span style="color: #0000FF;">if</span> SaveAll
        <span style="color: #0000FF;">warning</span><span style="color: #080;">&#40;</span> <span style="color:#A020F0;">'MATLAB:hgsave:DeprecatedOption'</span>, <span style="color: #F0F;">...</span>
            <span style="color:#A020F0;">'The '</span><span style="color:#A020F0;">'all'</span><span style="color:#A020F0;">' option to hgsave will be removed in a future release.'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span>
    hgS = hgsaveStructDbl<span style="color: #080;">&#40;</span>h, SaveAll<span style="color: #080;">&#41;</span>;
    SaveVer = <span style="color:#A020F0;">'070000'</span>;
    SaveOldFig = <span style="color: #0000FF;">true</span>;
&nbsp;
<span style="color: #0000FF;">else</span>   <span style="color: #228B22;">% &lt;== HG2</span>
&nbsp;
    <span style="color: #228B22;">% Warn if user passed in 'all' flag</span>
    <span style="color: #0000FF;">if</span> SaveAll
        <span style="color: #0000FF;">warning</span><span style="color: #080;">&#40;</span> <span style="color:#A020F0;">'MATLAB:hgsave:DeprecatedOption'</span>, <span style="color: #F0F;">...</span>
            <span style="color:#A020F0;">'The '</span><span style="color:#A020F0;">'all'</span><span style="color:#A020F0;">' option to hgsave has been removed.'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span>
    <span style="color: #0000FF;">if</span> SaveOldFig
        hgS = hgsaveStructClass<span style="color: #080;">&#40;</span>h<span style="color: #080;">&#41;</span>;
        SaveVer = <span style="color:#A020F0;">'080000'</span>;
    <span style="color: #0000FF;">else</span>
        hgO = hgsaveObject<span style="color: #080;">&#40;</span>h<span style="color: #080;">&#41;</span>;
        SaveVer = <span style="color:#A020F0;">'080000'</span>;
    <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Revision encoded as 2 digits for major revision,</span>
<span style="color: #228B22;">% 2 digits for minor revision, and 2 digits for</span>
<span style="color: #228B22;">% patch revision.  This is the minimum revision</span>
<span style="color: #228B22;">% required to fully support the file format.</span>
<span style="color: #228B22;">% e.g. 070000 means 7.0.0</span></pre></div></div><p>As can be seen, when Matlab starts using HG2 (perhaps in 2011?), the top-level structure node will be called &#8220;hgS_080000&#8243;, indicating Matlab 8.0. QED.</p><p>As a side-note, note that in HG2/Matlab8, although the comment about using &#8216;all&#8217; indicates that it has been removed, in practice it is still accepted (although not being used). This will enable your code to be backward-compatible whenever HG2 launches, and future-compatible today.</p><p>Have you used <i><b>handle2struct</b></i> or <i><b>struct2handle</b></i>? If so, please share your experience in a <a
href="http://undocumentedmatlab.com/blog/handle2struct-struct2handle-and-matlab-8/#respond">comment</a>.</p><p><em>Let the upcoming 2011 be a year filled with revelations, announcements and fulfillment! Happy New Year everybody!</em></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/fig-files-format/' rel='bookmark' title='FIG files format'>FIG files format</a> <small>FIG files are actually MAT files in disguise. This article explains how this can be useful in Matlab applications....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-layout-managers-uicontainer-and-relatives/' rel='bookmark' title='Matlab layout managers: uicontainer and relatives'>Matlab layout managers: uicontainer and relatives</a> <small>Matlab contains a few undocumented GUI layout managers, which greatly facilitate handling GUI components in dynamically-changing figures....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/' rel='bookmark' title='getundoc &#8211; get undocumented object properties'>getundoc &#8211; get undocumented object properties</a> <small>getundoc is a very simple utility that displays the hidden (undocumented) properties of a specified handle object....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/handle2struct-struct2handle-and-matlab-8/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> <item><title>Matlab layout managers: uicontainer and relatives</title><link>http://undocumentedmatlab.com/blog/matlab-layout-managers-uicontainer-and-relatives/</link> <comments>http://undocumentedmatlab.com/blog/matlab-layout-managers-uicontainer-and-relatives/#comments</comments> <pubDate>Wed, 09 Jun 2010 22:03:32 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[GUIDE]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1593</guid> <description><![CDATA[Matlab contains a few undocumented GUI layout managers, which greatly facilitate handling GUI components in dynamically-changing figures.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives/' rel='bookmark' title='Tab panels &#8211; uitab and relatives'>Tab panels &#8211; uitab and relatives</a> <small>This article describes several undocumented Matlab functions that support tab-panels...</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-the-matlab-desktop-layout-programmatically/' rel='bookmark' title='Setting the Matlab desktop layout programmatically'>Setting the Matlab desktop layout programmatically</a> <small>The Matlab desktop enables saving and switching layouts using the main menu. This post shows how to do so programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/handle2struct-struct2handle-and-matlab-8/' rel='bookmark' title='handle2struct, struct2handle &amp; Matlab 8.0'>handle2struct, struct2handle &#038; Matlab 8.0</a> <small>This article explains how we can use a couple of undocumented functions in Matlab GUI, and what we can learn from this about Matlab's future....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>When designing Matlab applications, we can either use Matlab&#8217;s designer (<a
target="_blank" href="http://undocumentedmatlab.com/blog/guide-customization/"><i><b>guide</b></i></a>), or manually position each GUI component programmatically, using its <strong>Position</strong> property. Matlab lacks the layout managers so common in Java, that enable easy relative component positioning, taking into account dynamic container size, components spacing weights etc. Of course, we can always trap the container&#8217;s <strong>ResizeFcn</strong> callback to update our layout, but doing so is one royal pain in the so-and-so&#8230;</p><p>Luckily, there is (of course) an undocumented solution to this problem, and at <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/279442#736318">the public&#8217;s demand</a> I will detail it below. It doesn&#8217;t solve all layout-management needs, but it goes a long way. Most importantly, it uses <strong>pure Matlab</strong> &#8211; no Java knowledge whatsoever is needed.</p><h3 id="uicontainer"><i>uicontainer</i></h3><p>Matlab&#8217;s uicontainer family (<i><b>uicontainer</b></i>, <i><b>uiflowcontainer</b></i> and <i><b>uigridcontainer</b></i>) consists of container objects that enable customizable layout management of contained components. Uicontainers can contain any Matlab component that may have a <i><b>uipanel</b></i> handle as a <b>Parent</b> property. This includes uicontrols, plot axes etc., as well as other uicontainers.</p><p>The basic <i><b>uicontainer</b></i> object appears to be little more than a transparent container for contained objects. It can be used interchangeably with <i><b>uipanel</b></i>, which appears to be a specialized type of a <i><b>uicontainer</b></i>. Indeed, as MathWorks <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-OQBBH/">notes</a>:<br
/> <q>The UICONTAINER function is undocumented, and is not intended for direct use. The UIPANEL function should be used instead, as it provides more functionality.</q></p><p>In some cases, Matlab itself uses <i><b>uicontainer</b></i> instead of <i><b>uipanel</b></i>: for example, ActiveX controls are enclosed within transparent <i><b>uicontrol</b></i> objects when added to a figure:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #080;">&#91;</span>hActivex,hContainer<span style="color: #080;">&#93;</span> = actxcontrol<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'OWC11.Spreadsheet.11'</span><span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hContainer,<span style="color:#A020F0;">'Type'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
uicontainer</pre></div></div><p><i><b>uicontainer</b></i> objects are not very customizable. For example, unlike <i><b>uipanel</b></i>s, <i><b>uicontainer</b></i>s have no titles or borders. We would therefore usually prefer to use <i><b>uipanel</b></i>s, as Mathworks suggested above. An exception to this rule is a case where we need to derive our own customized container class. An example of this is found in %matlabroot%/toolbox/matlab/uitools/@uitools/@uitab/schema.m, which derives <i><b>uicontainer</b></i> to create a <i><b>uitab</b></i> container (which will be described in a future article).</p><p>Relatives of <i><b>uicontainer</b></i> are more useful in general: <i><b>uiflowcontainer</b></i>s and <i><b>uigridcontainer</b></i>s act similarly to Java&#8217;s layout managers – specifically, <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/layout/flow.html">FlowLayout</a> and <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/layout/grid.html">GridLayout</a>. I expect to see additional layout managers incarnated within Matlab <i><b>uicontainer</b></i>s in future Matlab versions (perhaps in the R2010b pre-release that came out today &#8211; I can&#8217;t wait to see&#8230;).</p><h3 id="uiflowcontainer"><i>uiflowcontainer</i></h3><p><i><b>uiflowcontainer</b></i> is a <i><b>uicontainer</b></i> that enables adding <i><b>uicontrol</b></i>s to the container without specifying an exact position as would be required for <i><b>uicontainer</b></i> or <i><b>uipanel</b></i> (actually, positions may be specified, but they are simply ignored).</p><p>By default, objects are added from the top-left corner, depending on the <i><b>uiflowcontainer</b></i>&#8216;s available space and dimensions: if width > height then rightward, otherwise downward. If the container&#8217;s dimensions change, for example by resizing the figure window, then the container&#8217;s components will automatically be resized accordingly:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hc = uiflowcontainer<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Units'</span>,<span style="color:#A020F0;">'norm'</span>,<span style="color:#A020F0;">'Position'</span>,<span style="color: #080;">&#91;</span>.1,.1,.8,.8<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
h1 = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'string'</span>,<span style="color:#A020F0;">'1'</span>,<span style="color:#A020F0;">'parent'</span>,hc<span style="color: #080;">&#41;</span>;
h2 = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'string'</span>,<span style="color:#A020F0;">'2'</span>,<span style="color:#A020F0;">'parent'</span>,hc<span style="color: #080;">&#41;</span>;
h3 = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'string'</span>,<span style="color:#A020F0;">'3'</span>,<span style="color:#A020F0;">'parent'</span>,hc<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 209px"><img
alt="'Auto' - before resizing figure" src="http://UndocumentedMatlab.com/images/uiflowcontainer1.png" title="'Auto' - before resizing figure" width="199" height="178" /><p
class="wp-caption-text">'Auto' - before resizing figure</p></div><div
class="wp-caption aligncenter" style="width: 164px"><img
alt="…and after resizing" src="http://UndocumentedMatlab.com/images/uiflowcontainer2.png" title="…and after resizing" width="154" height="178" /><p
class="wp-caption-text">…and after resizing</p></div></center></p><p>The components flow direction within the container may be modified by setting the <i><b>uiflowcontainer</b></i>&#8216;s <strong>FlowDirection</strong> property from its default value of &#8216;Auto&#8217; to &#8216;AutoReverse&#8217;, &#8216;BottomUp&#8217;, &#8216;TopDown&#8217;, &#8216;LeftToRight&#8217;, or &#8216;RightToLeft&#8217;:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hc,<span style="color:#A020F0;">'FlowDirection'</span>,<span style="color:#A020F0;">'BottomUp'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hc,<span style="color:#A020F0;">'FlowDirection'</span>,<span style="color:#A020F0;">'RightToLeft'</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 164px"><img
alt="'BottomUp'" src="http://UndocumentedMatlab.com/images/uiflowcontainer3.png" title="'BottomUp'" width="154" height="178" /><p
class="wp-caption-text">'BottomUp'</p></div><div
class="wp-caption aligncenter" style="width: 164px"><img
alt="'RightToLeft'" src="http://UndocumentedMatlab.com/images/uiflowcontainer4.png" title="'RightToLeft'" width="154" height="178" /><p
class="wp-caption-text">'RightToLeft'</p></div></center></p><p>Spacing between the components and the container&#8217;s border, and between themselves, may be controlled via the <strong>Margin</strong> property. By default, <strong>Margin</strong> is set to 2 (pixels):<br
/><center><div
class="wp-caption aligncenter" style="width: 164px"><img
alt="Margin = 2 (default)" src="http://UndocumentedMatlab.com/images/uiflowcontainer3.png" title="Margin = 2 (default)" width="154" height="178" /><p
class="wp-caption-text"><strong>Margin</strong> = 2 (default)</p></div><div
class="wp-caption aligncenter" style="width: 164px"><img
alt="Margin = 10" src="http://UndocumentedMatlab.com/images/uiflowcontainer5.png" title="Margin = 10" width="154" height="178" /><p
class="wp-caption-text"><strong>Margin</strong> = 10</p></div></center></p><p>The advantage of using <i><b>uiflowcontainer</b></i> is its automatic resizing and positioning of components. Notice how we simply specified the <i><b>uiflowcontainer</b></i>&#8216;s handle as the control&#8217;s parent, and got all this functionality out-of-the-box!</p><p>If we need to use fixed-dimensions components, we can use <i><b>uicontrol</b></i>&#8216;s undocumented properties <strong>HeightLimits</strong> and <strong>WidthLimits</strong>, each of which is a 2-element numeric vector specifying the minimal and maximal allowed value for the height or width (both of these vectors are [2,Inf] by default). <i><b>uiflowcontainer</b></i> tries to accommodate the requested limits by stretching or compressing its components (we need to resize the figure for the component resizing to become visible):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>h1, <span style="color:#A020F0;">'HeightLimits'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'WidthLimits'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">30</span>,<span style="color: #33f;">30</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>h2, <span style="color:#A020F0;">'HeightLimits'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">50</span>,<span style="color: #33f;">50</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>h3, <span style="color:#A020F0;">'HeightLimits'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">2</span>,<span style="color: #0000FF;">inf</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>   <span style="color: #228B22;">% =default value</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 164px"><img
alt="WidthLimits set" src="http://UndocumentedMatlab.com/images/uiflowcontainer6.png" title="WidthLimits set" width="154" height="187" /><p
class="wp-caption-text"><strong>WidthLimits</strong> set</p></div></center></p><p>Sometimes, however, no amount of component resizing is enough to fully contain all components within the <i><b>uiflowcontainer</b></i>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>h2, <span style="color:#A020F0;">'WidthLimits'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">150</span>,<span style="color: #33f;">150</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>h3, <span style="color:#A020F0;">'HeightLimits'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">50</span>,<span style="color: #33f;">50</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>  <span style="color: #228B22;">% resize figure to see effect</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 165px"><img
alt="WidthLimits set" src="http://UndocumentedMatlab.com/images/uiflowcontainer7.png" title="WidthLimits set" width="155" height="189" /><p
class="wp-caption-text"><strong>WidthLimits</strong> set</p></div></center></p><p>Note: uiflowcontainer normally ignores the specified limits if they would cause the component to stretch beyond the container boundaries. This happens unless the limits are identical (as in the preceding example), which informs <i><b>uiflowcontainer</b></i> that it has no judgment in the component&#8217;s dimensions.</p><p>In more complex cases, consider coding your own customized class deriving from <i><b>uiflowcontainer</b></i>. An example for such a customization can be seen in %matlabroot%/toolbox/matlab/uitools/@uitools/@uitabgroup/schema.m, which derives <i><b>uiflowcontainer</b></i> to create a <i><b>uitabgroup</b></i> container.</p><p>Components within a <i><b>uiflowcontainer</b></i> are ordered according to the order they were added to the container. This order can be modified by rearranging the handles in the container&#8217;s <strong>Children</strong> property, or by using the uistack function which does the same. Note that a side-effect of this is that the components dimensions are re-normalized:<br
/><center><div
class="wp-caption aligncenter" style="width: 165px"><img
alt="uistack(h2,'top')" src="http://UndocumentedMatlab.com/images/uiflowcontainer9.png" title="uistack(h2,'top')" width="155" height="189" /><p
class="wp-caption-text"><i><b>uistack</b></i>(h2,'top')</p></div><div
class="wp-caption aligncenter" style="width: 165px"><img
alt="uistack(h2,'bottom')" src="http://UndocumentedMatlab.com/images/uiflowcontainer8.png" title="uistack(h2,'bottom')" width="155" height="189" /><p
class="wp-caption-text"><i><b>uistack</b></i>(h2,'bottom')<br
/></p></div></center></p><h3 id="uigridcontainer"><i>uigridcontainer</i></h3><p><i><b>uigridcontainer</b></i> is similar to <i><b>uiflowcontainer</b></i> in its specialization of the layout: in this case, the container is divided into a transparent grid of N-by-M cells (1&#215;1 by default), each of which can contain its own component:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hc = uigridcontainer<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Units'</span>,<span style="color:#A020F0;">'norm'</span>,<span style="color:#A020F0;">'Position'</span>,<span style="color: #080;">&#91;</span>.1,.1,.8,.8<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hc, <span style="color:#A020F0;">'GridSize'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">2</span>,<span style="color: #33f;">3</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% default GridSize is [1,1]</span>
h1 = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'string'</span>,<span style="color:#A020F0;">'1'</span>,<span style="color:#A020F0;">'parent'</span>,hc<span style="color: #080;">&#41;</span>;
h2 = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'string'</span>,<span style="color:#A020F0;">'2'</span>,<span style="color:#A020F0;">'parent'</span>,hc<span style="color: #080;">&#41;</span>;
h3 = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'string'</span>,<span style="color:#A020F0;">'3'</span>,<span style="color:#A020F0;">'parent'</span>,hc<span style="color: #080;">&#41;</span>;
h4 = <span style="color: #0000FF;">axes</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hc<span style="color: #080;">&#41;</span>; x = -<span style="color: #33f;">4</span><span style="color: #F0F;">:</span>.1<span style="color: #F0F;">:</span><span style="color: #33f;">4</span>; <span style="color: #0000FF;">plot</span><span style="color: #080;">&#40;</span>h4,x,<span style="color: #0000FF;">sin</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>h4,<span style="color:#A020F0;">'YTickLabel'</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,<span style="color:#A020F0;">'XTickLabel'</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 165px"><img
alt="A 2-by-3 uigridcontainer" src="http://UndocumentedMatlab.com/images/uigridcontainer1.png" title="A 2-by-3 uigridcontainer" width="155" height="186" /><p
class="wp-caption-text">A 2-by-3 uigridcontainer</p></div></center></p><p>The grid cells relative size can be controlled via the <strong>HorizontalWeight</strong> and <strong>VerticalWeight</strong> properties (set to NaN by default). These properties should be a numeric vector the same size as the corresponding number of cells. The property values are not important – only their relative values are used to control the relative cell dimensions. The <strong>EliminateEmptySpace</strong> property (default=&#8217;off&#8217;) controls whether empty grid rows/columns are eliminated from the container or displayed. As in <i><b>uiflowcontainer</b></i>s, the <strong>Margin</strong> property controls the spacing between the internal components and borders:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hc, <span style="color:#A020F0;">'HorizontalWeight'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">6</span>,<span style="color: #33f;">3</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'VerticalWeight'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">0.2</span>,<span style="color: #33f;">0.5</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">delete</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span>h2,h3<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% only h1,h4 remain</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hc,<span style="color:#A020F0;">'EliminateEmptySpace'</span>,<span style="color:#A020F0;">'on'</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 140px"><img
alt="Non-equal grid weights" src="http://UndocumentedMatlab.com/images/uigridcontainer2.png" title="Non-equal grid weights" width="132" height="162" /><p
class="wp-caption-text">Non-equal grid weights</p></div><div
class="wp-caption aligncenter" style="width: 150px"><img
alt="EliminateEmptySpace='on'" src="http://UndocumentedMatlab.com/images/uigridcontainer4.png" title="EliminateEmptySpace='on'" width="135" height="162" /><p
class="wp-caption-text">EliminateEmptySpace='on'</p></div><div
class="wp-caption aligncenter" style="width: 140px"><img
alt="...and 'off'" src="http://UndocumentedMatlab.com/images/uigridcontainer3.png" title="...and 'off'" width="135" height="162" /><p
class="wp-caption-text">...and 'off'</p></div></center></p><h3 id="alternatives">Other layout alternatives</h3><p>Brad Phelan of XTargets has <a
target="_blank" rel="nofollow" href="http://web.archive.org/web/20071015004151/http://xtargets.com/cms/Tutorials/Matlab-Programming/Advanced-Layout-Management-With-Handle-Graphics.html">created</a> Matlab equivalents of Java&#8217;s <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/layout/border.html">BorderLayout</a> and <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/layout/spring.html">SpringLayout</a>. The advantage of using Brad&#8217;s layout managers is that they appear to have full Matlab interoperability, including the ability to add Matlab components, unlike Java&#8217;s layout managers.</p><p>A File Exchange contributor named Jason has added a <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/22968">GridBagLayout implementation</a>, mimicking Java&#8217;s well-known <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html">GridBagLayout</a>.</p><p>Additional and more flexible layout managers are available in Java (one of my favorites is <a
target="_blank" rel="nofollow" href="http://www.jgoodies.com/freeware/forms/">JGoodies Forms</a>, which is pre-bundled with Matlab). Just remember the limitation that no Matlab component (such as GUI controls or plot axes) can be added to Java containers. Therefore, feel free to use these Java containers as long as their contained GUI is limited to Java components (JButton, JComboBox etc.).</p><h3 id="uitools">Other uitools</h3><p>Today&#8217;s article about <i><b>uicontainer</b></i> and its relatives was the first of several posts that will describe undocumented functions that reside in the %matlabroot%/toolbox/matlab/uitools folder. Feel free to look within this folder for other interesting undocumented functions. Most of these functions are semi-documented, meaning that they have a usable help-section hidden within the m-file (type &#8220;edit uicontainer.m&#8221; for example). Many have existed more-or-less unchanged for many releases. Note that there is no guarantee that they will remain in future releases. When using unsupported functionality, always code defensively.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives/' rel='bookmark' title='Tab panels &#8211; uitab and relatives'>Tab panels &#8211; uitab and relatives</a> <small>This article describes several undocumented Matlab functions that support tab-panels...</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-the-matlab-desktop-layout-programmatically/' rel='bookmark' title='Setting the Matlab desktop layout programmatically'>Setting the Matlab desktop layout programmatically</a> <small>The Matlab desktop enables saving and switching layouts using the main menu. This post shows how to do so programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/handle2struct-struct2handle-and-matlab-8/' rel='bookmark' title='handle2struct, struct2handle &amp; Matlab 8.0'>handle2struct, struct2handle &#038; Matlab 8.0</a> <small>This article explains how we can use a couple of undocumented functions in Matlab GUI, and what we can learn from this about Matlab's future....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/matlab-layout-managers-uicontainer-and-relatives/feed/</wfw:commentRss> <slash:comments>11</slash:comments> </item> <item><title>GUIDE customization</title><link>http://undocumentedmatlab.com/blog/guide-customization/</link> <comments>http://undocumentedmatlab.com/blog/guide-customization/#comments</comments> <pubDate>Wed, 10 Jun 2009 17:00:43 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[GUIDE]]></category> <category><![CDATA[UIInspect]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=383</guid> <description><![CDATA[Matlab's GUI Design Editor (GUIDE) has several interesting undocumented features. This post describes how to customize GUIDE rulers.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/button-customization/' rel='bookmark' title='Button customization'>Button customization</a> <small>Matlab's button uicontrols (pushbutton and togglebutton) are basically wrappers for a Java Swing JButton object. This post describes how the buttons can be customized using this Java object in ways...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-customization-report/' rel='bookmark' title='Uitable customization report'>Uitable customization report</a> <small>In last week&#8217;s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the...</small></li><li><a
href='http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/' rel='bookmark' title='FindJObj &#8211; find a Matlab component&#8217;s underlying Java object'>FindJObj &#8211; find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-sorting/' rel='bookmark' title='Uitable sorting'>Uitable sorting</a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>GUIDE is the acronym for Matlab&#8217;s Graphical User Interface Design Editor. It is very handy for designing simple GUI figures, although my experience has shown that it has limitations for complex GUIs. Nevertheless, GUIDE is the tool used by most Matlab developers when designing GUIs. In this post, I will show a few undocumented customizations that could help make GUIDE sessions more productive.</p><p>The starting point is GUIDE&#8217;s undocumented return value, which is a Java reference to the Layout Editor panel within the GUIDE figure frame:</p><blockquote><pre>
<span style="color: #000000;">&gt;&gt; h = guide</span>
h =
Layout Document [untitled]

<span style="color: #000000;">&gt;&gt; h.getClass</span>
ans =
class com.mathworks.toolbox.matlab.guide.LayoutEditor
</pre></blockquote><p>This return handle can be used to access GUIDE components and functionality. We can start by inspecting the interesting GUIDE layout hierarchy using my <a
rel="nofollow" target="_blank" href="http://www.mathworks.com/matlabcentral/fileexchange/14317">FindJObj utility</a>, and the associated properties and method using my <a
rel="nofollow" target="_blank" href="http://www.mathworks.com/matlabcentral/fileexchange/17935">UIInspect utility</a>:</p><blockquote><pre><span style="color: #000000;">&gt;&gt; h.findjobj;
&gt;&gt; h.uiinspect;
</span></pre></blockquote><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="Hierarchy of Layout Editor within the GUIDE frame" src="http://undocumentedmatlab.com/images/guide_ruler1.png" title="Hierarchy of Layout Editor within the GUIDE frame" width="450" height="351" /><p
class="wp-caption-text">Hierarchy of Layout Editor within the GUIDE frame</p></div></center></p><p>Note: If you wish to see the hierarchy of the entire GUIDE figure frame, simply run FindJObj on the frame reference, by either of the two following methods (and similarly for UIInspect):</p><blockquote><pre><span style="color: #000000;">&gt;&gt; findjobj(h.getFrame);
&gt;&gt; findjobj(h.getTopLevelWindow);
</span></pre></blockquote><p>We see that the Layout Editor contains, in addition to the expected LayoutArea and two MWScrollbars, several objects that relate to a ruler. These rulers can be activated via the GUIDE menu (Tools / Grid and Rulers), or via the Matlab Command Prompt as described below:</p><p>Looking at the ruler properties in FindJObj or UIInspect, we can see a settable boolean property called &#8220;RulerState&#8221;. If we turn it on we can see that a very handy pixels-ruler appears. Once we set this property, it remains in effect for every future GUIDE session:</p><p><center><div
class="wp-caption aligncenter" style="width: 440px"><img
alt="Before: GUIDE with no rulers" src="http://undocumentedmatlab.com/images/guide_ruler2.png" title="Before: GUIDE with no rulers" width="430" height="286" /><p
class="wp-caption-text">Before: GUIDE with no rulers</p></div></center></p><blockquote><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">h.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">4</span><span style="color: #080;">&#41;</span>.<span style="">setRulerState</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% Horizontal</span>
h.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">5</span><span style="color: #080;">&#41;</span>.<span style="">setRulerState</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% Vertical</span></pre></div></div></blockquote><p>Note: RulerState actually controls a system preference (LayoutShowRulers, a boolean flag) that controls the visibility of both rulers, and persists across Matlab/GUIDE sessions.  To change the visibility of only a single ruler for this GUIDE session only, or on old Matlab versions (e.g. Matlab 7.1 aka R14 SP3) that do not have the &#8216;RulerState&#8217; property, use the hide()/show()/setVisible(flag) methods, or set the &#8216;Visible&#8217; property:</p><blockquote><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Equivalent ways to show horizontal ruler for this GUIDE session only</span>
hRuler = h.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">4</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% =top horizontal ruler</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hRuler, <span style="color:#A020F0;">'Visible'</span>,<span style="color:#A020F0;">'on'</span><span style="color: #080;">&#41;</span>;
hRuler.<span style="">setVisible</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% or: hRuler.setVisible(1)</span>
hRuler.<span style="">show</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;</pre></div></div></blockquote><p><center><div
class="wp-caption aligncenter" style="width: 440px"><img
alt="After: GUIDE with pixel rulers" src="http://undocumentedmatlab.com/images/guide_ruler3.png" title="After: GUIDE with pixel rulers" width="430" height="286" /><p
class="wp-caption-text">After: GUIDE with pixel rulers</p></div></center></p><p>Using this method, we can customize the rulers &#8211; options which are unavailable using the standard GUIDE menu options: We can specify horizontal/vertical grid size, tick &#038; label interval and similar ruler properties. For example, let&#8217;s set a 5-pixel minor tick interval, 25-pixel major interval, labels every 50 pixels, starting offset of 40 pixels and a ruler size limited at 260 pixels:</p><blockquote><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hRuler = h.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">4</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% =top horizontal ruler</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hRuler, <span style="color:#A020F0;">'MinorInterval'</span>,<span style="color: #33f;">5</span>, <span style="color:#A020F0;">'MajorInterval'</span>,<span style="color: #33f;">25</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hRuler, <span style="color:#A020F0;">'LabelInterval'</span>,<span style="color: #33f;">50</span>, <span style="color:#A020F0;">'LabelUnit'</span>,<span style="color: #33f;">50</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hRuler, <span style="color:#A020F0;">'Margin'</span>,<span style="color: #33f;">40</span>, <span style="color:#A020F0;">'Length'</span>,<span style="color: #33f;">260</span><span style="color: #080;">&#41;</span>;</pre></div></div></blockquote><p><center><div
class="wp-caption aligncenter" style="width: 440px"><img
alt="GUIDE with modified pixel rulers" src="http://undocumentedmatlab.com/images/guide_ruler4.png" title="GUIDE with modified pixel rulers" width="430" height="286" /><p
class="wp-caption-text">GUIDE with modified pixel rulers</p></div></center></p><p>Note that the vertical ruler&#8217;s labels start (=LabelStart property) at the figure&#8217;s height, and have a <em>decreasing</em> LabelInterval of -50. This is done because Java coordinates start counting from the top-left corner <em>downward</em>, whereas Matlab counts from the bottom-left <em>upward</em>. In GUIDE, we naturally wish to display the Matlab coordinates, hence the transformation.</p><p>Note: unfortunately, most of these properties do not have equivalent settable system properties that I could find. Here is a list of all the GUIDE-related system properties that I found:</p><ul><li><strong>LayoutShowRulers</strong> &#8211; boolean, controls display of both rulers</li><li><strong>LayoutShowGuides</strong> &#8211; boolean, controls display of blue guidelines</li><li><strong>LayoutShowGrid</strong> &#8211; boolean, controls display of gray gridlines</li><li><strong>LayoutGridWidth</strong> &#8211; integer, controls the size of the grid boxes</li><li><strong>LayoutSnapToGrid</strong> &#8211; boolean, controls snap-to-grid behavior</li><li><strong>LayoutActivate</strong> &#8211; boolean, controls ability to run (activate) unsaved figures without confirmation</li><li><strong>LayoutChangeDefaultCallback</strong> &#8211; boolean, ??? (I can see this preference in my matlab.prf file but I have no idea what it does or how it got there&#8230;)</li><li><strong>LayoutExport</strong> &#8211; boolean, controls ability to export unsaved figures without confirmation</li><li><strong>LayoutExtension</strong> &#8211; boolean, controls display of file extension in the GUIDE window title</li><li><strong>LayoutFullPath</strong> &#8211; boolean, controls display of file path in the GUIDE window title</li><li><strong>LayoutMCodeComments</strong> &#8211; boolean, controls generation of comments for m-file callbacks</li><li><strong>LayoutToolBar</strong> &#8211; boolean, controls display of the GUIDE widow toolbar</li><li><strong>LayoutToolNames</strong> &#8211; boolean, controls display of tool names in the components palette</li></ul><p>Have you discovered other undocumented features in GUIDE? If so, please share your findings in the comments section below.</p><p><strong><u>Warning</u></strong>: These undocumented features are way deep in unsupported territory. They depend heavily on Matlab&#8217;s internal implementation, which may change without any prior notice between Matlab releases. They work ok on Matlab versions 7.1 (R14 SP3) through 7.6 (R2008a), and perhaps on other versions as well. However, the very next Matlab release might break these features, so beware.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/button-customization/' rel='bookmark' title='Button customization'>Button customization</a> <small>Matlab's button uicontrols (pushbutton and togglebutton) are basically wrappers for a Java Swing JButton object. This post describes how the buttons can be customized using this Java object in ways...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-customization-report/' rel='bookmark' title='Uitable customization report'>Uitable customization report</a> <small>In last week&#8217;s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the...</small></li><li><a
href='http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/' rel='bookmark' title='FindJObj &#8211; find a Matlab component&#8217;s underlying Java object'>FindJObj &#8211; find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-sorting/' rel='bookmark' title='Uitable sorting'>Uitable sorting</a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/guide-customization/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           tag
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-05-21 19:53:08 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/tag/guide/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      0.922s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1337655188; expires=Wed, 22-May-2013 02:53:08 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Tue, 22 May 2012 02:53:08 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Tue, 22 May 2012 03:53:08 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               bd68d8f4d3f412dffc8b85ad84de1b03
Content-Encoding:   gzip
-->
