<?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; Semi-documented function</title> <atom:link href="http://undocumentedmatlab.com/blog/category/semi-documented-function/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>Customizing menu items part 1</title><link>http://undocumentedmatlab.com/blog/customizing-menu-items-part-1/</link> <comments>http://undocumentedmatlab.com/blog/customizing-menu-items-part-1/#comments</comments> <pubDate>Wed, 25 Apr 2012 18:14:08 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Figure window]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Menubar]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2897</guid> <description><![CDATA[Matlab menus can be customized in a variety of undocumented manners - first article of a series.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-2/' rel='bookmark' title='Customizing menu items part 2'>Customizing menu items part 2</a> <small>Matlab menu items can be customized in a variety of useful ways using their underlying Java object. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-3/' rel='bookmark' title='Customizing menu items part 3'>Customizing menu items part 3</a> <small>Matlab menu items can easily display custom icons, using just a tiny bit of Java magic powder. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes/' rel='bookmark' title='Customizing uitree nodes &#8211; part 1'>Customizing uitree nodes &#8211; part 1</a> <small>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function...</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>Over the past years, I have not posted articles dealing with menu items. I have shown how to <a
target="_blank" href="http://undocumentedmatlab.com/blog/modifying-default-toolbar-menubar-actions/">directly access menu items&#8217; hidden handles</a>, but not much more than that. A year ago I <a
target="_blank" href="http://undocumentedmatlab.com/blog/2010-perspective/">promised</a> a mini-series on menu customizations, and it&#8217;s time to keep my promise. In today&#8217;s article, the first in the mini-series, I will present several undocumented menu customization topics that rely on pure-Matlab (i.e, no Java today). The next article in this series will focus on Java-based customizations.</p><h3 id="Invoking">Invoking menu item callbacks</h3><p>As noted above, a figure window&#8217;s menu items can be directly accessed. Once we access a menu item&#8217;s handle, we can extract its <b>Callback</b> property and directly invoke it (for example, using the semi-documented <a
target="_blank" href="http://undocumentedmatlab.com/blog/hgfeval/"><i><b>hgfeval</b></i> function</a>). Note that menu callbacks are kept in <b>Callback</b>, while toolbar callbacks are kept in <b>ClickedCallback</b>.</p><p>Menu callbacks generally use internal <a
target="_blank" href="http://undocumentedmatlab.com/blog/legend-semi-documented-feature/#Semi-documented">semi-documented</a> functions (i.e., having a readable help section but no doc, online help, or official support), which are part of Matlab&#8217;s uitools folder. These functions are specific to each top-level menu tree: <i><b>filemenufcn, editmenufcn, viewmenufcn, insertmenufcn, toolsmenufcn, desktopmenufcn, winmenu</b></i>, and <i><b>helpmenufcn</b></i> implement the figure&#8217;s eight respective top-level menu trees&#8217; callbacks. These functions accept an optional figure handle (otherwise, <i><b>gcbf</b></i> is assumed), followed by a string specifying the specific menu item whose action needs to be run. <i><b>webmenufcn</b></i> implements the Help menu&#8217;s Web Resources sub-menu callbacks in a similar manner.</p><p>Use of these functions makes it easy to invoke a menu action directly from our Matlab code: instead of accessing the relevant menu item and invoking its <b>Callback</b>, we simply find out the menu item string in advance and use it directly. For example,</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">filemenufcn FileClose;
editmenufcn<span style="color: #080;">&#40;</span>hFig,<span style="color:#A020F0;">'EditPaste'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><i><b>uimenufcn</b></i> is a related fully-undocumented (built-in) function, available since Matlab R11 (late 1990s). It accepts a figure handle (or the zero [0] handle to indicate the desktop) and action name. For example, the fully-documented <i><b>commandwindow</b></i> function uses the following code to bring the Command Window into focus:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">uimenufcn<span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>, <span style="color:#A020F0;">'WindowCommandWindow'</span><span style="color: #080;">&#41;</span>;</pre></div></div><h3 id="Uitools">Customizing menus via uitools</h3><p><i><b>makemenu</b></i> is another semi-documented uitool function that enables easy creation of hierarchical menu trees with separators and accelerators. It is a simple and effective wrapper for <i><b>uimenu</b></i>. <i><b>makemenu</b></i> is a useful function that has been made obsolete (grandfathered) without any known replacement.</p><p><i><b>makemenu</b></i> accepts four parameters: a figure handle, a char matrix of labels (&#8216;&gt;&#8217; indicating sub item, &#8216;&gt;&gt;&#8217; indicating sub-sub items etc.; &#8216;&amp;&#8217; indicating keyboard shortcut; &#8216;^x&#8217; indicating an accelerator key; &#8216;-&#8217; indicating a separator line), a char matrix of callbacks, and an optional char matrix of tags (empty by default). <i><b>makemenu</b></i> makes use of another semi-documented grandfathered function, <i><b>menulabel</b></i>, to parse the specified label components. <i><b>makemenu</b></i> returns an array of handles of the created <i><b>uimenu</b></i> items:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">labels = str2mat<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'&amp;File'</span>, <span style="color: #F0F;">...</span>    <span style="color: #228B22;">% File top menu</span>
           <span style="color:#A020F0;">'&gt;&amp;New^n'</span>, <span style="color: #F0F;">...</span>           <span style="color: #228B22;">% File=&gt;New</span>
           <span style="color:#A020F0;">'&gt;&amp;Open'</span>, <span style="color: #F0F;">...</span>            <span style="color: #228B22;">% File=&gt;Open</span>
           <span style="color:#A020F0;">'&gt;&gt;Open &amp;document^d'</span>, <span style="color: #F0F;">...</span>    <span style="color: #228B22;">% File=&gt;Open=&gt;doc</span>
           <span style="color:#A020F0;">'&gt;&gt;Open &amp;graph^g'</span>, <span style="color: #F0F;">...</span>       <span style="color: #228B22;">% File=&gt;Open=&gt;graph</span>
           <span style="color:#A020F0;">'&gt;-------'</span>, <span style="color: #F0F;">...</span>          <span style="color: #228B22;">% File=&gt;separator line</span>
           <span style="color:#A020F0;">'&gt;&amp;Save^s'</span>, <span style="color: #F0F;">...</span>          <span style="color: #228B22;">% File=&gt;Save</span>
           <span style="color:#A020F0;">'&amp;Edit'</span>, <span style="color: #F0F;">...</span>		<span style="color: #228B22;">% Edit top menu</span>
           <span style="color:#A020F0;">'&amp;View'</span>, <span style="color: #F0F;">...</span>		<span style="color: #228B22;">% View top menu</span>
           <span style="color:#A020F0;">'&gt;&amp;Axis^a'</span>, <span style="color: #F0F;">...</span>          <span style="color: #228B22;">% View=&gt;Axis</span>
           <span style="color:#A020F0;">'&gt;&amp;Selection region^r'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% View=&gt;Selection</span>
calls = str2mat<span style="color: #080;">&#40;</span><span style="color:#A020F0;">''</span>, <span style="color: #F0F;">...</span>		<span style="color: #228B22;">% no action: File top menu</span>
           <span style="color:#A020F0;">'disp('</span><span style="color:#A020F0;">'New'</span><span style="color:#A020F0;">')'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">''</span>, <span style="color: #F0F;">...</span>			<span style="color: #228B22;">% no action: Open sub-menu</span>
           <span style="color:#A020F0;">'disp('</span><span style="color:#A020F0;">'Open doc'</span><span style="color:#A020F0;">')'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'disp('</span><span style="color:#A020F0;">'Open graph'</span><span style="color:#A020F0;">')'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">''</span>, <span style="color: #F0F;">...</span>			<span style="color: #228B22;">% no action: Separator</span>
           <span style="color:#A020F0;">'disp('</span><span style="color:#A020F0;">'Save'</span><span style="color:#A020F0;">')'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">''</span>, <span style="color: #F0F;">...</span>			<span style="color: #228B22;">% no action: Edit top menu</span>
           <span style="color:#A020F0;">''</span>, <span style="color: #F0F;">...</span>			<span style="color: #228B22;">% no action: View top menu</span>
           <span style="color:#A020F0;">'disp('</span><span style="color:#A020F0;">'View axis'</span><span style="color:#A020F0;">')'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'disp('</span><span style="color:#A020F0;">'View selection region'</span><span style="color:#A020F0;">')'</span><span style="color: #080;">&#41;</span>;
handles = makemenu<span style="color: #080;">&#40;</span>hFig, labels, calls<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hFig,<span style="color:#A020F0;">'menuBar'</span>,<span style="color:#A020F0;">'none'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 277px"><img
alt="A simple figure menu" src="http://UndocumentedMatlab.com/images/makemenu.png" title="A simple figure menu" width="267" height="148"/><p
class="wp-caption-text">A simple figure menu</p></div></center></p><h3 id="HTML">Customizing menus via HTML</h3><p>Since menu items share the same <a
target="_blank" href="http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/">HTML/CSS support feature</a> as all Java Swing labels, we can specify font size/face/color, bold, italic, underline, superscript/subscript, and practically any HTML formatting.</p><p>Note that some features, such as the font or foreground/background colors, have specific properties that we can set using the Java handle, instead of using HTML. The benefit of using HTML is that it enables setting all the formatting in a single property. HTML does not require using Java – just pure Matlab (see the following example).</p><p>Multi-line menu items can easily be done with HTML: simply include a <code>&lt;br&gt;</code> element in the label – the menu item will split into two lines and automatically resize vertically when displayed:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">txt1 = <span style="color:#A020F0;">'&lt;html&gt;&lt;b&gt;&lt;u&gt;&lt;i&gt;Save&lt;/i&gt;&lt;/u&gt;'</span>;
txt2 = <span style="color:#A020F0;">'&lt;font color=&quot;red&quot;&gt;&lt;sup&gt;this file&lt;/sup&gt;&lt;/font&gt;&lt;/b&gt;&lt;/html&gt;'</span>;
txt3 = <span style="color:#A020F0;">'&lt;br /&gt;this file as...'</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>findall<span style="color: #080;">&#40;</span>hFig,<span style="color:#A020F0;">'tag'</span>,<span style="color:#A020F0;">'figMenuFileSave'</span><span style="color: #080;">&#41;</span>,   <span style="color:#A020F0;">'Label'</span>,<span style="color: #080;">&#91;</span>txt1,txt2<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>findall<span style="color: #080;">&#40;</span>hFig,<span style="color:#A020F0;">'tag'</span>,<span style="color:#A020F0;">'figMenuFileSaveAs'</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'Label'</span>,<span style="color: #080;">&#91;</span>txt1,txt3<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 195px"><img
alt="A multi-line HTML-rendered menu item" src="http://UndocumentedMatlab.com/images/uimenu2.png" title="A multi-line HTML-rendered menu item" width="185" height="184"/><p
class="wp-caption-text">A multi-line HTML-rendered menu item</p></div></center></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>hMenuItem, <span style="color:#A020F0;">'Label'</span>,<span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;&amp;2: C:\My Documents\doc.txt&lt;br /&gt;'</span>
   <span style="color:#A020F0;">'&lt;font size=&quot;-1&quot; face=&quot;Courier New&quot; color=&quot;red&quot;&gt;&amp;nbsp;&amp;nbsp; '</span>
   <span style="color:#A020F0;">'Date: 15-Jun-2011 13:23:45&lt;br /&gt;&amp;nbsp;&amp;nbsp; Size: 123 KB&lt;/font&gt;&lt;/html&gt;'</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: 421px"><img
alt="HTML-rendered menu items" src="http://UndocumentedMatlab.com/images/uimenu2b.png" title="HTML-rendered menu items" width="411" height="246"/><p
class="wp-caption-text">HTML-rendered menu items</p></div></center></p><p>Much more complex customizations can be achieved using Java. So stay tuned to part 2 of this mini-series&#8230;</p><p>Note: Menu customization is explored in depth in section 4.6 of my <a
target="_blank" href="http://undocumentedmatlab.com/matlab-java-book/">book</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-2/' rel='bookmark' title='Customizing menu items part 2'>Customizing menu items part 2</a> <small>Matlab menu items can be customized in a variety of useful ways using their underlying Java object. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-3/' rel='bookmark' title='Customizing menu items part 3'>Customizing menu items part 3</a> <small>Matlab menu items can easily display custom icons, using just a tiny bit of Java magic powder. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes/' rel='bookmark' title='Customizing uitree nodes &#8211; part 1'>Customizing uitree nodes &#8211; part 1</a> <small>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function...</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/customizing-menu-items-part-1/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <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>Types of undocumented Matlab aspects</title><link>http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/</link> <comments>http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/#comments</comments> <pubDate>Thu, 24 Nov 2011 18:00:36 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented feature]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JMI]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[Undocumented property]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2534</guid> <description><![CDATA[This article lists the different types of undocumented/unsupported/hidden aspects in Matlab<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/reasons-for-undocumented-matlab-aspects/' rel='bookmark' title='Reasons for undocumented Matlab aspects'>Reasons for undocumented Matlab aspects</a> <small>There are many reasons for the numerous undocumented aspects in Matlab - this article explains them....</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-cursorbar-object/' rel='bookmark' title='Undocumented cursorbar object'>Undocumented cursorbar object</a> <small>Matlab's internal undocumented graphics.cursorbar object can be used to present dynamic data-tip cross-hairs...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uiundo-matlab-undocumented-undo-redo-manager/' rel='bookmark' title='uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager'>uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager</a> <small>The built-in uiundo function provides easy yet undocumented access to Matlab's powerful undo/redo functionality. This article explains its usage....</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-xml-functionality/' rel='bookmark' title='Undocumented XML functionality'>Undocumented XML functionality</a> <small>Matlab's built-in XML-processing functions have several undocumented features that can be used by Java-savvy users...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Why are there so many undocumented aspects in Matlab?</p><p>This is a great question, recently <a
target="_blank" href="http://undocumentedmatlab.com/blog/guide-customization/#comment-61578">asked</a> by a reader of this blog, so I wanted to expand on it in next week&#8217;s article. Before specifying the different reasons, let&#8217;s map the nature of undocumented aspects that we find in Matlab.</p><p>The term <i>undocumented/unsupported</i> (as opposed to <i>mis-documentated</i> or <i>deprecated</i>) actually refers to quite a large number of different types.<br
/> In the following list, the hyperlinks on the list-item titles lead to a list of corresponding articles on this website:</p><ul><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/undocumented-function/">Undocumented functions</a></b><br
/> Matlab functions which appears nowhere in the documentation, are usually built-in functions (do not have an m-file) and can only be inferred from online CSSM posts or usage within one of the Matlab m-functions installed with Matlab (the latter being the usual case). None of these functions is officially supported by MathWorks. <a
target="_blank" href="http://undocumentedmatlab.com/blog/category/mex/">MEX</a> is an important source for such functions.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/semi-documented-function/">Semi-documented functions</a></b><br
/> Matlab functionality which exists in Matlab m-functions installed with Matlab, but have their main comment separated from the H1 comment line, thereby hiding it from normal view (via Matlab&#8217;s <i><b>help</b></i> function). The H1 comment line itself is simply a standard warning that this function is not officially supported and may change in some future version. To see the actual help comment, simply edit the function (using Matlab&#8217;s <i><b>edit</b></i> function or any text editor) and place a comment sign (%) at the empty line between the H1 comment and the actual help section. The entire help section will then onward be visible via the <i><b>help</b></i> function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">        <span style="color: #0000FF;">function</span> <span style="color: #080;">&#91;</span>tree, container<span style="color: #080;">&#93;</span> = uitree<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
        <span style="color: #228B22;">% WARNING: This feature is not supported in MATLAB</span>
        <span style="color: #228B22;">% and the API and functionality may change in a future release.</span>
<span style="color: #0000FF;">fix</span> =&gt;  <span style="color: #228B22;">%</span>
        <span style="color: #228B22;">% UITREE creates a uitree component with hierarchical data in a figure window.</span>
        <span style="color: #228B22;">%   UITREE creates an empty uitree object with default property values in</span>
        <span style="color: #228B22;">%   a figure window.</span>
        <span style="color: #228B22;">%...</span></pre></div></div><p>These functions are not documented in the full documentation (via Matlab&#8217;s <i><b>doc</b></i> function, or online). The odd thing is that some of these functions may appear in the category help output (for example, <i><b>help</b>(&#8216;uitools&#8217;)</i>), and in some cases may even have a fully-visible help section (e.g., <i><b>help</b>(&#8216;setptr&#8217;)</i>), but do not have any online help documentation (<i><b>docsearch</b>(&#8216;setptr&#8217;)</i> fails, and <i><b>doc</b>(&#8216;setptr&#8217;)</i> simply displays the readable help text).</p><p>All these functions are officially unsupported by MathWorks, even when having a readable help section. The rule of thumb appears to be that a Matlab function is supported only if it has online documentation. Note, however, that in some rare cases a documentation discrepancy may be due to a MathWorks documentation error, not to unsupportability&#8230;</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/undocumented-function/">Helper functions</a></b><br
/> Many fully-supported Matlab functions use helper functions that have a specific use in the main (documented) function(s).  Often, these helper functions are tightly-coupled to their documented parents and are useless as stand-alone functions. But quite a few of them have quite useful stand-alone use, as I&#8217;ve already shown in some past articles.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/undocumented-feature/">Undocumented features</a> and <a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/undocumented-property/">properties</a></b><br
/> Features of otherwise-documented Matlab functions, which appear nowhere in the official documentation. You guessed it – these are also not supported by MathWorks&#8230; Like undocumented functions, you can only infer such features by the occasional CSSM post or a reference somewhere in Matlab&#8217;s m-code.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/semi-documented-feature/">Semi-documented features</a></b><br
/> Features of otherwise-documented Matlab functions, which are documented in a separate section beneath the main help section, and nowhere else (not in the full doc not the online documentation). If you did not know in advance that these features existed, you could only learn of them by manually looking at Matlab&#8217;s m-files (which is what I do in most cases&#8230;).</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/undocumented-property/">Undocumented properties</a></b><br
/> Many Matlab objects have internal properties, which can be retrieved (via Matlab&#8217;s <i><b>get</b></i> function) and/or set (via the <i><b>set</b></i> function) programmatically. All these properties are fully documented. Many objects also possess hidden properties, some of which are very interesting and useful, but which are undocumented and (oh yes) unsupported. Like undocumented features, they can only be inferred from CSSM or existing code. In a recent <a
target="_blank" href="http://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/">article</a> I described my <i><b>getundoc</b></i> utility, which lists these undocumented properties of specified objects.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/internal-component/">Internal Matlab classes</a></b><br
/> Matlab uses a vast array of specialized Java classes to handle everything from algorithms to GUI. These classes are (of course) undocumented/unsupported. They can often be accessed directly from the Matlab Command Window or user m-files. GUI classes can be inferred by inspecting the figure frame&#8217;s Java components, and non-GUI classes can often be inferred from references in Matlab&#8217;s m-files.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/JMI">Matlab-Java integration</a></b><br
/> Matlab&#8217;s GUI interface, as well as the Java-to-Matlab interface (JMI) is fully undocumented and unsupported. In addition to JMI, there are other mechanisms to run Matlab code from within Java (namely JMI, COM and DDE) &#8211; these are all unsupported and by-and-large undocumented.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/?s=UDD">Matlab&#8217;s UDD mechanism</a></b><br
/> UDD (Unified Data Definition?) is used extensively in Matlab as the internal object-oriented mechanism for describing object properties and functionalities. We can use UDD for a wide variety of uses. UDD was described in a series of articles here in early 2011.</li></ul><p>Next week I will list the reasons that cause MathWorks to decide whether a particular feature or property should be documented or not.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/reasons-for-undocumented-matlab-aspects/' rel='bookmark' title='Reasons for undocumented Matlab aspects'>Reasons for undocumented Matlab aspects</a> <small>There are many reasons for the numerous undocumented aspects in Matlab - this article explains them....</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-cursorbar-object/' rel='bookmark' title='Undocumented cursorbar object'>Undocumented cursorbar object</a> <small>Matlab's internal undocumented graphics.cursorbar object can be used to present dynamic data-tip cross-hairs...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uiundo-matlab-undocumented-undo-redo-manager/' rel='bookmark' title='uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager'>uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager</a> <small>The built-in uiundo function provides easy yet undocumented access to Matlab's powerful undo/redo functionality. This article explains its usage....</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-xml-functionality/' rel='bookmark' title='Undocumented XML functionality'>Undocumented XML functionality</a> <small>Matlab's built-in XML-processing functions have several undocumented features that can be used by Java-savvy users...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Javacomponent background color</title><link>http://undocumentedmatlab.com/blog/javacomponent-background-color/</link> <comments>http://undocumentedmatlab.com/blog/javacomponent-background-color/#comments</comments> <pubDate>Wed, 12 Oct 2011 14:39:43 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Undocumented feature]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2459</guid> <description><![CDATA[This article explains how to align Java component background color with a Matlab color.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/common-javacomponent-problems/' rel='bookmark' title='Common javacomponent problems'>Common javacomponent problems</a> <small>The javacomponent function is very useful for placing Java components on-screen, but has a few quirks. ...</small></li><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/color-selection-components/' rel='bookmark' title='Color selection components'>Color selection components</a> <small>Matlab has several internal color-selection components that can easily be integrated in Matlab GUI...</small></li><li><a
href='http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/' rel='bookmark' title='cprintf &#8211; display formatted color text in the Command Window'>cprintf &#8211; display formatted color text in the Command Window</a> <small>cprintf is a utility that utilized undocumented Matlab desktop functionalities to display color and underline-styled formatted text in the Command Window...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>In this website, I have often shown how Matlab application functionality can be significantly enhanced by including Java components in the Matlab GUI, using the built-in semi-documented <i><b>javacomponent</b></i> function (which I <a
target="_blank" href="http://undocumentedmatlab.com/blog/javacomponent/">described here</a> last year).</p><p>Using java components in Matlab is simple and easy, but there are a few annoying catches. One of these is the fact that the default Matlab figure background color ([0.8, 0.8, 0.8]) is a different shade of gray than the default <i><b>uicontrol</b></i> background color ([0.9255, 0.9137, 0.847]). <i><b>javacomponent</b></i>s use the same background color as <i><b>uicontrol</b></i>s.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hPanel = uipanel<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'pixe'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">30</span>,<span style="color: #33f;">30</span>,<span style="color: #33f;">200</span>,<span style="color: #33f;">100</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'title'</span>,<span style="color:#A020F0;">'Matlab uipanel'</span><span style="color: #080;">&#41;</span>;
jLabel = javax.<span style="">swing</span>.<span style="">JLabel</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Java Label'</span><span style="color: #080;">&#41;</span>;
<span style="color: #080;">&#91;</span>jhlabel,jContainer<span style="color: #080;">&#93;</span>=javacomponent<span style="color: #080;">&#40;</span>jLabel, <span style="color: #080;">&#91;</span><span style="color: #33f;">250</span>,<span style="color: #33f;">50</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">50</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 404px"><img
alt="Javacomponents use different default background color than figures" src="http://UndocumentedMatlab.com/images/javacomponent_bgcolor.png" title="Javacomponents use different default background color than figures" width="394" /><p
class="wp-caption-text">Javacomponents use different default background color than figures</p></div></center></p><p>While Matlab users are familiar with updating Matlab colors, doing so with Java colors is a bit different. We have two basic ways to align the <i><b>javacomponent</b></i> background color with the figure&#8217;s: either change the figure&#8217;s <b>Color</b> property to the Java component&#8217;s bgcolor, or vice versa:</p><h3 id="Option1">Changing the <i><b>javacomponent</b></i>&#8216;s background color to the figure color</h3><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Fix the Java component:</span>
<span style="color: #228B22;">% Variant #1</span>
bgcolor = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>, <span style="color:#A020F0;">'Color'</span><span style="color: #080;">&#41;</span>;
jLabel.<span style="">setBackground</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span>bgcolor<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>,bgcolor<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>,bgcolor<span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Variant #2</span>
bgcolor = <span style="color: #0000FF;">num2cell</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>, <span style="color:#A020F0;">'Color'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
jLabel.<span style="">setBackground</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span>bgcolor<span style="color: #080;">&#123;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Fix the Matlab panel uicontrol</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hPanel, <span style="color:#A020F0;">'BackgroundColor'</span>, <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>,<span style="color:#A020F0;">'Color'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 404px"><img
alt="controls with fixed background colors" src="http://UndocumentedMatlab.com/images/javacomponent_bgcolor2.png" title="controls with fixed background colors" width="394" /><p
class="wp-caption-text">controls with fixed background colors</p></div></center></p><h3 id="Option2">Changing the figure&#8217;s color to the <i><b>javacomponent</b></i>&#8216;s background color</h3><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">bgcolor = jLabel.<span style="">getBackground</span>.<span style="">getComponents</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</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><span style="color: #0000FF;">gcf</span>, <span style="color:#A020F0;">'Color'</span>, bgcolor<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 404px"><img
alt="figure with modified color to match the controls" src="http://UndocumentedMatlab.com/images/javacomponent_bgcolor3.png" title="figure with modified color to match the controls" width="394" /><p
class="wp-caption-text">figure with modified color to match the controls</p></div></center></p><p>Look at the list of related posts below for other articles related to colors in Matlab.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/common-javacomponent-problems/' rel='bookmark' title='Common javacomponent problems'>Common javacomponent problems</a> <small>The javacomponent function is very useful for placing Java components on-screen, but has a few quirks. ...</small></li><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/color-selection-components/' rel='bookmark' title='Color selection components'>Color selection components</a> <small>Matlab has several internal color-selection components that can easily be integrated in Matlab GUI...</small></li><li><a
href='http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/' rel='bookmark' title='cprintf &#8211; display formatted color text in the Command Window'>cprintf &#8211; display formatted color text in the Command Window</a> <small>cprintf is a utility that utilized undocumented Matlab desktop functionalities to display color and underline-styled formatted text in the Command Window...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/javacomponent-background-color/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Bug and workaround in timeseries plot</title><link>http://undocumentedmatlab.com/blog/bug-and-workaround-in-timeseries-plot/</link> <comments>http://undocumentedmatlab.com/blog/bug-and-workaround-in-timeseries-plot/#comments</comments> <pubDate>Wed, 07 Sep 2011 18:41:33 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2428</guid> <description><![CDATA[Matlab's internal hgconvertunits function has a bug that affects timeseries plots. Luckily there is a simple workaround.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/plot-liminclude-properties/' rel='bookmark' title='Plot LimInclude properties'>Plot LimInclude properties</a> <small>The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-performance/' rel='bookmark' title='Plot performance'>Plot performance</a> <small>Undocumented inner plot mechanisms can be used to significantly improved plotting performance...</small></li><li><a
href='http://undocumentedmatlab.com/blog/controlling-plot-data-tips/' rel='bookmark' title='Controlling plot data-tips'>Controlling plot data-tips</a> <small>Data-tips are an extremely useful plotting tool that can easily be controlled programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties/' rel='bookmark' title='Borderless button used for plot properties'>Borderless button used for plot properties</a> <small>A borderless button can be used to add unobtrusive functionality to plot axes...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>I was about to write on something completely different today, when I discovered the following internal Matlab bug (and workaround) that I thought could be useful to some readers, so I changed my plans and the original article will see light next week (or the next, or whenever&#8230;).</p><h3 id="Bug">The bug</h3><p>Try the following code snippet:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">today = <span style="color: #0000FF;">fix</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">now</span><span style="color: #080;">&#41;</span>;
ts = timeseries<span style="color: #080;">&#40;</span><span style="color: #0000FF;">rand</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">201</span><span style="color: #080;">&#41;</span>, <span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span>today-<span style="color: #33f;">200</span><span style="color: #F0F;">:</span>today<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">figure</span>;
hPanel = uipanel<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span>,<span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span>.2,.2,.5,.5<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
hAxes = <span style="color: #0000FF;">axes</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Parent'</span>,hPanel, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'norm'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">plot</span><span style="color: #080;">&#40;</span>ts, <span style="color:#A020F0;">'Parent'</span>,hAxes<span style="color: #080;">&#41;</span>;</pre></div></div><p>As you can see, the plot&#8217;s x-tick labels are all bunched together:<br
/><center><div
class="wp-caption aligncenter" style="width: 290px"><img
alt="Timeseries plot bug (note x tick labels)" src="http://UndocumentedMatlab.com/images/ts_plot_bug_before.png" title="Timeseries plot bug (note x tick labels)" width="280" /><p
class="wp-caption-text">Timeseries plot bug (note x tick labels)</p></div></center></p><h3 id="Workaround">The workaround</h3><p>I debugged the problem and traced it to line #317 of &#8230;\R2011b\toolbox\matlab\timeseries\@timeseries\plot.m (in the R2011b installation):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">axpos = hgconvertunits<span style="color: #080;">&#40;</span>f,<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>ax,<span style="color:#A020F0;">'Position'</span><span style="color: #080;">&#41;</span>,<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>ax,<span style="color:#A020F0;">'Units'</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'normalized'</span>,f<span style="color: #080;">&#41;</span>;</pre></div></div><p>This line uses <i><b>hgconvertunits</b></i> to get the figure-based normalized units of the axes. <i><b>hgconvertunits</b></i> is an internal semi-documented helper function that convert a Handle-Graphics position vector in one set of units into another position vector in other units based on some other reference HG object. For example, it can tell me what the position of an internal control is in normalized figure units, even if the control is deeply nested within multiple panels. Which is exactly what it tries to do in this case.</p><p>Unfortunately, <i><b>hgconvertunits</b></i> apparently has a bug that causes it to return the parent-based normalized position values rather than the figure-based ones in this particular case. This causes the function to think that the axes is much bigger than it really is, and therefore it uses more x-tick labels than it should.</p><p>This <i><b>hgconvertunits</b></i> bug may very possibly affect numerous other Matlab functions that use this built-in function. The correct fix is to fix the <i><b>hgconvertunits</b></i> function. However, since this is an non-modifiable internal function, I found the following workaround for the <i><b>timeseries</b></i> plot only, by replacing the above line with the following:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">axpos = hgconvertunits<span style="color: #080;">&#40;</span>f,getpixelposition<span style="color: #080;">&#40;</span>ax,f<span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'pixel'</span>,<span style="color:#A020F0;">'normalized'</span>,f<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 291px"><img
alt="Timeseries plot fix (note x tick labels)" src="http://UndocumentedMatlab.com/images/ts_plot_bug_after.png" title="Timeseries plot fix (note x tick labels)" width="281" /><p
class="wp-caption-text">Timeseries plot fix (note x tick labels)</p></div></center></p><p>Now the x-tick labels appear nicely, including during resize and zooming.</p><p>This problem occurs on R2011b. I was able to recreate the same problem and use the same fix back to R2010b. I do not know how far earlier the bug goes&#8230;</p><p>I reported the problem today (1-FEM0L7 for anyone interested). If I learn of a patch for <i><b>hgconvertunits</b></i> I will post an addendum on this page.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/plot-liminclude-properties/' rel='bookmark' title='Plot LimInclude properties'>Plot LimInclude properties</a> <small>The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-performance/' rel='bookmark' title='Plot performance'>Plot performance</a> <small>Undocumented inner plot mechanisms can be used to significantly improved plotting performance...</small></li><li><a
href='http://undocumentedmatlab.com/blog/controlling-plot-data-tips/' rel='bookmark' title='Controlling plot data-tips'>Controlling plot data-tips</a> <small>Data-tips are an extremely useful plotting tool that can easily be controlled programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties/' rel='bookmark' title='Borderless button used for plot properties'>Borderless button used for plot properties</a> <small>A borderless button can be used to add unobtrusive functionality to plot axes...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/bug-and-workaround-in-timeseries-plot/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Uitable sorting</title><link>http://undocumentedmatlab.com/blog/uitable-sorting/</link> <comments>http://undocumentedmatlab.com/blog/uitable-sorting/#comments</comments> <pubDate>Tue, 26 Jul 2011 18:00:01 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JIDE]]></category> <category><![CDATA[uitable]]></category> <category><![CDATA[uitools]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2391</guid> <description><![CDATA[Matlab's uitables can be sortable using simple undocumented features<pre> </pre>Related posts:<ol><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/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</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/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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/ref/uitable.html"><i><b>uitable</b></i></a> is probably the most complex basic GUI controls available in Matlab. It displays data in a table within a figure, with settable properties as with any other Matlab Handle-Graphics (HG) control. After many years in which the <i><b>uitable</b></i> was available but <a
target="_blank" href="http://undocumentedmatlab.com/blog/legend-semi-documented-feature/#Semi-documented">semi-documented</a> and not officially supported in Matlab, it finally became fully documented and supported in R2008a (aka Matlab 7.6). At that time its internal implementation has changed from a MathWorks-developed Java table to a <a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/JIDE/">JIDE</a>-based Java table (another JIDE-derived table was described <a
target="_blank" href="http://undocumentedmatlab.com/blog/jide-property-grids/">here</a> last year). Since R2008a, both versions of <i><b>uitable</b></i> are available &#8211; the old version is available by adding the &#8216;v0&#8242; input arg.</p><p>Matlab&#8217;s <i><b>uitable</b></i> exposes only a very limited subset of functionalities and properties to the user. Numerous other functionalities are available by accessing the underlying Java table and hidden Matlab properties. Today I will describe a very <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/answers/1880-uitable-can-the-headers-be-made-clickable">common need</a> in GUI tables, that for some unknown reason is missing in Matlab&#8217;s <i><b>uitable</b></i>: Sorting table data columns.</p><p>Last week I <a
target="_blank" href="http://undocumentedmatlab.com/blog/running-vb-code-in-matlab/">explained</a> how we can modify table headers of an ActiveX table control to display sorting icons. In that case, sorting was built-in the control, and the question was just how to display the sorting arrow icon. Unfortunately, Matlab&#8217;s <i><b>uitable</b></i> does not have sorting built-in, although it&#8217;s quite easy to add it, as I shall now show.</p><h3 id="Old">Old uitable sorting</h3><p>The old <i><b>uitable</b></i> is the default control used until R2007b, or that can be selected with the &#8216;v0&#8242; input arg since R2008a. It was based on an internal MathWorks extension of the standard Java Swing <a
target="_blank" rel="nofollow" href="http://download.oracle.com/javase/tutorial/uiswing/components/table.html">JTable</a> &#8211; a class called <code>com.mathworks.widgets.spreadsheet.SpreadsheetTable</code>.</p><p>Users will normally try to sort columns by clicking the header. This has been a deficiency of JTable for ages. To solve this for the old (pre-R2008a) <i><b>uitable</b></i>, download one of several available JTable sorter classes, or my TableSorter class (available <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14225-java-based-data-table">here</a>).  Add the TableSorter.jar file to your static java classpath (via <code>edit('classpath.txt')</code>) or your dynamic classpath (<code>javaaddpath('TableSorter.jar')</code>).</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display the uitable and get its underlying Java object handle</span>
<span style="color: #080;">&#91;</span>mtable,hcontainer<span style="color: #080;">&#93;</span> = uitable<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color: #0000FF;">gcf</span>, <span style="color: #0000FF;">magic</span><span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>, <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'A'</span>, <span style="color:#A020F0;">'B'</span>, <span style="color:#A020F0;">'C'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;   <span style="color: #228B22;">% discard the 'v0' in R2007b and earlier</span>
jtable = mtable.<span style="">getTable</span>;   <span style="color: #228B22;">% or: get(mtable,'table');</span>
&nbsp;
<span style="color: #228B22;">% We want to use sorter, not data model...</span>
<span style="color: #228B22;">% Unfortunately, UitablePeer expects DefaultTableModel not TableSorter so we need a modified UitablePeer class</span>
<span style="color: #228B22;">% But UitablePeer is a Matlab class, so use a modified TableSorter &amp; attach it to the Model</span>
<span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">which</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'TableSorter'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
   <span style="color: #228B22;">% Add TableSorter as TableModel listener</span>
   sorter = TableSorter<span style="color: #080;">&#40;</span>jtable.<span style="">getModel</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
   jtable.<span style="">setModel</span><span style="color: #080;">&#40;</span>sorter<span style="color: #080;">&#41;</span>;
   sorter.<span style="">setTableHeader</span><span style="color: #080;">&#40;</span>jtable.<span style="">getTableHeader</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
   <span style="color: #228B22;">% Set the header tooltip (with sorting instructions)</span>
   jtable.<span style="">getTableHeader</span>.<span style="">setToolTipText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'&lt;html&gt;&amp;nbsp;&lt;b&gt;Click&lt;/b&gt; to sort up; &lt;b&gt;Shift-click&lt;/b&gt; to sort down&lt;br /&gt;&amp;nbsp;...&lt;/html&gt;'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">else</span>
   <span style="color: #228B22;">% Set the header tooltip (no sorting instructions...)</span>
   jtable.<span style="">getTableHeader</span>.<span style="">setToolTipText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'&lt;html&gt;&amp;nbsp;&lt;b&gt;Click&lt;/b&gt; to select entire column&lt;br /&gt;&amp;nbsp;&lt;b&gt;Ctrl-click&lt;/b&gt; (or &lt;b&gt;Shift-click&lt;/b&gt;) to select multiple columns&amp;nbsp;&lt;/html&gt;'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 607px"><img
alt="Sorted uitable - old version" src="http://www.mathworks.com/matlabcentral/fx_files/14225/1/table.png" title="Sorted uitable - old version" width="597" /><p
class="wp-caption-text">Sorted <i><b>uitable</b></i> - old version</p></div></center></p><h3 id="New">New uitable sorting</h3><p>The new <i><b>uitable</b></i> is based on JIDE&#8217;s <code>com.jidesoft.grid.SortableTable</code> and so has built-in sorting support – all you need to do is to turn it on. First get the underlying Java object using my <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/">FindJObj utility</a>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display the uitable and get its underlying Java object handle</span>
mtable = uitable<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>, <span style="color:#A020F0;">'Data'</span>,<span style="color: #0000FF;">magic</span><span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'ColumnName'</span>,<span style="color: #080;">&#123;</span><span style="color:#A020F0;">'A'</span>, <span style="color:#A020F0;">'B'</span>, <span style="color:#A020F0;">'C'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
jscrollpane = findjobj<span style="color: #080;">&#40;</span>mtable<span style="color: #080;">&#41;</span>;
jtable = jscrollpane.<span style="">getViewport</span>.<span style="">getView</span>;
&nbsp;
<span style="color: #228B22;">% Now turn the JIDE sorting on</span>
jtable.<span style="">setSortable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;		<span style="color: #228B22;">% or: set(jtable,'Sortable','on');</span>
jtable.<span style="">setAutoResort</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
jtable.<span style="">setMultiColumnSortable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
jtable.<span style="">setPreserveSelectionsAfterSorting</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Note: the Matlab <code>mtable</code> handle has a hidden <b>Sortable</b> property, but it has no effect – use the Java property mentioned above instead. I assume that the hidden <b>Sortable</b> property was meant to implement the sorting behavior in R2008a, but MathWorks never got around to actually implement it, and so it remains this way to this day.</p><h3 id="Report">A more detailed report</h3><p>I have prepared a 30-page report about using and customizing Matlab&#8217;s <i><b>uitable</b></i>, which greatly expands on the above. This report is available for a small fee <a
target="_blank" rel="nofollow" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&#038;business=octahedron.ltd@gmail.com&#038;currency_code=USD&#038;amount=35&#038;return=&#038;item_name=Matlab-uitable-report">here</a> (please allow up to 48 hours for email delivery). The report includes the following (more details <a
target="_blank" href="http://undocumentedmatlab.com/blog/uitable-customization-report/">here</a>):</p><ul><li>comparison of the old vs. the new <i><b>uitable</b></i> implementations</li><li>description of the <i><b>uitable</b></i> properties and callbacks</li><li>alternatives to <i><b>uitable</b></i> using a variety of technologies</li><li>updating a specific cell&#8217;s value</li><li>setting background and foreground colors for a cell or column</li><li>using dedicated cell renderer and editor components</li><li>HTML processing</li><li>setting dynamic cell-specific tooltip</li><li>setting dynamic cell-specific drop-down selection options</li><li>using a color-selection drop-down for cells</li><li>customizing scrollbars</li><li>customizing column widths and resizing</li><li>customizing selection behavior</li><li>data sorting (expansion of today&#8217;s article)</li><li>data filtering (similar to Excel&#8217;s data filtering control)</li><li>merging table cells</li><li>programmatically adding/removing rows</li><li>numerous links to online resources</li><li>overview of the JIDE grids package, which contains numerous extremely useful GUI controls and components</li></ul><p><pre> </pre>Related posts:<ol><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/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</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/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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/uitable-sorting/feed/</wfw:commentRss> <slash:comments>16</slash:comments> </item> <item><title>Borderless button used for plot properties</title><link>http://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties/</link> <comments>http://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties/#comments</comments> <pubDate>Wed, 11 May 2011 17:28:35 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[IDS]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2307</guid> <description><![CDATA[A borderless button can be used to add unobtrusive functionality to plot axes<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/plot-liminclude-properties/' rel='bookmark' title='Plot LimInclude properties'>Plot LimInclude properties</a> <small>The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....</small></li><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/plot-type-selection-components/' rel='bookmark' title='Plot-type selection components'>Plot-type selection components</a> <small>Several built-in components enable programmatic plot-type selection in Matlab GUI - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/udd-properties/' rel='bookmark' title='UDD Properties'>UDD Properties</a> <small>UDD provides a very convenient way to add customizable properties to existing Matlab object handles...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A couple of days ago, a reader of this blog has posted a comment asking for advise in enabling the user to dynamically set plot properties. While this can be done using the built-in <i><b>inspect</b></i> function, the reader correctly noted that this presents a list of numerous properties, most of which may not be very interesting for the casual user. So I wanted to use the opportunity to show an alternative mechanism that I have used in one of my applications and I think answers the need. It relies on a border-less button that is located right next to the plot axis origin, and when clicked, presents a simple plot line-style modification dialog window.</p><p>We start by creating a simple Java button (a <code>com.mathworks.mwswing.MJButton</code> in this case) with the simple text &#8216;+&#8217;. The benefit of using <code>com.mathworks.mwswing.MJButton</code> rather than the standard <code>javax.swing.JButton</code>, which <code>MJButton</code> extends, is that <code>MJButton</code> added a <b>FlyOverAppearance</b> property to the standard <code>JButton</code>. This is a very handy feature that enables to present a border-less button, except upon mouse hove, in which case a shadow border is displayed. This is exactly the effect used to highlight toolbar buttons upon mouse hover. To emphasize the button&#8217;s action, we set a HAND_CURSOR cursor whenever the mouse hovers over the button.</p><p>This button is then displayed onscreen using the built-in semi-documented <i><b>javacomponent</b></i> function, at the axes origin position. We set the button&#8217;s callback property to <i><b>uisetlineprops</b></i>, which was adapted from <a
target="_blank" rel="nofollow" href="http://www.mathworks.cn/matlabcentral/fileexchange/14143-uisetlineprops">a File Exchange submission</a> by the same name:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">axesPos = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hAxes,<span style="color:#A020F0;">'pos'</span><span style="color: #080;">&#41;</span>;
btLinePropsCbStr = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'uisetlineprops(findall('</span> <span style="color: #0000FF;">num2str</span><span style="color: #080;">&#40;</span>hAxes,<span style="color: #33f;">99</span><span style="color: #080;">&#41;</span> <span style="color:#A020F0;">','</span><span style="color:#A020F0;">'type'</span><span style="color:#A020F0;">','</span><span style="color:#A020F0;">'line'</span><span style="color:#A020F0;">'))'</span><span style="color: #080;">&#93;</span>;
btLinePropsPos = <span style="color: #080;">&#91;</span>axesPos<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>+<span style="color: #33f;">0.003</span>,<span style="color: #33f;">0.03</span>,<span style="color: #33f;">0.03</span><span style="color: #080;">&#93;</span>;
&nbsp;
<span style="color: #228B22;">% Note: all the following code is just to have a specific cursor</span>
<span style="color: #228B22;">% ^^^^ (HAND_CURSOR) when hovering over the button...</span>
btLineprops = com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">MJButton</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'+'</span><span style="color: #080;">&#41;</span>;
btLineprops.<span style="">setBorder</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
btLineprops.<span style="">setBackground</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Color</span>.<span style="">white</span><span style="color: #080;">&#41;</span>;
btLineprops.<span style="">setCursor</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Cursor</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Cursor</span>.<span style="">HAND_CURSOR</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
btLineprops.<span style="">setFlyOverAppearance</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
btLineprops.<span style="">setToolTipText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Modify properties of plot lines'</span><span style="color: #080;">&#41;</span>;
<span style="color: #080;">&#91;</span>dummy,btContainer<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span>btLineprops,<span style="color: #080;">&#91;</span><span style="color: #33f;">0</span> <span style="color: #33f;">0</span> <span style="color: #33f;">1</span> <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>,hFig<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%#ok</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>btLineprops, <span style="color:#A020F0;">'ActionPerformedCallback'</span>,btLinePropsCbStr<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>btContainer, <span style="color:#A020F0;">'Units'</span>,<span style="color:#A020F0;">'Norm'</span>, <span style="color:#A020F0;">'Position'</span>,btLinePropsPos<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 425px"><img
alt="A borderless button used to modify plot properties" src="http://UndocumentedMatlab.com/images/borderless_button_animated.gif" title="A borderless button used to modify plot properties" width="415" /><p
class="wp-caption-text">A borderless button used to modify plot properties</p></div></center></p><p>The benefit of using this simple trick is that the &#8216;+&#8217; button is unobtrusive, and yet highly accessible. Of course, similar button can be used for a wide variety of callback functionalities, limited only by your imagination!</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/plot-liminclude-properties/' rel='bookmark' title='Plot LimInclude properties'>Plot LimInclude properties</a> <small>The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....</small></li><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/plot-type-selection-components/' rel='bookmark' title='Plot-type selection components'>Plot-type selection components</a> <small>Several built-in components enable programmatic plot-type selection in Matlab GUI - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/udd-properties/' rel='bookmark' title='UDD Properties'>UDD Properties</a> <small>UDD provides a very convenient way to add customizable properties to existing Matlab object handles...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> <item><title>An interesting uitree utility</title><link>http://undocumentedmatlab.com/blog/interesting-uitree-utility/</link> <comments>http://undocumentedmatlab.com/blog/interesting-uitree-utility/#comments</comments> <pubDate>Wed, 30 Mar 2011 18:00:59 +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[Stock Matlab function]]></category> <category><![CDATA[uitools]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2218</guid> <description><![CDATA[ExploreStruct is a utility that shows how custom uitrees can be integrated in Matlab GUI<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree/' rel='bookmark' title='Customizing uitree'>Customizing uitree</a> <small>This article describes how to customize Matlab GUI tree controls created using the undocumented uitree function...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitree/' rel='bookmark' title='uitree'>uitree</a> <small>This article describes the undocumented Matlab uitree function, which displays data in a GUI tree component...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes/' rel='bookmark' title='Customizing uitree nodes &#8211; part 1'>Customizing uitree nodes &#8211; part 1</a> <small>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function...</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>&#8220;uitree&#8221; is consistently one of the most-popular search terms on this site, together with &#8220;uitable&#8221; and &#8220;uitab&#8221;. I also receive frequent questions about tree components in Matlab.</p><p>I already wrote a series of articles on Matlab&#8217;s built-in <a
target="_blank" href="http://undocumentedmatlab.com/blog/legend-semi-documented-feature/#Semi-documented">semi-documented</a> <i><b>uitree</b></i> (starting <a
target="_blank" href="http://undocumentedmatlab.com/blog/uitree/">here</a>), and I am on a constant lookout for additional interesting angles on <i><b>uitree</b></i> to post here. Brett Shoelson&#8217;s recent <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/pick/2011/03/25/looking-back-2005-in-review/">review</a> of 2005&#8242;s Pick-of-the-Week utilities, provided me with such an opportunity.</p><p>One of 2005&#8242;s featured utilities, <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/7828-STRUCTURE%20EXPLORER">Structure Explorer</a> by Hassan Lahdili, originally written using Microsoft&#8217;s TreeView ActiveX control, was apparently rewritten using Matlab&#8217;s Java-based <i><b>uitree</b></i>. This enables the utility to run on non-Windows platforms and have a more consistent look-and-feel with Matlab&#8217;s Java-based GUI:</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fx_files/7828/1/fig2.JPG"><img
alt="Hassan Lahdili's Structure Explorer (click to enlarge)" src="http://www.mathworks.com/matlabcentral/fx_files/7828/1/fig2.JPG" title="Hassan Lahdili's Structure Explorer (click to enlarge)" width="450" height="325" /></a><p
class="wp-caption-text">Hassan Lahdili's Structure Explorer (click to enlarge)</p></div></center></p><p>Hassan&#8217;s ExploreStruct utility, in addition to being useful as-is, provides a very useful learning tool for <i><b>uitree</b></i> integration in Matlab GUI. This utility uses custom icons for different node types, custom callbacks for node expansion (<b>NodeWillExpandCallback</b>) and node selection (<b>NodeSelectedCallback</b>), and tree context (right-click) menu.</p><p>Unfortunately, it appears that the ExploreStruct utility has not been updated for compatibility with the latest Matlab releases. This causes numerous warning messages about not using &#8216;v0&#8242; when calling <i><b>uitree</b></i> and <i><b>uitreenode</b></i>. I also had to add a call to <i><b>drawnow</b></i> following the <i><b>uitree</b></i>&#8216;s creation in line #30, otherwise the tree did not appear (<a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/">due to EDT issues</a>). Also, I had to comment out line #83 (<i>set(root,&#8217;UIContextMenu&#8217;, cmenu);</i>) which caused an error (<i>&#8220;There is no &#8216;UIContextMenu&#8217; property in the &#8216;com.mathworks.hg.peer.UITreeNode&#8217; class&#8221;</i>) &#8211; the tree&#8217;s context-menu actually works even without this line.</p><p>Finally, I had to fix line #182 so that the node icons will appear correctly:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">%pth = [matlabroot, '\work\exp_struct_icons\'];  %old</span>
pth = <span style="color: #080;">&#91;</span><span style="color: #0000FF;">fileparts</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">which</span><span style="color: #080;">&#40;</span>mfilename<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color:#A020F0;">'\exp_struct_icons\'</span><span style="color: #080;">&#93;</span>;  <span style="color: #228B22;">%new</span></pre></div></div><p>ExploreStruct is not documented and does not have extensive error-checking that I would like to see in real-world applications. But it is relatively easy to read and understand due to its use of internal functions and meaningful variable names.</p><p>Altogether, aside from the minor nuances mentioned above, I believe that readers who are interested in implementing custom tree objects in their Matlab GUI can learn a lot from this utility, and you can easily adapt its code for your own needs (or if you can&#8217;t, I am always <a
target="_blank" href="http://undocumentedmatlab.com/consulting/">willing to help</a>).</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree/' rel='bookmark' title='Customizing uitree'>Customizing uitree</a> <small>This article describes how to customize Matlab GUI tree controls created using the undocumented uitree function...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitree/' rel='bookmark' title='uitree'>uitree</a> <small>This article describes the undocumented Matlab uitree function, which displays data in a GUI tree component...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree-nodes/' rel='bookmark' title='Customizing uitree nodes &#8211; part 1'>Customizing uitree nodes &#8211; part 1</a> <small>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function...</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/interesting-uitree-utility/feed/</wfw:commentRss> <slash:comments>1</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 callbacks for Java events</title><link>http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/</link> <comments>http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/#comments</comments> <pubDate>Tue, 30 Nov 2010 22:09:35 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[Listener]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1987</guid> <description><![CDATA[Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/udd-events-and-listeners/' rel='bookmark' title='UDD Events and Listeners'>UDD Events and Listeners</a> <small>UDD event listeners can be used to listen to property value changes and other important events of Matlab objects...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool-callbacks/' rel='bookmark' title='uisplittool &amp; uitogglesplittool callbacks'>uisplittool &#038; uitogglesplittool callbacks</a> <small>Matlab's undocumented uisplittool and uitogglesplittool are powerful toolbar controls - this article explains how to customize their behavior...</small></li><li><a
href='http://undocumentedmatlab.com/blog/udd-and-java/' rel='bookmark' title='UDD and Java'>UDD and Java</a> <small>UDD provides built-in convenience methods to facilitate the integration of Matlab UDD objects with Java code - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A few days ago, a user <a
target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/4291769/how-do-i-notify-a-matlab-object-of-an-event-in-a-java-object">posted a question</a> on StackOverflow asking whether it is possible to trap a Java-based event in a Matlab callback.</p><p>It so happens that only a few weeks ago I completed a consulting project which required exactly this. The project was to integrate a Matlab computational engine with a Java interface to <a
target="_blank" rel="nofollow" href="http://www.interactivebrokers.com/">Interactive Brokers</a> (IB) &#8211; a well-known online brokerage firm. The idea was to use the Java interface to fetch real-time data about securities (stocks, bonds, options etc.), use a Matlab processing utility, then use the Java interface again to send Buy or Sell orders back to IB.</p><p>If you are interested in the final result (i.e., a complete and field-tested Matlab-IB interface), <a
target="_blank" href="http://undocumentedmatlab.com/ib-matlab/">look here</a>.</p><h3 id="challenge">The challenge</h3><p>A big challenge in this project (aside from handling quite a few IB interface quirks), was to propagate events from the Java interface to the Matlab application. This had to be done asynchronously, since events such as order execution can occur at any time following the order placement. Moreover, even simple requests such as retrieving security information (bid/ask prices for example) is handled by IB via Java events, not as simple function return values.</p><p>Handling Java-based events in Matlab is not a trivial task. Not only merely undocumented, but it is also not intuitive. I have spent quite a few hours trying to crack this issue. In fact, I believe it was one of my more challenging tasks in figuring out the undocumented aspects of the Matlab-Java interface. Few other challenges were as difficult, yet with a happy ending (Drag &#038; Drop is a similar issue &#8211; I will describe it in another article sometime).</p><h3 id="solution">The solution</h3><p>Fast-forward all the fruitless attempted variations, here is the bottom line. Refer to the following simple Java class example:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EventTest
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Vector</span> data <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Vector</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> addMyTestListener<span style="color: #009900;">&#40;</span>MyTestListener lis<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        data.<span style="color: #006633;">addElement</span><span style="color: #009900;">&#40;</span>lis<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> removeMyTestListener<span style="color: #009900;">&#40;</span>MyTestListener lis<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        data.<span style="color: #006633;">removeElement</span><span style="color: #009900;">&#40;</span>lis<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> MyTestListener <span style="color: #000000; font-weight: bold;">extends</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">EventListener</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">void</span> testEvent<span style="color: #009900;">&#40;</span>MyTestEvent event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyTestEvent <span style="color: #000000; font-weight: bold;">extends</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">EventObject</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">float</span> oldValue,newValue<span style="color: #339933;">;</span>        
        MyTestEvent<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> obj, <span style="color: #000066; font-weight: bold;">float</span> oldValue, <span style="color: #000066; font-weight: bold;">float</span> newValue<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">oldValue</span> <span style="color: #339933;">=</span> oldValue<span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">newValue</span> <span style="color: #339933;">=</span> newValue<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> notifyMyTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Vector</span> dataCopy<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">synchronized</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            dataCopy <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Vector</span><span style="color: #009900;">&#41;</span>data.<span style="color: #006633;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>dataCopy.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            MyTestEvent event <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyTestEvent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>MyTestListener<span style="color: #009900;">&#41;</span>dataCopy.<span style="color: #006633;">elementAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">testEvent</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>When compiling EventTest.java, three class files are created: EventTest.class, EventTest$MyTestEvent.class and EventTest$MyTestListener.class. Place them on Matlab&#8217;s Java static classpath, using <i><b>edit</b>(&#8216;classpath.txt&#8217;)</i> (using the dynamic classpath causes many problems that using the static classpath solves). They can now be accessed as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">which</span> EventTest
EventTest <span style="color: #0000FF;">is</span> a Java method  <span style="color: #228B22;">% EventTest constructor</span>
&nbsp;
&gt;&gt; evt = EventTest
evt =
EventTest@16166fc
&nbsp;
&gt;&gt; evt.<span style="color: #0000FF;">get</span>
	<span style="color: #0000FF;">Class</span> = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">1</span> by <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span> array<span style="color: #080;">&#93;</span>
	TestEventCallback = 
	TestEventCallbackData = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
&nbsp;
	BeingDeleted = off
	ButtonDownFcn = 
	Children = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	Clipping = on
	CreateFcn = 
	DeleteFcn = 
	BusyAction = queue
	HandleVisibility = on
	HitTest = on
	Interruptible = on
	Parent = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	Selected = off
	SelectionHighlight = on
	Tag = 
	<span style="color: #0000FF;">Type</span> = EventTest
	<span style="color: #0000FF;">UIContextMenu</span> = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	UserData = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	Visible = on
&nbsp;
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>evt<span style="color: #080;">&#41;</span>
	<span style="color: #0000FF;">Class</span>
	TestEventCallback<span style="color: #F0F;">:</span> string -or- <span style="color: #0000FF;">function</span> handle -or- <span style="color: #0000FF;">cell</span> array
	<span style="color: #F0F;">...</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>evt,<span style="color:#A020F0;">'TestEventCallback'</span>,@<span style="color: #080;">&#40;</span>h,e<span style="color: #080;">&#41;</span><span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span>h<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>evt<span style="color: #080;">&#41;</span>
	<span style="color: #0000FF;">Class</span> = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">1</span> by <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span> array<span style="color: #080;">&#93;</span>
	TestEventCallback = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">1</span> by <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> function_handle array<span style="color: #080;">&#93;</span>   &lt;= ok
	TestEventCallbackData = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	<span style="color: #F0F;">...</span>
&nbsp;
&gt;&gt; evt.<span style="">notifyMyTest</span>   <span style="color: #228B22;">% invoke Java event</span>
              <span style="color: #33f;">0.0009765625</span>   <span style="color: #228B22;">% &lt;= Matlab callback</span></pre></div></div><p>Note how Matlab automatically converted the Java event testEvent, declared in interface MyTestListener, into a Matlab callback TestEventCallback (the first character is always capitalized). All Java events are automatically converted in this fashion, by appending a &#8216;Callback&#8217; suffix. Here is a code snippet from R2008a&#8217;s \toolbox\matlab\uitools\@opaque\addlistener.m that shows this (slightly edited):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hSrc = handle<span style="color: #080;">&#40;</span>jobj,<span style="color:#A020F0;">'callbackproperties'</span><span style="color: #080;">&#41;</span>;
allfields = <span style="color: #0000FF;">sortrows</span><span style="color: #080;">&#40;</span>fields<span style="color: #080;">&#40;</span><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hSrc<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">for</span> <span style="color: #0000FF;"><span style="color: #33f;">i</span></span> = <span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>allfields<span style="color: #080;">&#41;</span>
   fn = allfields<span style="color: #080;">&#123;</span><span style="color: #0000FF;"><span style="color: #33f;">i</span></span><span style="color: #080;">&#125;</span>;
   <span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">findstr</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Callback'</span>,fn<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">strrep</span><span style="color: #080;">&#40;</span>fn,<span style="color:#A020F0;">'Callback'</span>,<span style="color:#A020F0;">''</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>
&nbsp;
callback = @<span style="color: #080;">&#40;</span>o,e<span style="color: #080;">&#41;</span> cbBridge<span style="color: #080;">&#40;</span>o,e,response<span style="color: #080;">&#41;</span>;
hdl = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>jobj<span style="color: #080;">&#41;</span>, eventName, callback<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">function</span> cbBridge<span style="color: #080;">&#40;</span>o,e,response<span style="color: #080;">&#41;</span>
   hgfeval<span style="color: #080;">&#40;</span>response, java<span style="color: #080;">&#40;</span>o<span style="color: #080;">&#41;</span>, e.<span style="">JavaEvent</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">end</span></pre></div></div><p>Note that <i><b>hgfeval</b></i>, which is used within the cbBridge callback function, is a semi-documented pure-Matlab built-in function, which I <a
target="_blank" href="http://UndocumentedMatlab.com/blog/hgfeval/">described</a> a few weeks ago.</p><p>If several events have the same case-insensitive name, then the additional callbacks will have an appended underscore character (e.g., &#8216;TestEventCallback_&#8217;):</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// In the Java class:</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> MyTestListener <span style="color: #000000; font-weight: bold;">extends</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">EventListener</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">void</span> testEvent<span style="color: #009900;">&#40;</span>MyTestEvent e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">void</span> testevent<span style="color: #009900;">&#40;</span>TestEvent2 e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% …and back in Matlab:</span>
&gt;&gt; evt=EventTest; evt.<span style="color: #0000FF;">get</span>
	<span style="color: #0000FF;">Class</span> = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">1</span> by <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span> array<span style="color: #080;">&#93;</span>
	TestEventCallback = 
	TestEventCallbackData = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	TestEventCallback_ = 
	TestEventCallback_Data = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span> 
	<span style="color: #F0F;">...</span></pre></div></div><p>To complete this discussion, it should be noted that Matlab also automatically defines corresponding Events in the Java object’s <i><b>classhandle</b></i>. Unfortunately, <i><b>classhandle</b></i> events are not differentiated in a similar manner – in this case only a single event is created, named Testevent. <i><b>classhandle</b></i> events, and their relationship to the preceding discussion, will be described in Donn Scull&#8217;s upcoming series on UDD.</p><p>An alternative to using callbacks on Java events, as shown above, is to use undocumented <a
target="_blank" href="http://UndocumentedMatlab.com/blog/continuous-slider-callback/#Event_Listener"><i><b>handle.listener</b></i>s</a>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>evt<span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'TestEvent'</span>,callback<span style="color: #080;">&#41;</span>;</pre></div></div><p>There are several other odds and ends, but this article should be sufficient for implementing a fully-functional Java event handling mechanism in Matlab. Good luck!</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/udd-events-and-listeners/' rel='bookmark' title='UDD Events and Listeners'>UDD Events and Listeners</a> <small>UDD event listeners can be used to listen to property value changes and other important events of Matlab objects...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool-callbacks/' rel='bookmark' title='uisplittool &amp; uitogglesplittool callbacks'>uisplittool &#038; uitogglesplittool callbacks</a> <small>Matlab's undocumented uisplittool and uitogglesplittool are powerful toolbar controls - this article explains how to customize their behavior...</small></li><li><a
href='http://undocumentedmatlab.com/blog/udd-and-java/' rel='bookmark' title='UDD and Java'>UDD and Java</a> <small>UDD provides built-in convenience methods to facilitate the integration of Matlab UDD objects with Java code - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/feed/</wfw:commentRss> <slash:comments>27</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           category
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-05-18 21:52:32 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/category/semi-documented-function/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      2.170s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1337403150; expires=Sun, 19-May-2013 04:52:30 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Sat, 19 May 2012 04:52:32 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Sat, 19 May 2012 05:52:32 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               7f01f4570e4c391bb1483e7596b37dc6
Content-Encoding:   gzip
-->
