<?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; Listener</title> <atom:link href="http://undocumentedmatlab.com/blog/tag/listener/feed/" rel="self" type="application/rss+xml" /><link>http://undocumentedmatlab.com</link> <description>Charting Matlab's unsupported hidden underbelly</description> <lastBuildDate>Thu, 02 Feb 2012 00:24:18 +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>UDD and Java</title><link>http://undocumentedmatlab.com/blog/udd-and-java/</link> <comments>http://undocumentedmatlab.com/blog/udd-and-java/#comments</comments> <pubDate>Wed, 23 Mar 2011 21:04: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[Undocumented feature]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Donn Shull]]></category> <category><![CDATA[Listener]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2213</guid> <description><![CDATA[UDD provides built-in convenience methods to facilitate the integration of Matlab UDD objects with Java code - this article explains how<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/' rel='bookmark' title='Matlab callbacks for Java events'>Matlab callbacks for Java events</a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/jmi-java-to-matlab-interface/' rel='bookmark' title='JMI &#8211; Java-to-Matlab Interface'>JMI &#8211; Java-to-Matlab Interface</a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/' rel='bookmark' title='FindJObj &#8211; find a Matlab component&#8217;s underlying Java object'>FindJObj &#8211; find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li><li><a
href='http://undocumentedmatlab.com/blog/fixing-a-java-focus-problem/' rel='bookmark' title='Fixing a Java focus problem'>Fixing a Java focus problem</a> <small>Java components added to Matlab GUIs do not participate in the standard focus cycle - this article explains how to fix this problem....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Once again I welcome Donn Shull, who concludes his series on Matlab&#8217;s undocumented UDD mechanism with a discussion of the UDD-Java relationship.</i></p><h3 id="Introduction">Introduction to the UDD-Java relationship</h3><p>Over the course of this series we have mentioned connections between UDD and Java. In <a
target="_blank" href="http://undocumentedmatlab.com/blog/udd-events-and-listeners/">UDD Events and Listeners</a> we described how in Matlab each Java object can have a UDD companion. In <a
target="_blank" href="http://undocumentedmatlab.com/blog/hierarchical-systems-with-udd/">Hierarchical Systems with UDD</a> we briefly noted that a UDD hierarchy may be passed to Java. This suggests that there is a two way relationship between between UDD and Java.</p><p>In this article we will use some undocumented built-in methods such as <b><i>java</i></b> and <b><i>classhandle</i></b> to explore the UDD-Java relationship. We have used built-in methods for UDD objects before. We have also mentioned the importance of studying code from The MathWorks. When you come across something that looks like it may be a UDD method you can check with the <b><i>which</i></b> command:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">which</span> java -<span style="color: #0000FF;">all</span>
C<span style="color: #F0F;">:</span>\MATLAB\R2010b\toolbox\matlab\general\java.<span style="">m</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% javahandle.com.mathworks.hg.peer.Echo method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% ui.figure method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% hg2utils.HGHandle method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% JavaVisible method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% hg.figure method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% hg.GObject method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% schema.class method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% handle.handle method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% schema.method method</span>
C<span style="color: #F0F;">:</span>\MATLAB\R2010b\toolbox\rptgen\rptgen\@sgmltag\java.<span style="">m</span>  <span style="color: #228B22;">% sgmltag method</span></pre></div></div><p>If you find <code>schema.class</code> in the comments for built-in methods, then the method is a general UDD method.</p><h3 id="javahandle">UDD javahandle companions for Java object</h3><p>Whenever a Java class is instantiated in Matlab, it is possible to <a
target="_blank" href="http://undocumentedmatlab.com/blog/udd-events-and-listeners/#Java">create a companion UDD object</a>. The created companion can be in either the <code>javahandle</code> or the <code>javahandle_withcallbacks</code> package. The primary reason for creating the companion object is to avoid <a
rel="nofollow" target="_blank" href="http://mathforum.org/kb/message.jspa?messageID=5950839">memory leaks</a> when attaching a Matlab callback to a callback property. It makes sense in general to use the <code>javahandle_withcallbacks</code> package.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #228B22;">% creage a java instance and the companion UDD object</span>
&gt;&gt; javaFrame = javax.<span style="">swing</span>.<span style="">JFrame</span>;
&nbsp;
&gt;&gt; <span style="color: #228B22;">% dot notation</span>
&gt;&gt; javaFrameUDD = javaFrame.<span style="">handle</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;
&nbsp;
&gt;&gt; <span style="color: #228B22;">% or Matlab notation</span>
&gt;&gt; javaFrameUDD = handle<span style="color: #080;">&#40;</span>javaFrame,<span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>We can use the built-in <b><i>classhandle</i></b> method to inspect our UDD companion object. This can be used, for example, to obtain a list of the events that the Java class generates:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #228B22;">% use classhandle to list a java classes events</span>
&gt;&gt; jch = javaFrame.<span style="">handle</span>.<span style="">classhandle</span>;
&nbsp;
&gt;&gt; <span style="color: #0000FF;">for</span> index = <span style="color: #33f;">1</span><span style="color: #F0F;">:</span>numel<span style="color: #080;">&#40;</span>jch.<span style="">Events</span><span style="color: #080;">&#41;</span>, <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span>jch.<span style="">Events</span><span style="color: #080;">&#40;</span>index<span style="color: #080;">&#41;</span>.<span style="">Name</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">end</span>
MouseWheelMoved
MouseClicked
MouseEntered
MouseExited
MousePressed
MouseReleased
WindowGainedFocus
WindowLostFocus
WindowActivated
WindowClosed
WindowClosing
WindowDeactivated
WindowDeiconified
WindowIconified
WindowOpened
ComponentHidden
ComponentMoved
ComponentResized
ComponentShown
MouseDragged
MouseMoved
ComponentAdded
ComponentRemoved
AncestorMoved
AncestorResized
FocusGained
FocusLost
WindowStateChanged
HierarchyChanged
CaretPositionChanged
InputMethodTextChanged
PropertyChange
KeyPressed
KeyReleased
KeyTyped
&nbsp;
&gt;&gt; <span style="color: #228B22;">% Use one of the object's callbacks</span>
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>javaFrameUDD,<span style="color:#A020F0;">'WindowGainedFocusCallback'</span>,@myCallbackFcn<span style="color: #080;">&#41;</span>;</pre></div></div><p>If we do not wish to use callback properties, then we can create our UDD companion in the <code>javahandle</code> package and use <b><i>handle.listener</i></b> to respond to events.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">javaFrame = javax.<span style="">swing</span>.<span style="">JFrame</span>;
javaFrameUDD = javaFrame.<span style="">handle</span>;
lis = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>javaFrameUDD,<span style="color:#A020F0;">'WindowGainedFocus'</span>,@myCallbackFcn<span style="color: #080;">&#41;</span>;</pre></div></div><h3 id="Passing">Passing UDD objects to Java code</h3><p>You can pass any UDD object to your Java classes in Matlab. Matlab will create a Java bean adapter for the UDD object. The bean adapter created is a subclass of <code>com.MathWorks.jmi.bean.UDDObject</code>. <code>UDDObject</code> implements the Java interfaces <code>com.MathWorks.jmi.bean.DynamicProperties</code>, <code>com.MathWorks.jmi.bean.MTObject</code>, <code>com.MathWorks.jmi.bean.TreeObject</code>, and <code>com.mathworks.services.Browseable</code>.</p><p>The generated bean adapter will have the methods of the parent class the methods of the UDD class, as well as <i>set</i> and <i>get</i> methods for the class properties. To understand how this works, let&#8217;s start with our <code>simple.object</code> and use the <b><i>java</i></b> method to inspect the bean adapter:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myObj = simple.<span style="">object</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'myObj'</span>, <span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;
&nbsp;
&gt;&gt; <span style="color: #228B22;">% using dot notation with the java method </span>
&gt;&gt; myObj.<span style="">java</span>.<span style="">getClass</span>
<span style="color: #0000FF;">ans</span> =
<span style="color: #0000FF;">class</span> objectBeanAdapter0
&nbsp;
&gt;&gt; myObj.<span style="">java</span>.<span style="color: #0000FF;">methods</span>
&nbsp;
<span style="color: #0000FF;">Methods</span> <span style="color: #0000FF;">for</span> <span style="color: #0000FF;">class</span> objectBeanAdapter0<span style="color: #F0F;">:</span>
&nbsp;
acquireReference                    createNullMatlabObjectListener      lastDown                            
addBelow                            createNullPropertyChangeListener    left                                
addBrowseableListener               <span style="color: #0000FF;">dialog</span>                              notify                              
addFirstBelow                       <span style="color: #0000FF;">disp</span>                                notifyAll                           
addLeft                             dispose                             objectBeanAdapter0                  
addMatlabObjectListener             equals                              releaseReference                    
addObjectPropertyChangeListener     findProperty                        removeBrowseableListener            
addRight                            firstDown                           removeMatlabObjectListener          
browseableCanHaveChildren           getChildAt                          removeObjectPropertyChangeListener  
browseableChild                     getChildCount                       right                               
browseableChildCount                getClass                            setDirtyFlag                        
browseableChildFetchCount           getClassName                        setDynamicPropertyValue             
browseableChildren                  getDynamicProperties                setName                             
browseableDataObject                getDynamicPropertyValue             setPropertyValue                    
browseableDisplayObject             getIndex                            setThreadSafetyCheckLevel           
browseableHasChildren               getIndexOfChild                     setValue                            
browseableNChildren                 getName                             toString                            
browseableNextNSiblings             getNewInstance                      up                                  
browseableNextSibling               getPropertyValue                    updateCache                         
browseableParent                    getValue                            updateChildCount                    
browseablePrevNSiblings             hashCode                            updateIndex                         
browseablePrevSibling               isDirty                             wait                                
checkThreadSafety                   isLeaf                              
clearDirtyFlag                      isObservable                        
compareTo                           isValid</pre></div></div><p>The parent class has added a large number of methods to the bean adapter for our original class. By looking at the list we can see our <i>dialog</i> and <i>disp</i> methods. There are also <i>getName</i>, <i>setName</i>, <i>getValue</i>, and <i>setValue</i> methods for our classes properties. The rest of the methods were inherited from the base <code>UDDObject</code> superclass. We can use any superclass method directly with the bean adapter object. For example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myObj.<span style="">java</span>.<span style="">getPropertyValue</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Name'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
myObj</pre></div></div><h3 id="Interface">Java interface class</h3><p>To be able to use our UDD object in user-written Java code, we need a Java interface class for it. While we could manually write an interface file, UDD provides a very handy convenience method to automatically create the interface file. For this, we use the <b><i>classhandle</i></b> method again. The <code>schema.class</code> object obtained using <b><i>classhandle</i></b> has a method called <i>createJavaInterface</i> that takes two string arguments: the Java interface classname, and the folder in which to place the interface file. The steps to create and use this interface file are:</p><ol><li>Create and test your UDD class</li><li>Create a Java interface file using <code>schema.class</code>&#8216;s <i>CreateJavaInterface</i></li><li>Modify your UDD class definition file (schema.m) to reference the Java interface file</li><li>Create the Java code that uses your class</li></ol><p>For example, to create a Java interface file for the simple object we created above, use the following commands in Matlab:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">classH = classhandle<span style="color: #080;">&#40;</span>myObj<span style="color: #080;">&#41;</span>;
classH.<span style="">createJavaInterface</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'simpleObjectInterface'</span>,<span style="color: #0000FF;">pwd</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>This will create the following simpleObjectInterface.java file in the current working directory:</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;">interface</span> simpleObjectInterface 
       <span style="color: #000000; font-weight: bold;">extends</span> com.<span style="color: #006633;">mathworks</span>.<span style="color: #006633;">jmi</span>.<span style="color: #006633;">bean</span>.<span style="color: #006633;">TreeObject</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/* Properties */</span>
    <span style="color: #000000; font-weight: bold;">public</span> java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">String</span> getName<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: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> getValue<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: #000066; font-weight: bold;">void</span> setValue<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/* Methods */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> dialog<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: #000066; font-weight: bold;">void</span> disp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>The interface file contains <i>set</i> and <i>get</i> accessor methods for our UDD object properties, and Java prototypes for the UDD methods (in our case, <i>dialog</i> and <i>disp</i>).</p><p>The next step is to modify our class definition file (schema.m) to reference the Java interface file we have created. This modification provides the information that Matlab needs to create the bean adapter that implements the Java interface:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">simpleClass = schema.<span style="color: #0000FF;">class</span><span style="color: #080;">&#40;</span>simplePackage, <span style="color:#A020F0;">'object'</span><span style="color: #080;">&#41;</span>;
simpleClass.<span style="">JavaInterfaces</span> = <span style="color: #080;">&#123;</span> <span style="color:#A020F0;">'simpleObjectInterface'</span> <span style="color: #080;">&#125;</span>;</pre></div></div><p>We can verify that the generated bean adapter implements the interface using Java Reflection techniques. As always when we have made changes to the class definition file, we need to use the <b><i>clear classes</i></b> command, and then recreate our objects:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myObj = simple.<span style="">object</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'myObj'</span>, <span style="color: #0000FF;">pi</span><span style="color: #080;">&#41;</span>
myObj =
  Name<span style="color: #F0F;">:</span> myObj
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">3.141593</span>
&nbsp;
&gt;&gt; myObjBean = java<span style="color: #080;">&#40;</span>myObj<span style="color: #080;">&#41;</span> 
myObjBean =
  Name<span style="color: #F0F;">:</span> myObj
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">3.141593</span>
&nbsp;
&gt;&gt; interfaces = myObjBean.<span style="">getClass</span>.<span style="">getInterfaces</span> 
interfaces =
java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #F0F;">:</span>
    <span style="color: #080;">&#91;</span>java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span><span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; interfaces<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> 
<span style="color: #0000FF;">ans</span> =
interface simpleObjectInterface</pre></div></div><h3 id="Usage">Using UDD in Java</h3><p>Let&#8217;s create a simple Java class that illustrates passing a UDD object to Java. Here we will just have two methods: The first gets the Value property from a class instance and doubles it; the second launches the class instance dialog:</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> accessUDDClass 
<span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">double</span> localValue<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> accessUDDClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doubleValue<span style="color: #009900;">&#40;</span>simpleObjectInterface UDDObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    localValue <span style="color: #339933;">=</span> UDDObj.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    UDDObj.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">*</span>localValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> launchDialog<span style="color: #009900;">&#40;</span>simpleObjectInterface UDDObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    UDDObj.<span style="color: #006633;">dialog</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: #009900;">&#125;</span></pre></div></div><p>If we have set up the Java compiler and environment variables correctly, we can compile our interface and Java class files from inside Matlab using the system command (alternately, we can compile using any external Java compiler or IDE):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; system<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javac accessUDDClass.java simpleObjectInterface.java'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
     <span style="color: #33f;">0</span></pre></div></div><p>Now test our simple Java class with the UDD object created earlier:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; javaObj = accessUDDClass
javaObj =
accessUDDClass@eb9b73
&nbsp;
&gt;&gt; javaObj.<span style="">doubleValue</span><span style="color: #080;">&#40;</span>myObj<span style="color: #080;">&#41;</span>  <span style="color: #228B22;">% pi =&gt; 2*pi</span>
&nbsp;
&gt;&gt; myObj
myObj =
  Name<span style="color: #F0F;">:</span> myObj
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">6.283185</span></pre></div></div><p>This concludes the UDD series. I would like to thank Yair for his help in preparing and presenting this information.</p><h3 id="Editor">Editor&#8217;s note</h3><p><i>I would like to thank Donn for his enourmously detailed work on UDD, and for preparing it in easy-to-follow articles. I can personally attest to the huge time investment it has taken him. I trully believe he deserves a warm &#8220;thank you&#8221; from the Matlab community. Please visit <a
target="_blank" rel="nofollow" href="http://aetoolbox.com/default.aspx">Donn&#8217;s website</a>, or add a short <a
href="http://UndocumentedMatlab.com/blog/udd-and-java/#respond">comment</a> below.</p><p>In the following weeks, I return to the regular stuff that made this website famous: solving day-to-day Matlab problems using simple undocumented built-in Matlab gems.<br/>- Yair</i></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/' rel='bookmark' title='Matlab callbacks for Java events'>Matlab callbacks for Java events</a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/jmi-java-to-matlab-interface/' rel='bookmark' title='JMI &#8211; Java-to-Matlab Interface'>JMI &#8211; Java-to-Matlab Interface</a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/' rel='bookmark' title='FindJObj &#8211; find a Matlab component&#8217;s underlying Java object'>FindJObj &#8211; find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li><li><a
href='http://undocumentedmatlab.com/blog/fixing-a-java-focus-problem/' rel='bookmark' title='Fixing a Java focus problem'>Fixing a Java focus problem</a> <small>Java components added to Matlab GUIs do not participate in the standard focus cycle - this article explains how to fix this problem....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/udd-and-java/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>UDD Events and Listeners</title><link>http://undocumentedmatlab.com/blog/udd-events-and-listeners/</link> <comments>http://undocumentedmatlab.com/blog/udd-events-and-listeners/#comments</comments> <pubDate>Wed, 16 Mar 2011 18:43:16 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Donn Shull]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listener]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[schema]]></category> <category><![CDATA[schema.prop]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2200</guid> <description><![CDATA[UDD event listeners can be used to listen to property value changes and other important events of Matlab objects<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/' rel='bookmark' title='Matlab callbacks for Java events'>Matlab callbacks for Java events</a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/detecting-window-focus-events/' rel='bookmark' title='Detecting window focus events'>Detecting window focus events</a> <small>Matlab does not have any documented method to detect window focus events (gain/loss). This article describes an undocumented way to detect such events....</small></li><li><a
href='http://undocumentedmatlab.com/blog/introduction-to-udd/' rel='bookmark' title='Introduction to UDD'>Introduction to UDD</a> <small>UDD classes underlie many of Matlab's handle-graphics objects and functionality. This article introduces these classes....</small></li><li><a
href='http://undocumentedmatlab.com/blog/creating-a-simple-udd-class/' rel='bookmark' title='Creating a simple UDD class'>Creating a simple UDD class</a> <small>This article explains how to create and test custom UDD packages, classes and objects...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Donn Shull continues his exploration of the undocumented UDD mechanism, today discussing the important and extremely useful topic of UDD events</i></p><h3 id="model">The UDD event model</h3><p>The UDD event model is very similar to the MCOS event model. There is an excellent <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/matlab_oop/bqvggvt.html">discussion</a> of the MCOS event model in Matlab&#8217;s official documentation. Most of the MCOS information also applies to UDD if you make the following substitutions:</p><table><tbody><tr><th
bgcolor="#D0D0D0">MCOS Event Model</th><th
bgcolor="#D0D0D0">UDD Event Model</th></tr><tr><td
bgcolor="#E7E7E7">notify</td><td
bgcolor="#E7E7E7">send</td></tr><tr><td
bgcolor="#E7E7E7">event.EventData</td><td
bgcolor="#E7E7E7">handle.EventData</td></tr><tr><td
bgcolor="#E7E7E7">events block</td><td
bgcolor="#E7E7E7">schema.event</td></tr><tr><td
bgcolor="#E7E7E7">event.listener</td><td
bgcolor="#E7E7E7">handle.listener</td></tr><tr><td
bgcolor="#E7E7E7">PreGet, PreSet</td><td
bgcolor="#E7E7E7">PropertyPreGet, PropertPreSet</td></tr><tr><td
bgcolor="#E7E7E7">PostGet, PostSet</td><td
bgcolor="#E7E7E7">PropertyPostGet, PropertyPostSet</td></tr></tbody></table><h3 id="handler">Event handler functions</h3><p>To begin the UDD event model discussion we will start at the end, with the event handler. The event handler function requires at least two input arguments: the source object which triggered the event, and an object of type <code>handle.EventData</code> or a subclass of <code>handle.EventData</code>.</p><p>To demonstrate how this works, let&#8217;s write a simple event handler function. This event handler will display the class of the source event and the class of the event data:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> displayEventInfo<span style="color: #080;">&#40;</span>source, eventData<span style="color: #080;">&#41;</span>
<span style="color: #228B22;">%DISPLAYEVENTINFO display the classes of source, data objects</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   DISPLAYEVENTINFO(SOURCE, EVENTDATA) returns the classes</span>
<span style="color: #228B22;">%   of the source object and the event data object</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   INPUTS:</span>
<span style="color: #228B22;">%       SOURCE    : the event source</span>
<span style="color: #228B22;">%       EVENTDATA : the event data</span>
  <span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>source<span style="color: #080;">&#41;</span>
    <span style="color: #0000FF;">fprintf</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>, <span style="color:#A020F0;">'The source object class is: %s'</span>,<span style="color: #0000FF;">class</span><span style="color: #080;">&#40;</span>source<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>eventData<span style="color: #080;">&#41;</span>
    <span style="color: #0000FF;">fprintf</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>, <span style="color:#A020F0;">'The event data class is: %s'</span>,<span style="color: #0000FF;">class</span><span style="color: #080;">&#40;</span>eventData<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span></pre></div></div><h3 id="listener">Creating a listener</h3><p>In the section on <a
target="_blank" href="http://undocumentedmatlab.com/blog/creating-a-simple-udd-class/">Creating a Simple UDD Class</a> we used <code>schema.event</code> in our <code>simple.object</code> class definition file to create a <code>simpleEvent</code> event. We now create an instance of <code>simple.object</code>, then use <b><i>handle.listener</i></b> to wait (&#8220;listen&#8221;) for the <code>simpleEvent</code> event to occur and call the <i>displayEventInfo</i> event handler function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">a = simple.<span style="">object</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'a'</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>a,<span style="color:#A020F0;">'simpleEvent'</span>,@displayEventInfo<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">setappdata</span><span style="color: #080;">&#40;</span>a, <span style="color:#A020F0;">'listeners'</span>, hListener<span style="color: #080;">&#41;</span>;</pre></div></div><p><b><u>Important:</u></b> The <code>hListener</code> handle must remain stored somewhere in Matlab memory, or the listener will not be used. For this reason, it is good practice to attach the listener handle to the listened object, using the <i><b>setappdata</b></i> function, as was done above. The listener will then be alive for exactly as long as its target object is alive.</p><h3 id="EventData">Creating an EventData object</h3><p>Next, create the <code>handle.EventData</code> object. The <code>handle.EventData</code> object constructor requires two arguments: an instance of the events source object, and the name of the event:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">evtData = handle.<span style="">EventData</span><span style="color: #080;">&#40;</span>a, <span style="color:#A020F0;">'simpleEvent'</span><span style="color: #080;">&#41;</span></pre></div></div><h3 id="event">Generating an event</h3><p>The last step is actually triggering an event. This is done by issuing the <i><b>send</b></i> command for the specified object, event name and event data:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; a.<span style="">send</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'simpleEvent'</span>, evtData<span style="color: #080;">&#41;</span>
The source object <span style="color: #0000FF;">class</span> <span style="color: #0000FF;">is</span><span style="color: #F0F;">:</span> simple.<span style="">object</span>
The event data <span style="color: #0000FF;">class</span> <span style="color: #0000FF;">is</span><span style="color: #F0F;">:</span> handle.<span style="">EventData</span></pre></div></div><p>If there is other information that you wish to pass to the callback function you can create a subclass of the <code>handle.EventData</code>. Add properties to hold your additional information and use your subclass as the second argument of the <i><b>send</b></i> method.</p><h3 id="builtin">Builtin UDD events</h3><p>The builtin <code>handle</code> package has six event data classes which are subclasses of the base <code>handle.EventData</code> class. Each of these classes is paired with specific UDD events that Matlab generates. Actions that trigger these events include creating/destroying an object, adding/removing objects from a hierarchy, and getting/setting property values. The following table lists the event names and <code>handle.*EventData</code> data types returned for these events:</p><table><tbody><tr><th
bgcolor="#D0D0D0">event data type</th><th
bgcolor="#D0D0D0">event trigger</th></tr><tr><td
bgcolor="#E7E7E7">handle.ClassEventData</td><td
bgcolor="#E7E7E7">ClassInstanceCreated</td></tr><tr><td
bgcolor="#E7E7E7">handle.EventData</td><td
bgcolor="#E7E7E7">ObjectBeingDestroyed</td></tr><tr><td
bgcolor="#E7E7E7">handle.ChildEventData</td><td
bgcolor="#E7E7E7">ObjectChildAdded, ObjectChildRemoved</td></tr><tr><td
bgcolor="#E7E7E7">handle.ParentEventData</td><td
bgcolor="#E7E7E7">ObjectParentChanged</td></tr><tr><td
bgcolor="#E7E7E7">handle.PropertyEventData</td><td
bgcolor="#E7E7E7">PropertyPreGet, PropertyPostGet</td></tr><tr><td
bgcolor="#E7E7E7">handle.PropertySetEventData</td><td
bgcolor="#E7E7E7">PropertyPreSet, PropertyPostSet</td></tr></tbody></table><p>As an example of some of these events let&#8217;s look at a <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/303232">question</a> recently asked on the CSSM newsgroup. The basic idea is that we want to monitor an axis, automatically make any added lines to be green in color, and prevent patches from being added.</p><p>The solution is to monitor the <code>ObjectChildAdded</code> event for an axis. We will write an event handler which checks the <code>handle.ChildEventData</code> to see what type of child was added. In the case of lines we will set their color to green; patch objects will be deleted from the axis. Here is our event handler function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> modifyAxesChildren<span style="color: #080;">&#40;</span>~, eventData<span style="color: #080;">&#41;</span>
<span style="color: #228B22;">%MODIFYAXESCHILDREN monitor and axis and modify added children</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   MODIFYAXESCHILDREN(SOURCE,EVENTDATA) is an event handler to</span>
<span style="color: #228B22;">%   change newly-added lines to green and remove added patches</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   INPUTS:</span>
<span style="color: #228B22;">%       EVENTDATA : handle.ChildEventData object</span>
   <span style="color: #0000FF;">switch</span> eventData.<span style="">Child</span>.<span style="">classhandle</span>.<span style="">Name</span>
      <span style="color: #0000FF;">case</span> <span style="color:#A020F0;">'line'</span>
         eventData.<span style="">Child</span>.<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Color'</span>, <span style="color:#A020F0;">'green'</span><span style="color: #080;">&#41;</span>;
         <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Color changed to green.'</span><span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">case</span> <span style="color:#A020F0;">'patch'</span>
         eventData.<span style="">Child</span>.<span style="color: #0000FF;">delete</span>;
         <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Patch removed.'</span><span style="color: #080;">&#41;</span>
   <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span></pre></div></div><p>Next create an axis, and a listener which is triggered when children are added:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% create a new axes and get its handle</span>
a = hg.<span style="color: #0000FF;">axes</span>;
&nbsp;
<span style="color: #228B22;">% create the listener</span>
listen = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>a, <span style="color:#A020F0;">'ObjectChildAdded'</span>, @modifyAxesChildren<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% add a line</span>
&gt;&gt; hg.<span style="color: #0000FF;">line</span>;
Color changed to green.
&nbsp;
<span style="color: #228B22;">% try to add a patch</span>
&gt;&gt; hg.<span style="color: #0000FF;">patch</span>;
<span style="color: #0000FF;">Patch</span> removed.</pre></div></div><p>Removing a child with either the <i>delete</i> or the <i>disconnect</i> method generates an <code>ObjectChildRemoved</code> event. The <i>delete</i> method also generates the <code>ObjectBeingDestroyed</code> event. Changing a child&#8217;s parent with the <i>up</i> method generates an <code>ObjectParentChanged</code> event.</p><p>Reading an object&#8217;s properties with either dot notation or with the <i>get</i> method generates <code>PropertyPreGet</code> and <code>PropertyPostGet</code> events.</p><p>Changing the value of a property generates the <code>PropertyPreSet</code> and <code>PropertyPostSet</code> events. As we saw in the section on <a
target="_Blank" href="http://undocumentedmatlab.com/blog/udd-properties/">UDD properties</a>, when the <b>AbortSet</b> access flag is &#8216;on&#8217;, property set events are only generated when a <i><b>set</b></i> operation actually changes the value of the property (as opposed to leaving it unchanged).</p><p>Note that the <b><i>handle.listener</i></b> syntax is slightly different for property events:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hProp = <span style="color: #0000FF;">findprop</span><span style="color: #080;">&#40;</span>a, <span style="color:#A020F0;">'Value'</span><span style="color: #080;">&#41;</span>;
hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>a,hProp,<span style="color:#A020F0;">'PropertyPreGet'</span>,@displayEventInfo<span style="color: #080;">&#41;</span>;</pre></div></div><h3 id="Java">Java events</h3><p>The final specialized event data object in the handle package is <code>handle.JavaEventData</code>. In Matlab, Java classes are not UDD classes, but each Java instance can have a UDD <i>peer</i>. The peer is created using the <i><b>handle</b></i> function. The Java peers are created in either UDD&#8217;s <code>javahandle</code> package or the <code>javahandle_withcallbacks</code> package. As their names imply, the latter enables listening to Java-triggered events using a Matlab callback.</p><p>To illustrate how this works we will create a Java Swing <code>JFrame</code> and listen for <code>MouseClicked</code> events:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Create the Java Frame</span>
javaFrame = javax.<span style="">swing</span>.<span style="">JFrame</span>;
javaFrame.<span style="">setSize</span><span style="color: #080;">&#40;</span><span style="color: #33f;">200</span>, <span style="color: #33f;">200</span><span style="color: #080;">&#41;</span>;
javaFrame.<span style="">show</span>;
&nbsp;
<span style="color: #228B22;">% Create a UDD peer for the new JFrame (two alternatives)</span>
javaFramePeer = javaFrame.<span style="">handle</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% alternative #1</span>
javaFramePeer = handle<span style="color: #080;">&#40;</span>javaFrame, <span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% alternative #2</span>
&nbsp;
<span style="color: #228B22;">% Create the a listener for the Java MouseClicked event</span>
listen = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>javaFramePeer, <span style="color:#A020F0;">'MouseClicked'</span>, @displayEventInfo<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="a simple Java Swing JFrame" src="http://UndocumentedMatlab.com/images/UDD_Java_Frame.png" title="a simple Java Swing JFrame" width="200" height="200" /><p
class="wp-caption-text">a simple Java Swing JFrame</p></div></center></p><p>When we click on the JFrame, our UDD peer triggers the callback:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;">The source object <span style="color: #000000; font-weight: bold;">class</span> is<span style="color: #339933;">:</span> javahandle_withcallbacks.<span style="color: #006633;">javax</span>.<span style="color: #006633;">swing</span>.<span style="color: #003399;">JFrame</span>
The event data <span style="color: #000000; font-weight: bold;">class</span> is<span style="color: #339933;">:</span> handle.<span style="color: #006633;">JavaEventData</span></pre></div></div><p>Since we created our peer in the <code>javahandle_withcallbacks</code> package, it is not necessary to create a listener using <i><b>handle.listener</b></i>. If we place our callback function handle in the <b>MouseClickedCallback</b> property it will be executed whenever the <code>MouseClicked</code> event is triggered. Such <b>*Callback</b> properties are automatically generated by Matlab when it creates the UDD peer (<a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/">details</a>).</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">clear</span> listen
javaFramePeer.<span style="">MouseClickedCallback</span> = @displayEventInfo</pre></div></div><p>This will work the same as before without the need to create and maintain a <b><i>handle.listener</i></b> object. If we had created our UDD peer in the <code>javahandle</code> package rather than <code>javahandle_withcallbacks</code>, we would not have the convenience of the <b>MouseClickedCallback</b> property, but we could still use the <b><i>handle.listener</i></b> mechanism to monitor events.</p><h3 id="custom">Creating callback properties for custom UDD classes</h3><p>It is easy to add callback properties to user created UDD objects. The technique involves embedding a <code>handle.listener</code> object in the UDD object. To illustrate this, we add a <b>SimpleEventCallback</b> property to our <code>simple.object</code>, then use a <b>SimpleEventListener</b> property to hold our embedded <b><i>handle.listener</i></b>. Add the following to <code>simple.object</code>&#8216;s schema.m definition file:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">   <span style="color: #228B22;">% Property to hold our callback handle</span>
   prop = schema.<span style="">prop</span><span style="color: #080;">&#40;</span>simpleClass, <span style="color:#A020F0;">'SimpleEventCallback'</span>, <span style="color:#A020F0;">'MATLAB callback'</span><span style="color: #080;">&#41;</span>;
   prop.<span style="">setFunction</span> = @setValue;
&nbsp;
   <span style="color: #228B22;">% hidden property to hold the listener for our callback</span>
   prop = schema.<span style="">prop</span><span style="color: #080;">&#40;</span>simpleClass, <span style="color:#A020F0;">'SimpleEventListener'</span>, <span style="color:#A020F0;">'handle'</span><span style="color: #080;">&#41;</span>;
   prop.<span style="">Visible</span> = <span style="color:#A020F0;">'off'</span>;
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #0000FF;">function</span> propVal = setValue<span style="color: #080;">&#40;</span>self, value<span style="color: #080;">&#41;</span>
   <span style="color: #228B22;">%SETVALUE function to transfer function handle from callback property to listener</span>
   self.<span style="">SimpleEventListener</span>.<span style="">Callback</span> = value;
   propVal = value;
<span style="color: #0000FF;">end</span></pre></div></div><p>Next we add the following to our simple.object constructor file:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% set the hidden listener property to a handle.listener</span>
simpleObject.<span style="">SimpleEventListener</span> = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>simpleObject, <span style="color:#A020F0;">'simpleEvent'</span>, <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Now if we set the <b>SimpleObjectCallback</b> property to a function handle, the handle is transferred to the embedded <b><i>handle.listener</i></b> Callback property. When a <code>simpleEvent</code> event is generated, our <code>SimpleEventCallback</code> function will be executed.</p><p>This series will conclude next week with a look at the special relationship between UDD and Java.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/' rel='bookmark' title='Matlab callbacks for Java events'>Matlab callbacks for Java events</a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/detecting-window-focus-events/' rel='bookmark' title='Detecting window focus events'>Detecting window focus events</a> <small>Matlab does not have any documented method to detect window focus events (gain/loss). This article describes an undocumented way to detect such events....</small></li><li><a
href='http://undocumentedmatlab.com/blog/introduction-to-udd/' rel='bookmark' title='Introduction to UDD'>Introduction to UDD</a> <small>UDD classes underlie many of Matlab's handle-graphics objects and functionality. This article introduces these classes....</small></li><li><a
href='http://undocumentedmatlab.com/blog/creating-a-simple-udd-class/' rel='bookmark' title='Creating a simple UDD class'>Creating a simple UDD class</a> <small>This article explains how to create and test custom UDD packages, classes and objects...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/udd-events-and-listeners/feed/</wfw:commentRss> <slash:comments>9</slash:comments> </item> <item><title>Introduction to UDD</title><link>http://undocumentedmatlab.com/blog/introduction-to-udd/</link> <comments>http://undocumentedmatlab.com/blog/introduction-to-udd/#comments</comments> <pubDate>Wed, 16 Feb 2011 18:00:09 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Donn Shull]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[Listener]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[schema]]></category> <category><![CDATA[schema.class]]></category> <category><![CDATA[schema.prop]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2036</guid> <description><![CDATA[UDD classes underlie many of Matlab's handle-graphics objects and functionality. This article introduces these classes.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/creating-a-simple-udd-class/' rel='bookmark' title='Creating a simple UDD class'>Creating a simple UDD class</a> <small>This article explains how to create and test custom UDD packages, classes and objects...</small></li><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/hierarchical-systems-with-udd/' rel='bookmark' title='Hierarchical Systems with UDD'>Hierarchical Systems with UDD</a> <small>UDD objects can be grouped in structured hierarchies - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/new-information-on-hg2/' rel='bookmark' title='New information on HG2'>New information on HG2</a> <small>More information on Matlab's new HG2 object-oriented handle-graphics system...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>I would like to welcome guest blogger <a
target="_blank" rel="nofollow" href="http://aetoolbox.com/">Donn Shull</a>. Donn will present a series of articles about UDD classes and objects, on which many undocumented Matlab features and functions are based.</i></p><h3 id="Background">Background on UDD</h3><p>Matlab has used objects for a long time. In R8 (Matlab 5.0), their first user accessible class system was introduced. Andy Register wrote a <a
target="_blank" rel="nofollow" href="http://www.scitechpub.com/catalog/product_info.php?products_id=386">detailed reference</a> on using this system. Although that original system is obsolete, it is still available in R24 (R2010b).</p><p>UDD objects (also referred to as <i>schema</i> objects) were introduced with R12 (Matlab 6.0). UDD has been a foundation platform for a number of core Matlab technologies. MathWorks have consistently maintained that UDD is only meant for internal development and not for Matlab users. So, while UDD has no formal documentation, there are plenty of examples and tools to help us learn about it.</p><p>It is somewhat odd that despite Matlab&#8217;s new object-oriented system (<a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/matlab_oop/bri1rtu.html">MCOS</a>)&#8217;s introduction 3 years ago, and the <a
target="_blank" href="http://UndocumentedMatlab.com/blog/tag/hg2/">ongoing concurrent development of HG2</a> classes, the older-technology UDD is still being actively developed, as evidenced by the increasing number of UDD classes in recent releases. More background on the differences between these different sets of classes can be found <a
target="_blank" href="http://UndocumentedMatlab.com/blog/new-information-on-hg2/">here</a>.</p><h3 id="WhyBother">Why should we bother learning UDD?</h3><p>There are some things to consider before deciding if you want to spend the time to learn about the UDD class system:</p><h4 id="Pros">The case against studying UDD classes</h4><ul><li>There is no documentation from The MathWorks for these classes</li><li>You will not get any help from The MathWorks in applying these classes</li><li>The UDD system is now more than a decade old and may be phased out in future Matlab releases (perhaps in HG2?)</li></ul><h4 id="Cons">The case for studying UDD classes</h4><ul><li>UDD is currently the foundation of handle graphics, Java integration, COM, and Simulink</li><li>The m code versions of UDD may be considered a forerunner of the newer MCOS class system</li><li>To avoid memory leaks when using Callbacks in GUI applications you currently need to use UDD</li><li>UDD techniques facilitate Matlab interaction with Java GUIs</li><li>UDD directly supports the Matlab style method invocation as well as dot notation for methods without the need to write subsasgn and subsref routines</li></ul><h3 id="Tools">Tools for Learning about UDD</h3><p>We start by describing some undocumented Matlab tools that will help us investigate and understand UDD classes.</p><ul><li><i><b>findpackage</b></i> &#8211; All UDD Classes are defined as members of a package. findpackage takes the package name as an input argument and returns a schema.package object which provides information about the package</li><li><i><b>findclass</b></i> &#8211; This method of the schema.package object returns a schema.class object of the named class if the class exists in the package</li><li><i><b>classhandle</b></i> &#8211; For a given UDD object <i><b>classhandle</b></i> returns a schema.class object with information about the class. <i><b>classhandle</b></i> and <i><b>findclass</b></i> are two ways of getting the same information about a UDD class. <i><b>findclass</b></i> works with a <i><b>schema.package</b></i> object and a class name and does not require an instance of the class. <i><b>classhandle</b></i> works with an instance of a class</li><li><i><b>findprop</b></i> &#8211; This method of the schema.class object returns a schema.prop object which contains information about the named property</li><li><i><b>findevent</b></i> &#8211; This method of the schema.class object returns a schema.prop object which contains information about the named event</li><li><i><b>handle</b></i> &#8211; handle is a multifaceted and unique term for The MathWorks. There are both UDD and MCOS handle classes. There is a UDD handle package. In terms of the tools we need, <i><b>handle</b></i> is also an undocumented function which converts a numeric handle into a UDD handle object. Depending on your background you may want to think of <i><b>handle</b></i> as a cast operator which casts a numeric handle into a UDD object.</li><li><i><b>methods</b></i> &#8211; This is used to display the methods of an object</li><li><i><b>methodsview</b></i> &#8211; Provides a graphic display of an objects methods</li><li><a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/17935-uiinspect-display-methods-properties-callbacks-of-an-object"><i><b>uiinspect</b></i></a> &#8211; Yair Altman&#8217;s object inspection tool, which can be used for COM, Java and Matlab classes (<i><b>uiinspect</b> will be described in a separate article in the near future</i>).</li></ul><p>Before we apply these tools we need to discuss the basic structure of UDD classes. Let&#8217;s compare them with the newer, well documented MCOS classes:</p><p>MCOS classes can be defined simply as a standalone class or scoped by placing the class in a package or a hierarchy of packages. With UDD, all classes must be defined in a package. UDD Packages are not hierarchical so a UDD package may not contain other packages. UDD classes can always be instantiated with syntax of packageName.className. By default MCOS classes are value classes. With MCOS you can subclass the handle class to create handle classes. UDD classes are handle classes by default, but it is possible to create UDD value classes.</p><h3 id="Exploring">Exploring some important built-in UDD Classes</h3><p>The current versions of Matlab include a number of built-in UDD packages. We will use our new tools to see what we can learn about these packages. Let us begin by inspecting the two packages that form the basis of the UDD class system.</p><h4 id="schema">The schema package</h4><p>The built-in schema package contains the classes for creating user written UDD classes. It also is used to provide meta information about UDD classes. Using <i><b>findpackage</b></i> we will obtain a schema.package object for the schema package and then use it obtain information about the classes it contains:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; pkg = findpackage<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'schema'</span><span style="color: #080;">&#41;</span>
pkg =
        schema.<span style="">package</span>
&nbsp;
&gt;&gt; pkg.<span style="color: #0000FF;">get</span>
               Name<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'schema'</span>
    DefaultDatabase<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 handle.<span style="">Database</span><span style="color: #080;">&#93;</span>
            Classes<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>9x1 schema.<span style="color: #0000FF;">class</span><span style="color: #080;">&#93;</span>
          Functions<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>0x1 handle<span style="color: #080;">&#93;</span>
        JavaPackage<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
         Documented<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span></pre></div></div><p>Note that here we have used the dot-notation pkg.<i><b>get</b></i> &#8211; we could also have used the Matlab notation <i><b>get</b>(pkg)</i> instead.</p><p>We have now learned that that there are nine classes in the schema package. The information about them in a schema package&#8217;s <b>Classes</b> property. To see the information about individual classes we inspect this property:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; pkg.<span style="">Classes</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>.<span style="color: #0000FF;">get</span>
               Name<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'class'</span>
            Package<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 schema.<span style="">package</span><span style="color: #080;">&#93;</span>
        Description<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
        AccessFlags<span style="color: #F0F;">:</span> <span style="color: #080;">&#123;</span>0x1 <span style="color: #0000FF;">cell</span><span style="color: #080;">&#125;</span>
             <span style="color: #0000FF;">Global</span><span style="color: #F0F;">:</span> <span style="color:#A020F0;">'off'</span>
             Handle<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
       Superclasses<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>0x1 handle<span style="color: #080;">&#93;</span>
    SuperiorClasses<span style="color: #F0F;">:</span> <span style="color: #080;">&#123;</span>0x1 <span style="color: #0000FF;">cell</span><span style="color: #080;">&#125;</span>
    InferiorClasses<span style="color: #F0F;">:</span> <span style="color: #080;">&#123;</span>0x1 <span style="color: #0000FF;">cell</span><span style="color: #080;">&#125;</span>
            <span style="color: #0000FF;">Methods</span><span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>4x1 schema.<span style="">method</span><span style="color: #080;">&#93;</span>
         Properties<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>13x1 schema.<span style="">prop</span><span style="color: #080;">&#93;</span>
             Events<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
     JavaInterfaces<span style="color: #F0F;">:</span> <span style="color: #080;">&#123;</span>0x1 <span style="color: #0000FF;">cell</span><span style="color: #080;">&#125;</span></pre></div></div><p>Not surprisingly, the first class in the schema.package is &#8216;class&#8217; itself. Here we can see that schema.class has 4 methods and 13 properties. We can also see that the schema.class objects have a <b>Name</b> property. Let&#8217;s use that information to list all the classes in the schema package:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; names = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>numel<span style="color: #080;">&#40;</span>pkg.<span style="">Classes</span><span style="color: #080;">&#41;</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">for</span> index = <span style="color: #33f;">1</span><span style="color: #F0F;">:</span>numel<span style="color: #080;">&#40;</span>names<span style="color: #080;">&#41;</span>, names<span style="color: #080;">&#123;</span>index<span style="color: #080;">&#125;</span> = pkg.<span style="">Classes</span><span style="color: #080;">&#40;</span>index<span style="color: #080;">&#41;</span>.<span style="">Name</span>; <span style="color: #0000FF;">end</span>;
&gt;&gt; names
names =
    <span style="color:#A020F0;">'class'</span>
    <span style="color:#A020F0;">'method'</span>
    <span style="color:#A020F0;">'signature'</span>
    <span style="color:#A020F0;">'package'</span>
    <span style="color:#A020F0;">'event'</span>
    <span style="color:#A020F0;">'prop'</span>
    <span style="color:#A020F0;">'type'</span>
    <span style="color:#A020F0;">'EnumType'</span>
    <span style="color:#A020F0;">'UserType'</span></pre></div></div><p>These are the base classes for the UDD package schema. To illustrate a different way to get information, let&#8217;s use the <i><b>findclass</b></i> method of schema.package to get information about the schema.prop class:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; p = findclass<span style="color: #080;">&#40;</span>pkg, <span style="color:#A020F0;">'prop'</span><span style="color: #080;">&#41;</span>
p =
        schema.<span style="color: #0000FF;">class</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>p<span style="color: #080;">&#41;</span>
               Name<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'prop'</span>
            Package<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 schema.<span style="">package</span><span style="color: #080;">&#93;</span>
        Description<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
        AccessFlags<span style="color: #F0F;">:</span> <span style="color: #080;">&#123;</span>0x1 <span style="color: #0000FF;">cell</span><span style="color: #080;">&#125;</span>
             <span style="color: #0000FF;">Global</span><span style="color: #F0F;">:</span> <span style="color:#A020F0;">'off'</span>
             Handle<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
       Superclasses<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>0x1 handle<span style="color: #080;">&#93;</span>
    SuperiorClasses<span style="color: #F0F;">:</span> <span style="color: #080;">&#123;</span>0x1 <span style="color: #0000FF;">cell</span><span style="color: #080;">&#125;</span>
    InferiorClasses<span style="color: #F0F;">:</span> <span style="color: #080;">&#123;</span>0x1 <span style="color: #0000FF;">cell</span><span style="color: #080;">&#125;</span>
            <span style="color: #0000FF;">Methods</span><span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>2x1 schema.<span style="">method</span><span style="color: #080;">&#93;</span>
         Properties<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>9x1 schema.<span style="">prop</span><span style="color: #080;">&#93;</span>
             Events<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
     JavaInterfaces<span style="color: #F0F;">:</span> <span style="color: #080;">&#123;</span>0x1 <span style="color: #0000FF;">cell</span><span style="color: #080;">&#125;</span></pre></div></div><h4 id="handle">The handle package</h4><p>The second basic UDD package is the handle package. Handle holds a special place in Matlab and has multiple meanings: Handle is a type of Matlab object that is passed by reference; <i><b>handle</b></i> is a function which converts a numeric handle to an object; handle is an abstract object in the new MCOS class system and handle is also a UDD package as well as the default type for UDD objects.</p><p>There is an interesting connection between UDD and MCOS that involves handle. In Matlab releases R12 through R2007b, the UDD handle package had up to 12 classes and did not have any package functions (package functions are functions which are scoped to a package; their calling syntax is [outputs] = packageName.<i>functionName(inputs)</i>).</p><p>Beginning with the formal introduction of MCOS in R2008a, the abstract MCOS class handle was introduced. The MCOS handle class has 12 methods. It also turns out that beginning with R2008a, the UDD handle package has 12 package functions which are the MCOS handle methods.</p><p>The 12 UDD classes in the handle package fall into two groups: The database and transaction classes work with the schema.package to provide a UDD stack mechanism; the listener and family of EventData classes work with schema.event to provide the UDD event mechanism:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; pkg = findpackage<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'handle'</span><span style="color: #080;">&#41;</span>
pkg =
        schema.<span style="">package</span>
&nbsp;
&gt;&gt; pkg.<span style="color: #0000FF;">get</span>
               Name<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'handle'</span>
    DefaultDatabase<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 handle.<span style="">Database</span><span style="color: #080;">&#93;</span>
            Classes<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>12x1 schema.<span style="color: #0000FF;">class</span><span style="color: #080;">&#93;</span>
          Functions<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>12x1 schema.<span style="">method</span><span style="color: #080;">&#93;</span>
        JavaPackage<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
         Documented<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
&nbsp;
&gt;&gt; names = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>numel<span style="color: #080;">&#40;</span>pkg.<span style="">Classes</span><span style="color: #080;">&#41;</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">for</span> index = <span style="color: #33f;">1</span><span style="color: #F0F;">:</span>numel<span style="color: #080;">&#40;</span>names<span style="color: #080;">&#41;</span>, names<span style="color: #080;">&#123;</span>index<span style="color: #080;">&#125;</span> = pkg.<span style="">Classes</span><span style="color: #080;">&#40;</span>index<span style="color: #080;">&#41;</span>.<span style="">Name</span>; <span style="color: #0000FF;">end</span>
&gt;&gt; names
names =
    <span style="color:#A020F0;">'Operation'</span>
    <span style="color:#A020F0;">'transaction'</span>
    <span style="color:#A020F0;">'Database'</span>
    <span style="color:#A020F0;">'EventData'</span>
    <span style="color:#A020F0;">'ClassEventData'</span>
    <span style="color:#A020F0;">'ChildEventData'</span>
    <span style="color:#A020F0;">'ParentEventData'</span>
    <span style="color:#A020F0;">'PropertyEventData'</span>
    <span style="color:#A020F0;">'PropertySetEventData'</span>
    <span style="color:#A020F0;">'listener'</span>
    <span style="color:#A020F0;">'JavaEventData'</span>
    <span style="color:#A020F0;">'subreference__'</span></pre></div></div><h4 id="hg">The hg package</h4><p>Arguably the most important UDD package in Matlab is the handle graphics package hg. Among the built-in UDD packages, hg is unique in several respects. As Matlab has evolved from R12 through R2011a, the number of default classes in the hg package has nearly doubled going from 17 classes to 30 (UDD has a mechanism for automatically defining additional classes as needed during run-time).</p><p>The hg package contains a mixture of Global and non Global classes. These classes return a numeric handle, unless they have been created using package scope. The uitools m-file package provides a great example of extending built-in UDD classes with user written m-file UDD classes.</p><p>The UDD class for a Handle-Graphics object can be obtained either by explicitly creating it with the hg package, or using the <i><b>handle</b></i> function on the numeric handle obtained from normal hg object creation. Using figure as an example, you can either use figh = hg.figure or fig = <i><b>figure</b></i> followed by figh = <i><b>handle</b></i>(fig):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; pkg = findpackage<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'hg'</span><span style="color: #080;">&#41;</span>
pkg =
        schema.<span style="">package</span>
&nbsp;
&gt;&gt; pkg.<span style="color: #0000FF;">get</span>
               Name<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'hg'</span>
    DefaultDatabase<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 handle.<span style="">Database</span><span style="color: #080;">&#93;</span>
            Classes<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>30x1 schema.<span style="color: #0000FF;">class</span><span style="color: #080;">&#93;</span>
          Functions<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>0x1 handle<span style="color: #080;">&#93;</span>
        JavaPackage<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'com.mathworks.hg'</span>
         Documented<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">for</span> index = <span style="color: #33f;">1</span><span style="color: #F0F;">:</span>numel<span style="color: #080;">&#40;</span>names<span style="color: #080;">&#41;</span>, names<span style="color: #080;">&#123;</span>index<span style="color: #080;">&#125;</span> = pkg.<span style="">Classes</span><span style="color: #080;">&#40;</span>index<span style="color: #080;">&#41;</span>.<span style="">Name</span>; <span style="color: #0000FF;">end</span>
&gt;&gt; names
names =
    <span style="color:#A020F0;">'GObject'</span>
    <span style="color:#A020F0;">'root'</span>
    <span style="color:#A020F0;">'LegendEntry'</span>
    <span style="color:#A020F0;">'Annotation'</span>
    <span style="color:#A020F0;">'figure'</span>
    <span style="color:#A020F0;">'uimenu'</span>
    <span style="color:#A020F0;">'uicontextmenu'</span>
    <span style="color:#A020F0;">'uicontrol'</span>
    <span style="color:#A020F0;">'uitable'</span>
    <span style="color:#A020F0;">'uicontainer'</span>
    <span style="color:#A020F0;">'hgjavacomponent'</span>
    <span style="color:#A020F0;">'uipanel'</span>
    <span style="color:#A020F0;">'uiflowcontainer'</span>
    <span style="color:#A020F0;">'uigridcontainer'</span>
    <span style="color:#A020F0;">'uitoolbar'</span>
    <span style="color:#A020F0;">'uipushtool'</span>
    <span style="color:#A020F0;">'uisplittool'</span>
    <span style="color:#A020F0;">'uitogglesplittool'</span>
    <span style="color:#A020F0;">'uitoggletool'</span>
    <span style="color:#A020F0;">'axes'</span>
    <span style="color:#A020F0;">'hggroup'</span>
    <span style="color:#A020F0;">'text'</span>
    <span style="color:#A020F0;">'line'</span>
    <span style="color:#A020F0;">'patch'</span>
    <span style="color:#A020F0;">'surface'</span>
    <span style="color:#A020F0;">'rectangle'</span>
    <span style="color:#A020F0;">'light'</span>
    <span style="color:#A020F0;">'image'</span>
    <span style="color:#A020F0;">'hgtransform'</span>
    <span style="color:#A020F0;">'uimcosadapter'</span></pre></div></div><p>So far we have just explored the very basic concepts of UDD. You may well be wondering what the big fuss is about, since the information presented so far does not have any immediately-apparent benefits.</p><p>The following set of articles will describe more advanced topics in UDD usage and customizations, using the building blocks presented today. Hopefully you will quickly understand how using UDD can help achieve some very interesting stuff with Matlab.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/creating-a-simple-udd-class/' rel='bookmark' title='Creating a simple UDD class'>Creating a simple UDD class</a> <small>This article explains how to create and test custom UDD packages, classes and objects...</small></li><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/hierarchical-systems-with-udd/' rel='bookmark' title='Hierarchical Systems with UDD'>Hierarchical Systems with UDD</a> <small>UDD objects can be grouped in structured hierarchies - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/new-information-on-hg2/' rel='bookmark' title='New information on HG2'>New information on HG2</a> <small>More information on Matlab's new HG2 object-oriented handle-graphics system...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/introduction-to-udd/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>Multi-column (grid) legend</title><link>http://undocumentedmatlab.com/blog/multi-column-grid-legend/</link> <comments>http://undocumentedmatlab.com/blog/multi-column-grid-legend/#comments</comments> <pubDate>Mon, 07 Feb 2011 18:00:04 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Adrian Cherry]]></category> <category><![CDATA[Listener]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[scribe]]></category> <category><![CDATA[Undocumented property]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2071</guid> <description><![CDATA[This article explains how to use undocumented axes listeners for implementing multi-column plot legends<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/legend-semi-documented-feature/' rel='bookmark' title='Legend &#8216;-DynamicLegend&#8217; semi-documented feature'>Legend &#8216;-DynamicLegend&#8217; semi-documented feature</a> <small>The built-in Matlab legend function has a very useful semi-documented feature for automatic dynamic update, which is explained here....</small></li><li><a
href='http://undocumentedmatlab.com/blog/multi-line-tooltips/' rel='bookmark' title='Multi-line tooltips'>Multi-line tooltips</a> <small>Multi-line tooltips are very easy to set up, once you know your way around a few undocumented hiccups....</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/introduction-to-udd/' rel='bookmark' title='Introduction to UDD'>Introduction to UDD</a> <small>UDD classes underlie many of Matlab's handle-graphics objects and functionality. This article introduces these classes....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>I would like to welcome guest blogger Adrian Cherry. Adrian will describe a very handy utility that shows how basic built-in Matlab functions can be improved and customized by just a bit of fiddling under Matlab&#8217;s hood.</i></p><h3 id="Legend">Legend Plotting</h3><p>Whilst I enjoy using the many time saving features of Matlab, one area where I feel it suffers is the technical plotting and annotation. This tale relates the development of a legend plotting routine, <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/29248-gridlegend-a-multi-column-format-for-legends">gridLegend</a>, in an effort to improve the presentation.</p><p>In my day job we have a requirement to condense a large quantity of data onto summary charts. However, there is only so much data consolidation possible before you start losing the information required. We often need to plot 40 or 50 lines of test data to visualize trends or outliers, using the legend to identify the number of test hours against each test specimen.</p><p>Using the standard Matlab <i><b>legend</b></i> function resulted in a long legend over twice the size of the associated plot:</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="Standard Matlab legend" src="http://UndocumentedMatlab.com/images/Legend1.png" title="Standard Matlab legend" width="450" height="337" /><p
class="wp-caption-text">Standard Matlab legend</p></div></center></p><p>I wanted some way of generating a more compact legend format.</p><p>Fortunately earlier in the year, an entry on Matlab Central allowing a multi-column legend to be generated was posted, <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/27389-columnlegend">columnlegend</a>. Although lacking some features, columnlegend gave me a good start on developing what I wanted for a multi column legend, culminating in <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/29248-gridlegend-a-multi-column-format-for-legends">gridLegend</a>:</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="Multi-column legend" src="http://UndocumentedMatlab.com/images/Legend2.png" title="Multi-column legend" width="450" height="398" /><p
class="wp-caption-text">Multi-column legend</p></div></center></p><h3 id="Undocumented">Delving into the Undocumented Matlab</h3><p>So where is the link with undocumented Matlab?</p><p>As mentioned in the original columnlegend entry, it was relatively simple to redraw the legend as required on the screen. However, as soon as the figure was printed or exported to an image file, internal Matlab workings would redraw the figure, including the legend, thereby undoing my careful legend crafting, squeezing it back into one column (Yuck!):</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="Matlab-reverted multi-column legend" src="http://UndocumentedMatlab.com/images/Legend3.png" title="Matlab-reverted multi-column legend" width="450" height="398" /><p
class="wp-caption-text">Matlab-reverted multi-column legend</p></div></center></p><p>As we wanted to automatically output images files, I had to delve into the hidden areas of Matlab to try to solve this problem.</p><p>My initial thought was to find out where the figure got redrawn for printing or export and override the standard legend call with a call to my new function. I couldn&#8217;t find the obvious culprit, stepping as far as I could through the print function there didn&#8217;t appear to be any call to the legend function.</p><p>In my search for information on how the legend worked I found the undocumented Matlab article about generating <a
target="_blank" href="http://UndocumentedMatlab.com/blog/legend-semi-documented-feature/">dynamic legends</a>. This dynamic-legend post covered details about attaching a listener to a child of the legend axes, in Matlab the legend function creates its own set of axes on the figure to display the legend.</p><p>Armed with the information that legend axes objects could have listeners attached, I considered that these might be the source of redrawing the legend for printing. So with the legend I had generated I took a look at what listeners were attached, using the undocumented hidden axes property <b>ScribeLegendListeners</b>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; legendListener = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'ScribeLegendListeners'</span><span style="color: #080;">&#41;</span>
legendListener = 
        fontname<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#93;</span>
        fontsize<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#93;</span>
      fontweight<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#93;</span>
       fontangle<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#93;</span>
       linewidth<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#93;</span>
         deleted<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#93;</span>
    proxydeleted<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#93;</span></pre></div></div><p>The font size and line positioning were all being redrawn for printing so this was potentially the source of my problem. However I&#8217;d not looked at a <i><b>handle.listener</b></i> before, so a little further digging was required:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">K&gt;&gt; <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>legendListener.<span style="">fontname</span><span style="color: #080;">&#41;</span>
      SourceObject<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 schema.<span style="">prop</span><span style="color: #080;">&#93;</span>
         Container<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 <span style="color: #0000FF;">axes</span><span style="color: #080;">&#93;</span>
         EventType<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'PropertyPostSet'</span>
          Callback<span style="color: #F0F;">:</span> <span style="color: #080;">&#123;</span>2x1 <span style="color: #0000FF;">cell</span><span style="color: #080;">&#125;</span>
    CallbackTarget<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
           Enabled<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span></pre></div></div><p>The option <b>Enabled</b> immediately drew my attention, and so the following lines were added to my gridLegend function to switch off these listeners and apply it back to the legend:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">LL = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'ScribeLegendListeners'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>LL.<span style="">fontname</span>,<span style="color:#A020F0;">'enabled'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>LL.<span style="">fontsize</span>,<span style="color:#A020F0;">'enabled'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>LL.<span style="">fontweight</span>,<span style="color:#A020F0;">'enabled'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>LL.<span style="">fontangle</span>,<span style="color:#A020F0;">'enabled'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>LL.<span style="">linewidth</span>,<span style="color:#A020F0;">'enabled'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'ScribeLegendListeners'</span>,LL<span style="color: #080;">&#41;</span>;</pre></div></div><p>Finally allowing me to output the image files with a multi-column legend:</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="Printed multi-column legend" src="http://UndocumentedMatlab.com/images/Legend4.jpg" title="Printed multi-column legend" width="450" height="363" /><p
class="wp-caption-text">Printed multi-column legend</p></div></center></p><p>So my thanks to the contributors on Matlab Central who enabled me to get started on gridLegend and to Yair for collating the many nuggets of information on Undocumented Matlab which allowed me to complete the function and get it posted on Matlab Central.</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="Multi-column legend in action" src="http://UndocumentedMatlab.com/images/gridLegend.jpg" title="Multi-column legend in action" width="450" height="300" /><p
class="wp-caption-text">Multi-column legend in action</p></div></center></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/legend-semi-documented-feature/' rel='bookmark' title='Legend &#8216;-DynamicLegend&#8217; semi-documented feature'>Legend &#8216;-DynamicLegend&#8217; semi-documented feature</a> <small>The built-in Matlab legend function has a very useful semi-documented feature for automatic dynamic update, which is explained here....</small></li><li><a
href='http://undocumentedmatlab.com/blog/multi-line-tooltips/' rel='bookmark' title='Multi-line tooltips'>Multi-line tooltips</a> <small>Multi-line tooltips are very easy to set up, once you know your way around a few undocumented hiccups....</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/introduction-to-udd/' rel='bookmark' title='Introduction to UDD'>Introduction to UDD</a> <small>UDD classes underlie many of Matlab's handle-graphics objects and functionality. This article introduces these classes....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/multi-column-grid-legend/feed/</wfw:commentRss> <slash:comments>1</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>26</slash:comments> </item> <item><title>Tab panels &#8211; uitab and relatives</title><link>http://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives/</link> <comments>http://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives/#comments</comments> <pubDate>Wed, 23 Jun 2010 12:43:16 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Figure window]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Listener]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[uitools]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1635</guid> <description><![CDATA[This article describes several undocumented Matlab functions that support tab-panels<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitab-customizations/' rel='bookmark' title='Uitab customizations'>Uitab customizations</a> <small>This article shows several customizations that can be done to Matlab's undocumented tab-panels functionality...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitab-colors-icons-images/' rel='bookmark' title='Uitab colors, icons and images'>Uitab colors, icons and images</a> <small>Matlab's semi-documented tab panels can be customized using some undocumented hacks...</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/syntax-highlighted-labels-panels/' rel='bookmark' title='Syntax highlighted labels &amp; panels'>Syntax highlighted labels &#038; panels</a> <small>Syntax-highlighted labels and edit-boxes can easily be displayed in Matlab GUI - this article explains how. ...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>In the past year, readers of this blog have used its search box thousands of times. Can you guess what the top search terms are?</p><p>It turns out that 7 of the top 15 search terms relate to tables, trees and tab-panes.</p><p>These items are related in being standard GUI elements that unfortunately have very lacking support in Matlab. They all have corresponding functions in the %matlabroot%/toolbox/matlab/uitools folder, which was already <a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/uitools/">introduced</a> here as containing many unsupported GUI functions. Specifically, <i><b>uitable</b></i> for tables (this became supported in R2008a, but even the supported version has many important undocumented aspects); <i><b>uitree</b></i> and <i><b>uitreenode</b></i> for trees; and <i><b>uitab</b></i> and <i><b>uitabgroup</b></i> for tab-panes, which are today&#8217;s subject. Future articles will describe tables, trees and tab-panes in more detail.</p><h3 id="uitab">uitab &#038; uitabgroup</h3><p>Like most other uitools, the <i><b>uitab</b></i> and <i><b>uitabgroup</b></i> functions are <a
target="_blank" href="http://undocumentedmatlab.com/blog/legend-semi-documented-feature/#Semi-documented">semi-documented</a>, meaning that they have no support or doc-page, but do have readable help sections within their m-files. In our case, edit the uitab.m and uitabgroup.m files to see their help section.</p><p>Available since 2004 (R14 SP2, aka 7.0.4), Matlab&#8217;s <i><b>uitabgroup</b></i> uses the Matlab Java widget com.mathworks.hg.peer.UITabGroupPeer, which extends the standard javax.swing.JTabbedPane. Unlike <i><b>uitable</b></i> and <i><b>uitree</b></i>, which use actual Java objects to both store and present the data, <i><b>uitabgroup</b></i> only sets up the Java object to display the tabs, whereas the tab contents themselves are placed in entirely unrelated Matlab uicontainers. Matlab uses very clever double-booking to keep the Java and Matlab objects synchronized. The ability to “switch” tabs is actually a deception: in reality, a listener placed on the <strong>SelectedIndex</strong> property of the tab group causes the relevant Matlab container to display and all the rest to become hidden. Other listeners control containers&#8217; position and size based on the tab group&#8217;s. Adding and removing tabs uses similar methods to add/remove empty tabs to the JTabbedPane. Read <i><b>uitabgroup</b></i>&#8216;s schema.m for details.</p><p>A drawback of this complex mechanism is the absence of a single customizable Java object. The benefit is that it allows us to place any Matlab content within the tabs, including plot axes which cannot be added to Java containers. Had <i><b>uitabgroup</b></i> been a Java container, we could not add axes plots or images to its tabs. In my humble opinion, Matlab&#8217;s tab implementation is an ingenious piece of engineering.</p><p>Here&#8217;s a simple tab-group adapted from <i><b>uitabgroup</b></i>&#8216;s help section:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hTabGroup = uitabgroup; <span style="color: #0000FF;">drawnow</span>;
tab1 = uitab<span style="color: #080;">&#40;</span>hTabGroup, <span style="color:#A020F0;">'title'</span>,<span style="color:#A020F0;">'Panel 1'</span><span style="color: #080;">&#41;</span>;
a = <span style="color: #0000FF;">axes</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>, tab1<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">surf</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">peaks</span><span style="color: #080;">&#41;</span>;
tab2 = uitab<span style="color: #080;">&#40;</span>hTabGroup, <span style="color:#A020F0;">'title'</span>,<span style="color:#A020F0;">'Panel 2'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span>tab2, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Close'</span>, <span style="color:#A020F0;">'Callback'</span>,<span style="color:#A020F0;">'close(gcbf)'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>(recent Matlab releases throw a warning when using this code: either add the &#8216;v0&#8242; input arg to <i><b>uitabgroup</b></i> and <b><i>uitab</i></b> calls, or suppress the MATLAB:uitabgroup:MigratingFunction warning)</p><p>Here, the returned <i><b>uitabgroup</b></i> object hTabGroup is actually a Matlab container (deriving from <a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-layout-managers-uicontainer-and-relatives/#uiflowcontainer"><i><b>uiflowcontainer</b></i></a>) that always displays two elements: the Java tab-group, and the active Matlab <i><b>uicontainer</b></i> (the active tab&#8217;s contents). Understanding this, hTabGroup&#8217;s <strong>FlowDirection</strong> property becomes clear. However, it is better to use hTabGroup&#8217;s <strong>TabLocation</strong> property, which accepts &#8216;top&#8217;, &#8216;bottom&#8217;, &#8216;left&#8217; and &#8216;right&#8217;:</p><p><center><div><div
class="wp-caption aligncenter" style="width: 220px"><img
alt="TabLocation = 'top'" src="http://UndocumentedMatlab.com/images/uitabgroup1.png" title="TabLocation = 'top'" width="210" height="220" /><p
class="wp-caption-text"><b>TabLocation</b> = 'top'</p></div><div
class="wp-caption aligncenter" style="width: 220px"><img
alt="TabLocation = 'left'" src="http://UndocumentedMatlab.com/images/uitabgroup2.png" title="TabLocation = 'left'" width="210" height="220" /><p
class="wp-caption-text"><b>TabLocation</b> = 'left'</p></div></div><p></center></p><p>Another hTabGroup property of interest is <strong>Margin</strong>, which sets the margin in pixels before each of the displayed elements – not just between them as might be expected: Increasing <strong>Margin</strong> (default=2 pixels) increases the gap between the tab group and the active tab&#8217;s contents, but also the gap between the tab group and figure edge:</p><p><center><div
class="wp-caption aligncenter" style="width: 470px"><img
alt="TabLocation = 'bottom', Margin = 20" src="http://UndocumentedMatlab.com/images/uitabgroup4.png" title="TabLocation = 'bottom', Margin = 20" width="210" height="220" /> &nbsp; <img
alt="TabLocation = 'left', Margin = 20" src="http://UndocumentedMatlab.com/images/uitabgroup3.png" title="TabLocation = 'left', Margin = 20" width="210" height="220" /><p
class="wp-caption-text"><b>Margin</b> = 20</p></div></center></p><p>Tabs can be selected programmatically, by setting hTabGroup&#8217;s <strong>SelectedIndex</strong> property. Reading this property is useful when setting tab-selection callbacks using the <strong>SelectionChangeFcn</strong> property:</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>hTabGroup,<span style="color:#A020F0;">'SelectionChangeFcn'</span>,@myCallbackFcn<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hTabGroup,<span style="color:#A020F0;">'SelectedIndex'</span>,<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;   <span style="color: #228B22;">% activate second tab</span></pre></div></div><p><b><u>Edit Oct 27 2010</u></b>: <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/uitools_csh/error_pages/bslcn87.html">R2010b made a few changes</a> to the hTabGroup properties: <b>SelectedTab</b> has been added (holds the handle of the selected tab, rather than its index as <b>SelectedIndex</b>; note that <b>SelectedTab</b> was added as a hidden property for some unknown reason, probably a programming mistake); the tab <b>BackgroundColor</b> cannot be modified (I&#8217;ll show how to bypass this limitation in a near-future article); <b>SelectionChangeFcn</b> callback and the <b>SelectionChanged</b> event have changed their names to <b>SelectionChangeCallback</b> and <b>SelectionChange</b> respectively. Note that this is one of the very rare occasions in which MathWorks have taken the trouble to notify users about changes to one of their undocumented/unsupported functions. They should be commended for this since it helps us Matlab users make better use of the product.</p><p>Additional control over the tab group&#8217;s behavior can be achieved by customizing the underlying Java object. This object is not directly exposed by <i><b>uitabgroup</b></i>, but can be found using the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/">FindJObj utility</a> or via the hidden <b>ApplicationData</b>. Remember that Java objects use 0-based indexing so tab #1 is actually the second tab. Also remember that <a
target="_blank" href="http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/">HTML is accepted just as in any other Swing-based label</a>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Get the underlying Java reference using FindJObj</span>
jTabGroup = findjobj<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'class'</span>,<span style="color:#A020F0;">'tabgroup'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% A direct alternative for getting jTabGroup</span>
jTabGroup = <span style="color: #0000FF;">getappdata</span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>hTabGroup<span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'JTabbedPane'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Now use the Java reference to set the title, tooltip etc.</span>
jTabGroup.<span style="">setTitleAt</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color:#A020F0;">'Tab #2'</span><span style="color: #080;">&#41;</span>;
jTabGroup.<span style="">setTitleAt</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color:#A020F0;">'&lt;html&gt;&lt;b&gt;&lt;i&gt;&lt;font size=+2&gt;Tab #2'</span><span style="color: #080;">&#41;</span>;
jTabGroup.<span style="">setToolTipTextAt</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color:#A020F0;">'Tab #2'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Disabling tabs can only be done using the Java handle:</span>
jTabGroup.<span style="">setEnabledAt</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% disable only tab #1 (=2nd tab)</span>
jTabGroup.<span style="">setEnabled</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% disable all tabs</span>
&lt;/font&gt;&lt;/i&gt;&lt;/b&gt;&lt;/html&gt;</pre></div></div><p>A future post will describe tab customization, including fonts, colors, icons and even addition of close buttons as in modern web browsers.</p><h3 id="tabdlg">tabdlg</h3><p><i><b>tabdlg</b></i> is a related semi-documented and unsupported uitool that, like <i><b>uitabgroup</b></i>, creates a tabbed user interface. However, unlike <i><b>uitabgroup</b></i>, <i><b>tabdlg</b></i> uses plain-vanilla Matlab, without reliance on Java (well, actually all Matlab GUI controls ultimately rely on Java, but <i><b>tabdlg</b></i> does not use any Java beyond that). The end result looks less professional than <i><b>uitabgroup</b></i>, but it works even when Java does not.</p><p><i><b>tabdlg</b></i> has an extensive help section, so it will not be detailed here. In brief, the input parameters specify the tab labels, dimensions, offsets, callbacks, font, default tab, sheet dimensions and parent figure. Here is a sample usage, taken from <i><b>tabdlg</b></i>&#8216;s help section. This code is executed whenever <i><b>tabdlg</b></i> is invoked without any input arguments:</p><p><center><div
class="wp-caption aligncenter" style="width: 220px"><img
alt="tabdlg left tab" src="http://UndocumentedMatlab.com/images/tabdlg1.png" title="tabdlg left tab" width="210" height="231" /><p
class="wp-caption-text"><b><i>tabdlg</i></b> left tab</p></div><div
class="wp-caption aligncenter" style="width: 220px"><img
alt="tabdlg right tab" src="http://UndocumentedMatlab.com/images/tabdlg2.png" title="tabdlg right tab" width="210" height="231" /><p
class="wp-caption-text"><b><i>tabdlg</i></b> right tab</p></div></center></p><h3 id="FEX">File Exchange alternatives</h3><p>There are many <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/?term=tab+gui">implementations of tab panels in the Matlab File Exchange</a>. Matlab&#8217;s official Desktop Blog had an <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/desktop/2010/02/15/putting-the-tab-into-a-gui/">article</a> about one specific example, which was that week&#8217;s Peek of the Week, and relied on adjacent buttons that are easy to implement, but in my personal opinion are a far cry from our expectations of a tab panel.</p><p>Better FEX utilities are: <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/25938-multiple-tab-gui">Multiple Tab GUI</a>, <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/22488-highlight-tab-objects-easily">Highlight Tab Objects easily</a>, and best of all: <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/11546-uitabpanel">uitabpanel</a> or <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/6996-tabpanel-constructor">TabPanel Constructor</a>.</p><p>Another very recent <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/27758-gui-layout-toolbox">submission</a> was <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/pick/2010/06/18/gui-layout-part-7/">this week&#8217;s POTW</a>. This utility gives a professional (although somewhat non-standard) look, and is very easy to program &#8211; an excellent utility indeed.</p><p>All of the numerous tab-panel FEX utilities, as well as the fact that tab-panels are one of the most searched-for terms in this website, indicate the Matlab community&#8217;s desire to have supported native-looking tab-panel GUI in Matlab. Perhaps after 6 years it is time to bring <i><b>uitab</b></i> and <i><b>uitabgroup</b></i> into the light?</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitab-customizations/' rel='bookmark' title='Uitab customizations'>Uitab customizations</a> <small>This article shows several customizations that can be done to Matlab's undocumented tab-panels functionality...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitab-colors-icons-images/' rel='bookmark' title='Uitab colors, icons and images'>Uitab colors, icons and images</a> <small>Matlab's semi-documented tab panels can be customized using some undocumented hacks...</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/syntax-highlighted-labels-panels/' rel='bookmark' title='Syntax highlighted labels &amp; panels'>Syntax highlighted labels &#038; panels</a> <small>Syntax-highlighted labels and edit-boxes can easily be displayed in Matlab GUI - this article explains how. ...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives/feed/</wfw:commentRss> <slash:comments>50</slash:comments> </item> <item><title>Inactive Control Tooltips &amp; Event Chaining</title><link>http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/</link> <comments>http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/#comments</comments> <pubDate>Wed, 24 Feb 2010 22:34:46 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[Listener]]></category> <category><![CDATA[Matt Whitaker]]></category> <category><![CDATA[uicontrol]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1153</guid> <description><![CDATA[Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks.<pre> </pre>Related posts:<ol><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/gui-integrated-browser-control/' rel='bookmark' title='GUI integrated browser control'>GUI integrated browser control</a> <small>A fully-capable browser component is included in Matlab and can easily be incorporated in regular Matlab GUI applications. This article shows how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/' rel='bookmark' title='Matlab-Java interface using a static control'>Matlab-Java interface using a static control</a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Once again, I welcome guest blogger Matt Whitaker, who continues his series of articles.</i></p><p>In my last post I explored some <a
target="_blank" href="http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/">tips on tooltips</a>. One of these tips involved displaying tooltips on disabled uicontrols. I explained that displaying tooltips on inactive controls is problematic since Matlab appears to intercept mouse events to these inactive controls, so even setting the tooltip on the underlying Java object will not work: The java object appears not to receive the mouse-hover event and therefore does not &#8220;know&#8221; that it&#8217;s time to display the tooltip.</p><p>When Yair and I deliberated this issue, he pointed me to his <a
target="_blank" href="http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/#comment-1173">comment on a previous article</a> showing an undocumented Java technique (Java also has some&#8230;) for forcing a tooltip to appear using the <b>ActionMap</b> of the uicontrol&#8217;s underlying Java object to get at a <i>postTip</i> action. We discussed using a <b>WindowButtonMotionFcn</b> callback to see if the mouse was above the inactive control, then triggering the forced tooltip display. Yair then went on to remind me and I quote: <i>&#8220;you&#8217;ll need to chain existing <b>WindowButtonMotionFcn</b> callbacks and take into account ModeManagers that override them.&#8221;</i></p><p>Frankly, having written code previously that handles callback chaining, I would rather poke myself in the eye with a fork!</p><p>The Image Processing Toolbox has the nice pair of <b><i>iptaddcallback</i></b> and <b><i>iptremovecallback</i></b> functions that largely handle these issues. But  for general Matlab, there seemed to be no alternative until I remembered that events trigger callbacks. I decided to use a listener for the <i>WindowButtonMotion</i> event to detect the mouse motion. <a
target="_blank" href="http://undocumentedmatlab.com/blog/continuous-slider-callback/#Event_Listener">Event listeners were briefly explained two weeks ago</a> and deserve a dedicated future article. The advantage of using an event listener is that we don&#8217;t disturb any existing <b>WindowButtonMotionFcn</b> callback. We still need to be somewhat careful that our listeners don&#8217;t do conflicting things, but it&#8217;s a lot easier than trying to manage everything through the single <b>WindowButtonMotionFcn</b>.</p><p>A demonstration of this appears below with some comments following (note that this code uses the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><b><i>FindJObj</i></b> utility</a>):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> inactiveBtnToolTip
  <span style="color: #228B22;">%Illustrates how to make a tooltip appear on an inactive control</span>
  h = <span style="color: #0000FF;">figure</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'WindowButtonMotionFcn'</span>,@windowMotion,<span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">400</span>,<span style="color: #33f;">400</span>,<span style="color: #33f;">200</span>,<span style="color: #33f;">200</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
  col = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>h,<span style="color:#A020F0;">'color'</span><span style="color: #080;">&#41;</span>;
  lbl = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'text'</span>, <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">160</span>,<span style="color: #33f;">120</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color: #F0F;">...</span>
                  <span style="color:#A020F0;">'Background'</span>,col, <span style="color:#A020F0;">'HorizontalAlignment'</span>,<span style="color:#A020F0;">'left'</span><span style="color: #080;">&#41;</span>;
  btn = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Parent'</span>,h, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Button'</span>, <span style="color: #F0F;">...</span>
                  <span style="color:#A020F0;">'Enable'</span>,<span style="color:#A020F0;">'inactive'</span>, <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">40</span>,<span style="color: #33f;">60</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'check'</span>, <span style="color:#A020F0;">'Parent'</span>,h, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Enable button tooltip'</span>, <span style="color: #F0F;">...</span>
            <span style="color:#A020F0;">'Callback'</span>,@chkTooltipEnable, <span style="color:#A020F0;">'Value'</span>,<span style="color: #33f;">1</span>, <span style="color: #F0F;">...</span>
            <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">80</span>,<span style="color: #33f;">180</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'Background'</span>,col<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">drawnow</span>;
&nbsp;
  <span style="color: #228B22;">%create the tooltip and postTip action</span>
  jBtn = findjobj<span style="color: #080;">&#40;</span>btn<span style="color: #080;">&#41;</span>;
  import java.<span style="">awt</span>.<span style="">event</span>.<span style="">ActionEvent</span>;
  javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setToolTipText'</span>,jBtn,<span style="color:#A020F0;">'This button is inactive'</span><span style="color: #080;">&#41;</span>;
  actionMap = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getActionMap'</span>,jBtn<span style="color: #080;">&#41;</span>;
  action = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'get'</span>,actionMap,<span style="color:#A020F0;">'postTip'</span><span style="color: #080;">&#41;</span>;
  actionEvent = ActionEvent<span style="color: #080;">&#40;</span>jBtn, ActionEvent.<span style="">ACTION_PERFORMED</span>, <span style="color:#A020F0;">'postTip'</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">%get the extents plus 2 pixels of the control to compare to the mouse position</span>
  btnPos = getpixelposition<span style="color: #080;">&#40;</span>btn<span style="color: #080;">&#41;</span>+<span style="color: #080;">&#91;</span>-<span style="color: #33f;">2</span>,-<span style="color: #33f;">2</span>,<span style="color: #33f;">4</span>,<span style="color: #33f;">4</span><span style="color: #080;">&#93;</span>; <span style="color: #228B22;">%give a little band around the control</span>
  left = btnPos<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
  right = <span style="color: #0000FF;">sum</span><span style="color: #080;">&#40;</span>btnPos<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">3</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
  btm = btnPos<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;
  top =  <span style="color: #0000FF;">sum</span><span style="color: #080;">&#40;</span>btnPos<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #33f;">2</span>,<span style="color: #33f;">4</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% add a listener on mouse movement events</span>
  tm = javax.<span style="">swing</span>.<span style="">ToolTipManager</span>.<span style="">sharedInstance</span>; <span style="color: #228B22;">%tooltip manager</span>
  pointListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>h,<span style="color:#A020F0;">'WindowButtonMotionEvent'</span>,@figMouseMove<span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">%inControl is a flag to prevent multiple triggers of the postTip action</span>
  <span style="color: #228B22;">%while mouse remains in the button</span>
  inControl = <span style="color: #0000FF;">false</span>;
&nbsp;
  <span style="color: #0000FF;">function</span> figMouseMove<span style="color: #080;">&#40;</span>src,evtData<span style="color: #080;">&#41;</span> <span style="color: #228B22;">%#ok</span>
    <span style="color: #228B22;">%get the current point</span>
    cPoint = evtData.<span style="">CurrentPoint</span>;
&nbsp;
    <span style="color: #0000FF;">if</span> cPoint<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> &gt;= left <span style="color: #F0F;">&amp;&amp;</span> cPoint<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> &lt;= right <span style="color: #F0F;">&amp;&amp;...</span>
       <span style="">cPoint</span><span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> &gt;= btm  <span style="color: #F0F;">&amp;&amp;</span> cPoint<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> &lt;= top
&nbsp;
      <span style="color: #0000FF;">if</span> ~inControl <span style="color: #228B22;">%we just entered</span>
        inControl = <span style="color: #0000FF;">true</span>;
        action.<span style="">actionPerformed</span><span style="color: #080;">&#40;</span>actionEvent<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%show the tooltip</span>
      <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
    <span style="color: #0000FF;">else</span>
      <span style="color: #0000FF;">if</span> inControl <span style="color: #228B22;">%we just existed</span>
        inControl = <span style="color: #0000FF;">false</span>;
        <span style="color: #228B22;">%toggle to make it disappear when leaving button</span>
        javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,tm,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
        javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,tm,<span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%gpMouseMove</span>
&nbsp;
  <span style="color: #0000FF;">function</span> windowMotion<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
    <span style="color: #228B22;">%illustrate that we can still do a regular window button motion callback</span>
    <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>lbl,<span style="color:#A020F0;">'String'</span>,<span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Mouse position: %d, %d'</span>,<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>h,<span style="color:#A020F0;">'CurrentPoint'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">drawnow</span>;
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%windowMotion</span>
&nbsp;
  <span style="color: #0000FF;">function</span> chkTooltipEnable<span style="color: #080;">&#40;</span>src,<span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
    <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>src,<span style="color:#A020F0;">'Value'</span><span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>pointListener,<span style="color:#A020F0;">'Enable'</span>,<span style="color:#A020F0;">'on'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">else</span>
      <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>pointListener,<span style="color:#A020F0;">'Enable'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%chkTooltipEnable</span>
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%inactiveBtnToolTip</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 218px"><img
alt="Tooltip on an inactive button" src="http://undocumentedmatlab.com/images/tooltip4.png" title="Tooltip on an inactive button" width="208" height="255" /><p
class="wp-caption-text">Tooltip on an inactive button</p></div></center></p><p>Comments on the code:</p><ol><li>The code illustrates that we can successfully add an additional listener to listen for mouse motion events while still carrying out the original <b>WindowButtonMotionFcn</b> callback. This makes chaining callbacks much easier.</li><li>The <b><i>handle.listener</i></b> object has an <b>Enable</b> property that we can use to temporarily turn the listener on and off. This can be seen in the <i>chkTooltipEnable()</i> callback for the check box in the code above. If we wanted to permanently remove the listener we would simply use <i><b>delete</b>(pointListener)</i>. Note that <b><i>addlistener</i></b> adds a hidden property to the object being listened to, so that the listener is tied to the object&#8217;s lifecycle. If you create a listener directly using <b><i>handle.listener</i></b> you are responsible for it&#8217;s disposition. Unfortunately, <b><i>addlistener</i></b> fails for HG handles on pre-R2009 Matlab releases, so we use <b><i>handle.listener</i></b> directly.</li><li>The code illustrates a good practice when tracking rapidly firing events like mouse movement of handling reentry into the callback while it is still processing a previous callback. Here we use a flag called inControl to prevent the <i>postTip</i> action being continuously fired while the mouse remains in the control.</li><li>I was unable to determine if there is any corresponding action for the <i>postTip</i> to dismiss tips so I resorted to using the ToolTipManager to toggle its own <b>Enable</b> property to cleanly hide the tooltip as the mouse leaves the control.</li></ol><p>Each Matlab callback has an associated event with it. Some of the ones that might be immediately useful at the figure-level are <b>WindowButtonDown</b>, <b>WindowButtonUp</b>, <b>WindowKeyPress</b>, and <b>WindowKeyRelease</b>. They can all be accessed through <b><i>handle.listener</i></b> or <b><i>addlistener</i></b> as in the code above.</p><p>Unfortunately, events do not always have names that directly correspond to the callback names. In order to see the list of available events for a particular Matlab object, use the following code, which relies on another undocumented function &#8211; <b><i>classhandle</i></b>. Here we list the events for <b><i>gcf</i></b>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>classhandle<span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'Events'</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'Name'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
    <span style="color:#A020F0;">'SerializeEvent'</span>
    <span style="color:#A020F0;">'FigureUpdateEvent'</span>
    <span style="color:#A020F0;">'ResizeEvent'</span>
    <span style="color:#A020F0;">'WindowKeyReleaseEvent'</span>
    <span style="color:#A020F0;">'WindowKeyPressEvent'</span>
    <span style="color:#A020F0;">'WindowButtonUpEvent'</span>
    <span style="color:#A020F0;">'WindowButtonDownEvent'</span>
    <span style="color:#A020F0;">'WindowButtonMotionEvent'</span>
    <span style="color:#A020F0;">'WindowPostChangeEvent'</span>
    <span style="color:#A020F0;">'WindowPreChangeEvent'</span></pre></div></div><p>Note that I have made extensive use of the <b><i>javaMethodEDT</b></i> function to execute Java methods that affect swing components on Swing&#8217;s Event Dispatch Thread. I plan to write about this and related functions in my next article.</p><p><pre> </pre>Related posts:<ol><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/gui-integrated-browser-control/' rel='bookmark' title='GUI integrated browser control'>GUI integrated browser control</a> <small>A fully-capable browser component is included in Matlab and can easily be incorporated in regular Matlab GUI applications. This article shows how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/' rel='bookmark' title='Matlab-Java interface using a static control'>Matlab-Java interface using a static control</a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Continuous slider callback</title><link>http://undocumentedmatlab.com/blog/continuous-slider-callback/</link> <comments>http://undocumentedmatlab.com/blog/continuous-slider-callback/#comments</comments> <pubDate>Mon, 08 Feb 2010 09:13:39 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[Listener]]></category> <category><![CDATA[schema.prop]]></category> <category><![CDATA[uicontrol]]></category> <category><![CDATA[Undocumented feature]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1052</guid> <description><![CDATA[Matlab slider uicontrols do not enable a continuous-motion callback by default. This article explains how this can be achieved using undocumented features.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy/' rel='bookmark' title='Controlling callback re-entrancy'>Controlling callback re-entrancy</a> <small>Callback reentrancy is a major problem for frequently-fired events. Luckily, it can easily be solved....</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-listbox-mouse-actions/' rel='bookmark' title='Setting listbox mouse actions'>Setting listbox mouse actions</a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</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>Every few months, a CSSM forum reader asks how to set up a continuously-invoked slider callback: Matlab&#8217;s <a
target="_blank" rel="nofollow" href="http://blinkdagger.com/matlab/matlab-gui-tutorial-slider/">slider uicontrol</a> invokes the user callback only when the mouse button is released, and not continuously while the slider&#8217;s thumb is dragged. This functionality was again referred-to <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/272224">yesterday</a>, and I decided it merits a dedicated post.</p><p>There are three distinct simple ways to achieve continuous callbacks:</p><h3 id="Java_Callbacks">Using Java callbacks</h3><p>As explained in an earlier <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">article</a>, Matlab uicontrols are basically Java Swing objects that possess a large number of useful callbacks. Matlab sliders&#8217; underlying Java objects, which are really not <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/slider.html">JSliders</a> but <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html#operation">JScrollBars</a>, have an <b>AdjustmentValueChangedCallback</b> property that is useful for our purposes and is accessible using the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><b><i>FindJObj utility</i></b></a>. Simply download <b><i>FindJObj</i></b> from the <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14317">File Exchange</a>, and then:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hSlider = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'slider'</span>, <span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span>;
jScrollBar = findjobj<span style="color: #080;">&#40;</span>hSlider<span style="color: #080;">&#41;</span>;
jScrollBar.<span style="">AdjustmentValueChangedCallback</span> = @myCbFcn;
<span style="color: #228B22;">% or: set(jScrollBar,'AdjustmentValueChangedCallback',@myCbFcn)</span></pre></div></div><p>Where <i>myCbFcn</i> is the Matlab callback function that will be invoked continuously when the arrow buttons are depressed or the slider&#8217;s thumb is dragged.</p><h3 id="Event_Listener">Using an event listener</h3><p>An alternative to the Java route is to use Matlab&#8217;s undocumented <b><i>handle.listener</i></b> function to listen to the slider&#8217;s <b>Action</b> event, as follows:</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>hSlider,<span style="color:#A020F0;">'ActionEvent'</span>,@myCbFcn<span style="color: #080;">&#41;</span>;</pre></div></div><p>This alternative is used by Matlab&#8217;s own <b><i>imscrollpanel</i></b> function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">if</span> isJavaFigure
   <span style="color: #228B22;">% Must use these ActionEvents to get continuous events fired as slider</span>
   <span style="color: #228B22;">% thumb is dragged. Regular callbacks on sliders give only one event</span>
   <span style="color: #228B22;">% when the thumb is released.</span>
   hSliderHorListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hSliderHor,<span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'ActionEvent'</span>,@scrollHorizontal<span style="color: #080;">&#41;</span>;
   hSliderVerListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hSliderVer,<span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'ActionEvent'</span>,@scrollVertical<span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">setappdata</span><span style="color: #080;">&#40;</span>hScrollpanel,<span style="color:#A020F0;">'sliderListeners'</span>,<span style="color: #F0F;">...</span>
      <span style="color: #080;">&#91;</span>hSliderHorListener hSliderVerListener<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">else</span>
   <span style="color: #228B22;">% Unfortunately, the event route is only available with Java Figures,</span>
   <span style="color: #228B22;">% so platforms without Java Figure support get discrete events only</span>
   <span style="color: #228B22;">% when the mouse is released from dragging the slider thumb.</span>
   <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hSliderHor,<span style="color:#A020F0;">'callback'</span>,@scrollHorizontal<span style="color: #080;">&#41;</span>
   <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hSliderVer,<span style="color:#A020F0;">'callback'</span>,@scrollVertical<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">end</span></pre></div></div><h3 id="Property_Listener">Using a property listener</h3><p>The <b><i>handle.listener</i></b> function can also be used to listen to property value changes. In our case, set a post-set listener, that gets triggered immediately following <b>Value</b> property updates, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hhSlider = handle<span style="color: #080;">&#40;</span>hSlider<span style="color: #080;">&#41;</span>;
hProp = <span style="color: #0000FF;">findprop</span><span style="color: #080;">&#40;</span>hhSlider,<span style="color:#A020F0;">'Value'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% a schema.prop object</span>
hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hhSlider,hProp,<span style="color:#A020F0;">'PropertyPostSet'</span>,@myCbFcn<span style="color: #080;">&#41;</span>;</pre></div></div><p>In addition to &#8216;PropertyPostSet&#8217;, we could also listen on &#8216;PropertyPreSet&#8217;, which is triggered immediately <u>before</u> the property is modified. There are also corresponding &#8216;*Get&#8217; options. In relatively old Matlab releases (I believe R2007b and earlier, but I&#8217;m not certain), the option names were simply &#8216;PostSet&#8217;, &#8216;PreSet&#8217; etc., without the &#8216;Property&#8217; prefix.</p><p>Do you know of any other way to achieve continuous callbacks? If so, I would be delighted to hear in the comments section below.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy/' rel='bookmark' title='Controlling callback re-entrancy'>Controlling callback re-entrancy</a> <small>Callback reentrancy is a major problem for frequently-fired events. Luckily, it can easily be solved....</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-listbox-mouse-actions/' rel='bookmark' title='Setting listbox mouse actions'>Setting listbox mouse actions</a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</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/continuous-slider-callback/feed/</wfw:commentRss> <slash:comments>28</slash:comments> </item> <item><title>setPrompt &#8211; Setting the Matlab Desktop prompt</title><link>http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/</link> <comments>http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/#comments</comments> <pubDate>Mon, 25 Jan 2010 15:30:54 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Desktop]]></category> <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[Callbacks]]></category> <category><![CDATA[Listener]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=965</guid> <description><![CDATA[The Matlab Desktop's Command-Window prompt can easily be modified using some undocumented features<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/setting-the-matlab-desktop-layout-programmatically/' rel='bookmark' title='Setting the Matlab desktop layout programmatically'>Setting the Matlab desktop layout programmatically</a> <small>The Matlab desktop enables saving and switching layouts using the main menu. This post shows how to do so programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-desktop-tab-completions/' rel='bookmark' title='Setting desktop tab completions'>Setting desktop tab completions</a> <small>The Matlab desktop's Command-Window tab-completion can be customized for user-defined functions...</small></li><li><a
href='http://undocumentedmatlab.com/blog/editormacro-v2-setting-command-window-key-bindings/' rel='bookmark' title='EditorMacro v2 &#8211; setting Command Window key-bindings'>EditorMacro v2 &#8211; setting Command Window key-bindings</a> <small>The EditorMacro utility was extended to support built-in Matlab Editor and Command-Window actions and key-bindings. This post describes the changes and the implementation details....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-status-bar-text/' rel='bookmark' title='Setting status-bar text'>Setting status-bar text</a> <small>The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A few days ago, a reader emailed me with a challenge to modify the standard matlab Command-Window prompt from &#8220;&gt;&gt; &#8221; to some other string, preferably a dynamic prompt with the current timestamp. At first thought this cannot be done: The Command-Window prompts are hard-coded and to the best of my knowledge cannot be modified via <a
target="_blank" href="http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/">properties</a> or <a
target="_blank" href="http://undocumentedmatlab.com/blog/changing-system-preferences-programmatically/">system preferences</a>.</p><p>So the prompt can (probably) not be modified in advance, but what if it could be modified after being displayed? It is true that my <a
target="_blank" href="http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/"><b<i>cprintf</i></b> utility</a> modifies the Command-Window contents in order to display formatted text in a variety of font colors. But this case is different since <b<i>cprintf</i></b> runs once synchronously (user-invoked), whereas the prompt appears asynchronously multiple times.</p><p>There are two methods of handling multiple asynchronous events in Matlab: setting a callback on the object, and setting a PostSet <b><i>handle.listener</i></b> (or <b><i>schema.listener</i></b>) on the relevant object property. The first of these methods is a well-known Matlab practice, although we shall see that it uses an undocumented callback and functionality; the PostSet method is entirely undocumented and not well-known and shall be described in some later article. I decided to use the callback method to set the prompt &#8211; interested readers can try the PostSet method.</p><h3>Setting the Command Window&#8217;s callback</h3><p>The solution involved finding the Command-Window reference handle, and setting one of its <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">many callbacks</a>, in our case <b>CaretUpdateCallback</b>. This callback is fired whenever the desktop text is modified, which is an event we trap to replace the displayed prompt:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Get the reference handle to the Command Window text area</span>
jDesktop = com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLDesktop</span>.<span style="">getInstance</span>;
<span style="color: #0000FF;">try</span>
  cmdWin = jDesktop.<span style="">getClient</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Command Window'</span><span style="color: #080;">&#41;</span>;
  jTextArea = cmdWin.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>.<span style="">getViewport</span>.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">catch</span>
  commandwindow;
  jTextArea = jDesktop.<span style="">getMainFrame</span>.<span style="">getFocusOwner</span>;
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Instrument the text area's callback</span>
<span style="color: #0000FF;">if</span> <span style="color: #0000FF;">nargin</span> <span style="color: #F0F;">&amp;&amp;</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span> <span style="color: #F0F;">&amp;&amp;</span> ~<span style="color: #0000FF;">strcmp</span><span style="color: #080;">&#40;</span>newPrompt,<span style="color:#A020F0;">'&gt;&gt; '</span><span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'CaretUpdateCallback'</span>,<span style="color: #080;">&#123;</span>@setPromptFcn,newPrompt<span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">else</span>
  <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'CaretUpdateCallback'</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>Now that we have the Command-Window object callback set, we need to set the logic of prompt replacement &#8211; this is done in the internal Matlab function <i>setPromptFcn</i>. Here is its core code:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Does the displayed text end with the default prompt?</span>
<span style="color: #228B22;">% Note: catch a possible trailing newline</span>
<span style="color: #0000FF;">try</span> jTextArea = jTextArea.<span style="">java</span>;  <span style="color: #0000FF;">catch</span>,  <span style="color: #0000FF;">end</span>  <span style="color: #228B22;">%#ok</span>
cwText = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'Text'</span><span style="color: #080;">&#41;</span>;
pos = strfind<span style="color: #080;">&#40;</span>cwText<span style="color: #080;">&#40;</span><span style="color: #0000FF;">max</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,end-<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span><span style="color: #F0F;">:</span><span style="color: #0000FF;">end</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'&gt;&gt; '</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>pos<span style="color: #080;">&#41;</span>
  <span style="color: #228B22;">% Short prompts need to be space-padded</span>
  newLen = jTextArea.<span style="">getCaretPosition</span>;
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span>&lt;<span style="color: #33f;">3</span>
    newPrompt<span style="color: #080;">&#40;</span><span style="color: #0000FF;">end</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:#A020F0;">' '</span>;
  <span style="color: #0000FF;">elseif</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span>&gt;<span style="color: #33f;">3</span>
    fprintfStr = newPrompt<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #F0F;">:</span>end-<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">fprintf</span><span style="color: #080;">&#40;</span>fprintfStr<span style="color: #080;">&#41;</span>;
    newLen = newLen + <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>fprintfStr<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% The Command-Window text should be modified on the EDT</span>
  <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'replaceRange(Ljava.lang.String;II)'</span>,<span style="color: #F0F;">...</span>
            <span style="">newPrompt</span><span style="color: #080;">&#40;</span>end-<span style="color: #33f;">2</span><span style="color: #F0F;">:</span><span style="color: #0000FF;">end</span><span style="color: #080;">&#41;</span>, newLen-<span style="color: #33f;">3</span>, newLen<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'repaint()'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>In this code snippet, note that we space-pad prompt string that are shorter than 3 characters: this is done to prevent an internal-Matlab mixup when displaying additional text &#8211; Matlab &#8220;knows&#8221; the Command-Window&#8217;s text position and it gets mixed up if it turns out to be shorter than expected.</p><p>Also note that I use the semi-documented <b><i>awtinvoke</i></b> function to replace the default prompt (and an automatically-appended space) on the Event-Dispatch Thread (more on this in a future article). Since Matlab R2008a, I could use the more convenient <b><i>javaMethodEDT</i></b> function, but I wanted my code to work on all prior Matlab 7 versions, where <b><i>javaMethodEDT</i></b> was not yet available.</p><h3>Preventing callback re-entry</h3><p>The callback snippet above would enter an endless loop if not changed: whenever the prompt is modified the callback would have been re-fired, the prompt re-modified and so on endlessly. There are many methods of preventing callback re-entry &#8211; here&#8217;s the one I chose:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> setPromptFcn<span style="color: #080;">&#40;</span>jTextArea,eventData,newPrompt<span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #228B22;">% Prevent overlapping reentry due to prompt replacement</span>
  <span style="color: #0000FF;">persistent</span> inProgress
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>inProgress<span style="color: #080;">&#41;</span>
    inProgress = <span style="color: #33f;">1</span>;  <span style="color: #228B22;">%#ok unused</span>
  <span style="color: #0000FF;">else</span>
    <span style="color: #0000FF;">return</span>;
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #0000FF;">try</span>
    <span style="color: #228B22;">% *** Prompt modification code goes here ***</span>
&nbsp;
    <span style="color: #228B22;">% force prompt-change callback to fizzle-out...</span>
    <span style="color: #0000FF;">pause</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0.02</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Never mind - ignore errors...</span>
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% Enable new callbacks now that the prompt has been modified</span>
  inProgress = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>;
&nbsp;
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% setPromptFcn</span></pre></div></div><h3>Handling multiple prompt types</h3><p>I now wanted my function to handle both static prompt strings (like: &#8216;[Yair] &#8216;) and dynamic prompts (like: &#8216;[25-Jan-2010 01:00:51] &#8216;). This is done by accepting string-evaluable strings/functions:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Try to evaluate the new prompt as a function</span>
<span style="color: #0000FF;">try</span>
  origNewPrompt = newPrompt;
  newPrompt = <span style="color: #0000FF;">feval</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">catch</span>
  <span style="color: #0000FF;">try</span>
    newPrompt = <span style="color: #0000FF;">eval</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Never mind - probably a string...</span>
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">ischar</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span> <span style="color: #F0F;">&amp;&amp;</span> <span style="color: #0000FF;">ischar</span><span style="color: #080;">&#40;</span>origNewPrompt<span style="color: #080;">&#41;</span>
  newPrompt = origNewPrompt;
<span style="color: #0000FF;">end</span></pre></div></div><h3>File Exchange submission</h3><p>I then added some edge-case error handling and wrapped everything in a single utility called <b><i>setPrompt</i></b> that is now <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/26471">available on the File Exchange</a>.</p><p>In the future, if I find time, energy and interest, maybe I&#8217;ll combine <b><i>cprintf</i></b>&#8216;s font-styling capabilities, to enable setting colored prompts.</p><h3>Setting a continuously-updated timestamp prompt</h3><p>Using the code above, we can now display a dynamic timestamp prompt, as follows:<br
/><center><div
class="wp-caption aligncenter" style="width: 438px"><img
alt="setPrompt usage examples" src="http://UndocumentedMatlab.com/images/setPrompt.png" title="setPrompt usage examples" width="428" height="224" /><p
class="wp-caption-text"><b><i>setPrompt</i></b> usage examples</p></div></center></p><p>However, the displayed timestamp is somewhat problematic in the sense that it indicates the time of prompt <u>creation</u> rather than the time that the associated Command-Window command was <u>executed</u>. In the screenshot above, [25-Jan-2010 01:29:42] is the time that the <b><i>234</i></b> command was executed, not the time that the <b><i>setPrompt</i></b> command was executed. This is somewhat misleading. It would be better if the last (current) timestamp was continuously updated and would therefore always display the latest command&#8217;s execution time. This can be done using a Matlab <b><i>timer</i></b> as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% This is entered in the main function before setting the prompt:</span>
stopPromptTimers;
<span style="color: #0000FF;">if</span> <span style="color: #0000FF;">nargin</span> <span style="color: #F0F;">&amp;&amp;</span> <span style="color: #0000FF;">strcmpi</span><span style="color: #080;">&#40;</span>newPrompt,<span style="color:#A020F0;">'timestamp'</span><span style="color: #080;">&#41;</span>
  <span style="color: #228B22;">% Update initial prompt &amp; prepare a timer to continuously update it</span>
  newPrompt = @<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color:#A020F0;">'['</span>,<span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">now</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'] '</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
  start<span style="color: #080;">&#40;</span>timer<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Tag'</span>,<span style="color:#A020F0;">'setPromptTimer'</span>, <span style="color:#A020F0;">'Name'</span>,<span style="color:#A020F0;">'setPromptTimer'</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'ExecutionMode'</span>,<span style="color:#A020F0;">'fixedDelay'</span>, <span style="color:#A020F0;">'ObjectVisibility'</span>,<span style="color:#A020F0;">'off'</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'Period'</span>,<span style="color: #33f;">0.99</span>, <span style="color:#A020F0;">'StartDelay'</span>,<span style="color: #33f;">0.5</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'TimerFcn'</span>,<span style="color: #080;">&#123;</span>@setPromptTimerFcn,jTextArea<span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Stop &amp; delete any existing prompt timer(s)</span>
<span style="color: #0000FF;">function</span> stopPromptTimers
  <span style="color: #0000FF;">try</span>
    timers = timerfindall<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'tag'</span>,<span style="color:#A020F0;">'setPromptTimer'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>timers<span style="color: #080;">&#41;</span>
      stop<span style="color: #080;">&#40;</span>timers<span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">delete</span><span style="color: #080;">&#40;</span>timers<span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Never mind...</span>
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% stopPromptTimers</span>
&nbsp;
<span style="color: #228B22;">% Internal timer callback function</span>
<span style="color: #0000FF;">function</span> setPromptTimerFcn<span style="color: #080;">&#40;</span>timerObj,eventData,jTextArea<span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">try</span>
    <span style="color: #0000FF;">try</span> jTextArea = jTextArea.<span style="">java</span>;  <span style="color: #0000FF;">catch</span>,  <span style="color: #0000FF;">end</span>  <span style="color: #228B22;">%#ok</span>
    pos = <span style="color: #0000FF;">getappdata</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'setPromptPos'</span><span style="color: #080;">&#41;</span>;
    newPrompt = <span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">now</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'replaceRange(Ljava.lang.String;II)'</span>,<span style="color: #F0F;">...</span>
              <span style="">newPrompt</span>, pos, pos+<span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'repaint()'</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Never mind...</span>
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% setPromptTimerFcn</span></pre></div></div><p>Can you come up with some innovative prompts? If so, please share them in a comment below.</p><p><u>Update 2010-Jan-26</u>: The code in this article was updated since it was first published yesterday.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/setting-the-matlab-desktop-layout-programmatically/' rel='bookmark' title='Setting the Matlab desktop layout programmatically'>Setting the Matlab desktop layout programmatically</a> <small>The Matlab desktop enables saving and switching layouts using the main menu. This post shows how to do so programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-desktop-tab-completions/' rel='bookmark' title='Setting desktop tab completions'>Setting desktop tab completions</a> <small>The Matlab desktop's Command-Window tab-completion can be customized for user-defined functions...</small></li><li><a
href='http://undocumentedmatlab.com/blog/editormacro-v2-setting-command-window-key-bindings/' rel='bookmark' title='EditorMacro v2 &#8211; setting Command Window key-bindings'>EditorMacro v2 &#8211; setting Command Window key-bindings</a> <small>The EditorMacro utility was extended to support built-in Matlab Editor and Command-Window actions and key-bindings. This post describes the changes and the implementation details....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-status-bar-text/' rel='bookmark' title='Setting status-bar text'>Setting status-bar text</a> <small>The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/feed/</wfw:commentRss> <slash:comments>9</slash:comments> </item> <item><title>Context-Sensitive Help</title><link>http://undocumentedmatlab.com/blog/context-sensitive-help/</link> <comments>http://undocumentedmatlab.com/blog/context-sensitive-help/#comments</comments> <pubDate>Wed, 05 Aug 2009 18:20:26 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Figure window]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Listeners]]></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[Listener]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[schema.prop]]></category> <category><![CDATA[uitools]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=491</guid> <description><![CDATA[Matlab has a hidden/unsupported built-in mechanism for easy implementation of context-sensitive help<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/adding-context-menu-to-uitree/' rel='bookmark' title='Adding a context-menu to a uitree'>Adding a context-menu to a uitree</a> <small>uitree is an undocumented Matlab function, which does not easily enable setting a context-menu. Here's how to do it....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-workspace-context-menu/' rel='bookmark' title='Customizing Workspace context-menu'>Customizing Workspace context-menu</a> <small>Matlab's Workspace table context-menu can be configured with user-defined actions - this article explains how....</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><li><a
href='http://undocumentedmatlab.com/blog/customizing-print-setup/' rel='bookmark' title='Customizing print setup'>Customizing print setup</a> <small>Matlab figures print-setup can be customized to automatically prepare the figure for printing in a specific configuration...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A recent <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/257077">CSSM thread about implementing a system-wide GUI help system</a> got me working on a post to present Matlab&#8217;s built-in hidden/unsupported mechanism for context-sensitive help. There are many different ways in which such a system can be implemented (read that thread for some ideas), so Matlab users are by no means limited to Matlab&#8217;s built-in implementation. However, the built-in system certainly merits consideration for its simplicity and usefulness.</p><p>We start with Matlab&#8217;s <b><i>cshelp</i></b> function (%matlabroot%\toolbox\matlab\uitools\cshelp.m). <b><i>cshelp</i></b> is semi-documented, meaning that it has a help section but no doc, online help or official support. This useful function was grandfathered (made obsolete) in Matlab 7.4 (R2007a) for an unknown reason and to my knowledge without any replacement. <b><i>cshelp</i></b> is entirely based on m-code (no hidden internal or Java code) and is surprisingly compact and readable.</p><p><b><i>cshelp</i></b> basically attaches two new properties (<i>CSHelpMode</i> and <i>CSHelpData</i>) to the specified figure (this is done using the <b><i>schema.prop</i></b> mechanism which will be described in a separate post), temporarily disables all active uicontrols, modifies the figure&#8217;s <i>WindowButtonDownFcn</i> callback and sets the mouse cursor (using <b><i>setptr</b></i> &#8211; another semi-documented function) to an arrow with a question mark.</p><p>Clicking any object in the figure&#8217;s main area (beneath the toolbar), causes the modified <i>WindowButtonDownFcn</i> callback to run whatever is stored in the figure&#8217;s <i>HelpFcn</i> property (string, @function_handle or {@function_handle, params, …} format). Here is a <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/101165">simple example taken from CSSM</a> (thanks J&eacute;r&ocirc;me):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">Hfcn = <span style="color:#A020F0;">'str=get(gco,'</span><span style="color:#A020F0;">'type'</span><span style="color:#A020F0;">'); title(['</span><span style="color:#A020F0;">'Type :'</span><span style="color:#A020F0;">' str])'</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>,<span style="color:#A020F0;">'HelpFcn'</span>,Hfcn<span style="color: #080;">&#41;</span>;
th = <span style="color: #33f;">0</span><span style="color: #F0F;">:</span><span style="color: #33f;">0.314</span><span style="color: #F0F;">:</span><span style="color: #33f;">2</span>*<span style="color: #0000FF;">pi</span>;
<span style="color: #0000FF;">plot</span><span style="color: #080;">&#40;</span>th,<span style="color: #0000FF;">sin</span><span style="color: #080;">&#40;</span>th<span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'r-'</span>,<span style="color:#A020F0;">'linewidth'</span>,<span style="color: #33f;">4</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'normalized'</span>, <span style="color:#A020F0;">'position'</span>,<span style="color: #080;">&#91;</span>.45 .02 .1 .05<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
cshelp<span style="color: #080;">&#40;</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><span style="color: #0000FF;">gcf</span>,<span style="color:#A020F0;">'CSHelpMode'</span>,<span style="color:#A020F0;">'on'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 301px"><img
alt="Simple context-sensitive help system" src="http://UndocumentedMatlab.com/images/cshelp2.png" title="Simple context-sensitive help system" width="291" height="298" /><p
class="wp-caption-text">Simple context-sensitive help system</p></div></center></p><p>In order to exit CSHelp mode, the figure&#8217;s <i>CSHelpMode</i> property must be set to &#8216;off&#8217;. However, remember that all the figure&#8217;s uicontrols are disabled in CSHelp mode. Therefore, the user may use one or more of the following methods (other tactics are also possible, but the ones below seem intuitive):</p><ul><li>Set the figure&#8217;s <i>KeyPressFcn</i> callback property to catch events (e.g., &lt;ESC&gt; key presses) and reset the <i>CSHelpMode</i> property from within the callback</li><li>Reset the <i>CSHelpMode</i> property at the end of the HelpFcn callback</li><li>Add a CS Help entry/exit option to the figure&#8217;s Help main menu</li><li>Add a CS Help entry/exit button to the figure toolbar</li></ul><p>The following code sample implements all of these suggested tactics (the code to synchronize the states of the menu item and toolbar button is not presented):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Set the &lt;ESC&gt; key press to exit CSHelp mode</span>
keyFcn = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'if strcmp(get(gcbf,'</span><span style="color:#A020F0;">'CurrentKey'</span><span style="color:#A020F0;">'),'</span><span style="color:#A020F0;">'escape'</span><span style="color:#A020F0;">'), '</span> <span style="color: #F0F;">...</span>
             <span style="color:#A020F0;">'set(gcbf,'</span><span style="color:#A020F0;">'CSHelpMode'</span><span style="color:#A020F0;">','</span><span style="color:#A020F0;">'off'</span><span style="color:#A020F0;">'); '</span> <span style="color: #F0F;">...</span>
          <span style="color:#A020F0;">'end'</span><span style="color: #080;">&#93;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>,<span style="color:#A020F0;">'keyPressFcn'</span>,keyFcn<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Exit CSHelp mode at the end of the CSHelp callback</span>
helpFcn = <span style="color:#A020F0;">'title(['</span><span style="color:#A020F0;">'Type :'</span><span style="color:#A020F0;">' get(gco,'</span><span style="color:#A020F0;">'type'</span><span style="color:#A020F0;">')]); set(gcbf,'</span><span style="color:#A020F0;">'CSHelpMode'</span><span style="color:#A020F0;">','</span><span style="color:#A020F0;">'off'</span><span style="color:#A020F0;">');'</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>,<span style="color:#A020F0;">'HelpFcn'</span>,helpFcn<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Add a CSHelp button to the figure toolbar</span>
<span style="color: #228B22;">% Note: retrieve the button icon from the CSHelp cursor icon</span>
hToolbar = findall<span style="color: #080;">&#40;</span>allchild<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'flat'</span>,<span style="color:#A020F0;">'type'</span>,<span style="color:#A020F0;">'uitoolbar'</span><span style="color: #080;">&#41;</span>;
oldPtr = getptr<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;
ptrData = setptr<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'help'</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>, oldPtr<span style="color: #080;">&#123;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
icon<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span>,<span style="color: #F0F;">:</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = ptrData<span style="color: #080;">&#123;</span><span style="color: #33f;">4</span><span style="color: #080;">&#125;</span>/<span style="color: #33f;">2</span>;  <span style="color: #228B22;">% Convert into RGB TrueColor icon</span>
icon<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span>,<span style="color: #F0F;">:</span>,<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> = ptrData<span style="color: #080;">&#123;</span><span style="color: #33f;">4</span><span style="color: #080;">&#125;</span>/<span style="color: #33f;">2</span>;
icon<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span>,<span style="color: #F0F;">:</span>,<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span> = ptrData<span style="color: #080;">&#123;</span><span style="color: #33f;">4</span><span style="color: #080;">&#125;</span>/<span style="color: #33f;">2</span>;
cbFcn = <span style="color:#A020F0;">'set(gcbf,'</span><span style="color:#A020F0;">'CSHelpMode'</span><span style="color:#A020F0;">',get(gcbo,'</span><span style="color:#A020F0;">'state'</span><span style="color:#A020F0;">'))'</span>;
csName = <span style="color:#A020F0;">'Context-sensitive help'</span>;
uitoggletool<span style="color: #080;">&#40;</span>hToolbar,<span style="color:#A020F0;">'CData'</span>,icon, <span style="color:#A020F0;">'ClickedCallback'</span>,cbFcn, <span style="color:#A020F0;">'TooltipString'</span>,csName<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Add a CSHelp menu option to the Help main menu</span>
<span style="color: #228B22;">% Note: unlike other main menus, the Help menu tag is empty, so</span>
<span style="color: #228B22;">% ^^^^  findall(gcf,'tag','figMenuHelp') is empty... Therefore,</span>
<span style="color: #228B22;">%       we find this menu by accessing the Help/About menu item</span>
helpAbout = findall<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>,<span style="color:#A020F0;">'tag'</span>,<span style="color:#A020F0;">'figMenuHelpAbout'</span><span style="color: #080;">&#41;</span>;
helpMenu = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>helpAbout,<span style="color:#A020F0;">'parent'</span><span style="color: #080;">&#41;</span>;
cbFcn = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'if strcmp(get(gcbo,'</span><span style="color:#A020F0;">'Checked'</span><span style="color:#A020F0;">'),'</span><span style="color:#A020F0;">'on'</span><span style="color:#A020F0;">'), '</span> <span style="color: #F0F;">...</span>
             <span style="color:#A020F0;">'set(gcbo,'</span><span style="color:#A020F0;">'Checked'</span><span style="color:#A020F0;">','</span><span style="color:#A020F0;">'off'</span><span style="color:#A020F0;">'); '</span> <span style="color: #F0F;">...</span>
         <span style="color:#A020F0;">'else, '</span> <span style="color: #F0F;">...</span>
             <span style="color:#A020F0;">'set(gcbo,'</span><span style="color:#A020F0;">'Checked'</span><span style="color:#A020F0;">','</span><span style="color:#A020F0;">'on'</span><span style="color:#A020F0;">'); '</span> <span style="color: #F0F;">...</span>
         <span style="color:#A020F0;">'end; '</span> <span style="color: #F0F;">...</span>
         <span style="color:#A020F0;">'set(gcbf,'</span><span style="color:#A020F0;">'CSHelpMode'</span><span style="color:#A020F0;">',get(gcbo,'</span><span style="color:#A020F0;">'checked'</span><span style="color:#A020F0;">'))'</span><span style="color: #080;">&#93;</span>;
<span style="color: #0000FF;">uimenu</span><span style="color: #080;">&#40;</span>helpMenu,<span style="color:#A020F0;">'Label'</span>,csName,<span style="color:#A020F0;">'Callback'</span>,cbFcn,<span style="color:#A020F0;">'Separator'</span>,<span style="color:#A020F0;">'on'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="Figure with context-sensitive help action in the main toolbar &#038; menu" src="http://UndocumentedMatlab.com/images/cshelp3.png" title="Figure with context-sensitive help action in the main toolbar &#038; menu" width="450" height="300" /><p
class="wp-caption-text">Figure with context-sensitive help action in the main toolbar &#038; menu</p></div></center></p><p><b><i>cshelp</i></b> has an additional optional argument, accepting another figure handle. This handle, if specified and valid, indicates a parent figure whose <i>CSHelpMode</i>, <i>HelpFcn</i> and <i>HelpTopicMap</i> properties should be shared with this figure (this is done using the <b><i>handle.listener</i></b> mechanism which will be described in a separate post). This option is useful when creating a multi-window GUI-wide context-sensitive help system. The user may then activate context-sensitive help in figure A and select the requested context object in figure B.</p><p><b><i>cshelp</i></b> would normally be coupled with Matlab&#8217;s help system for non-trivial GUI implementations. The undocumented and hidden properties <i>HelpTopicKey</i> (of all handles) and <i>HelpTopicMap</i> (of figures), enable easy tie-in to the CSHelp system. A simplified sample is presented below:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">Hfcn=<span style="color: #080;">&#91;</span><span style="color:#A020F0;">'helpview(get(gco,'</span><span style="color:#A020F0;">'HelpTopicKey'</span><span style="color:#A020F0;">'),'</span><span style="color:#A020F0;">'CSHelpWindow'</span><span style="color:#A020F0;">');'</span><span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'set(gcbf,'</span><span style="color:#A020F0;">'CSHelpMode'</span><span style="color:#A020F0;">','</span><span style="color:#A020F0;">'off'</span><span style="color:#A020F0;">');'</span> <span style="color: #080;">&#93;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>,<span style="color:#A020F0;">'HelpFcn'</span>,Hfcn<span style="color: #080;">&#41;</span>;
th = <span style="color: #33f;">0</span><span style="color: #F0F;">:</span><span style="color: #33f;">0.314</span><span style="color: #F0F;">:</span><span style="color: #33f;">2</span>*<span style="color: #0000FF;">pi</span>;
hLine = <span style="color: #0000FF;">plot</span><span style="color: #080;">&#40;</span>th,<span style="color: #0000FF;">sin</span><span style="color: #080;">&#40;</span>th<span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'r-'</span>,<span style="color:#A020F0;">'linewidth'</span>,<span style="color: #33f;">4</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hLine,<span style="color:#A020F0;">'HelpTopicKey'</span>,<span style="color:#A020F0;">'MyCSHelpFile.html#Line'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,  <span style="color:#A020F0;">'HelpTopicKey'</span>,<span style="color:#A020F0;">'MyCSHelpFile.html#Axes'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><b><i>cshelp</i></b> is by no way limited to presenting Matlab documentation: Refer to <b><i>helpview</i></b>&#8216;s help section for an in-depth description of help maps and help topics. In a nutshell, <b><i>helpview</i></b> accepts any HTML webpage filepath (or webpage internal (#) reference), followed by optional parameter &#8216;CSHelpWindow&#8217; (that indicates that the specified help page should be displayed in a stand-alone popup window rather than in the desktop&#8217;s standard Help tab), and optional extra parameters specifying the popup window&#8217;s figure handle and position. The webpage filepath parameter may be replaced by two string parameters, HelpTopicMap filepath and HelpTopicKey. Note that <b><i>helpview</i></b> itself is another semi-documented function.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/adding-context-menu-to-uitree/' rel='bookmark' title='Adding a context-menu to a uitree'>Adding a context-menu to a uitree</a> <small>uitree is an undocumented Matlab function, which does not easily enable setting a context-menu. Here's how to do it....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-workspace-context-menu/' rel='bookmark' title='Customizing Workspace context-menu'>Customizing Workspace context-menu</a> <small>Matlab's Workspace table context-menu can be configured with user-defined actions - this article explains how....</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><li><a
href='http://undocumentedmatlab.com/blog/customizing-print-setup/' rel='bookmark' title='Customizing print setup'>Customizing print setup</a> <small>Matlab figures print-setup can be customized to automatically prepare the figure for printing in a specific configuration...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/context-sensitive-help/feed/</wfw:commentRss> <slash:comments>10</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           tag
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-02-04 03:19:22 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/tag/listener/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      2.782s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1328350759; expires=Sun, 03-Feb-2013 10:19:19 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Sat, 04 Feb 2012 10:19:22 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Sat, 04 Feb 2012 11:19:22 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               a637dddec0e8559dfa6cd8a519ba86c8
Content-Encoding:   gzip
-->
