<?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; Guest bloggers</title> <atom:link href="http://undocumentedmatlab.com/blog/category/guest-bloggers/feed/" rel="self" type="application/rss+xml" /><link>http://undocumentedmatlab.com</link> <description>Charting Matlab's unsupported hidden underbelly</description> <lastBuildDate>Wed, 08 Feb 2012 18:40:25 +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>Controlling callback re-entrancy</title><link>http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy/</link> <comments>http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy/#comments</comments> <pubDate>Wed, 10 Aug 2011 18:00:57 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[COM]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Malcolm Lidierth]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2403</guid> <description><![CDATA[Callback reentrancy is a major problem for frequently-fired events. Luckily, it can easily be solved.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/continuous-slider-callback/' rel='bookmark' title='Continuous slider callback'>Continuous slider callback</a> <small>Matlab slider uicontrols do not enable a continuous-motion callback by default. This article explains how this can be achieved using undocumented features....</small></li><li><a
href='http://undocumentedmatlab.com/blog/controlling-plot-data-tips/' rel='bookmark' title='Controlling plot data-tips'>Controlling plot data-tips</a> <small>Data-tips are an extremely useful plotting tool that can easily be controlled programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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/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><i>I&#8217;d like to welcome guest blogger <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/authors/23816">Malcolm Lidierth</a> of King&#8217;s College London. Malcolm is well known in the Matlab-Java community for his <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/15580-using-java-swing-components-in-matlab">jcontrol utility</a>. Some months ago, I mentioned his work on another File Exchange contribution, <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/28326-mutilities">MUtilities</a> when I discussed <a
target="_blank" href="http://undocumentedmatlab.com/blog/transparent-matlab-figure-window/">transparent Matlab figure windows</a>. Today, Malcolm discusses one of his lesser-known but extremely important <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/26027-ismultiplecall">isMultipleCall utility</a>.</i></p><p>Every now and again, a very simple bit of code turns out to be more useful than the author initially imagined. Something I have repeatedly used is the <i><b>isMultipleCall</b></i> function which I <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/26027-ismultiplecall">posted</a> to MATLAB Central&#8217;s File Exchange a year or so ago.</p><p>The <i><b>isMultipleCall</b></i> function uses fully-documented pure-MATLAB to extend the control that can be achieved over callbacks.</p><p>Here was the problem: I had a modular system built in MATLAB which allowed third-party developers to add their own plugins. I wanted a mechanism to force the dismissal (&#8220;bail-out&#8221;) of a callback even when the <b>Interruptible</b> property of the parent object was set to &#8216;on&#8217;. Such callback re-entrancy issues are common for rapidly-firing events, and debugging and fixing them is usually <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/answers/5809-how-to-implement-a-speed-limit-for-gui-button-press">not easy</a>.</p><p>The callback&#8217;s dismissal code would need to be fast because it might be called many dozens of times, e.g. in a <b>WindowButtonMotion</b> callback. An obvious approach was to check the function call stack using MATLAB&#8217;s <i><b>dbstack</b></i> function. Although, at first, this seemed likely to be too slow, profiling showed it was not – taking &lt; 40&micro;sec per call &#8211; and within a <b>WindowButtonMotion</b> callback in a real GUI, I could not perceive any slowing of the code.</p><p>Here is the function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> <span style="color: #0000FF;">flag</span>=isMultipleCall<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">flag</span> = <span style="color: #0000FF;">false</span>; 
  <span style="color: #228B22;">% Get the stack</span>
  s = <span style="color: #0000FF;">dbstack</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">if</span> numel<span style="color: #080;">&#40;</span>s<span style="color: #080;">&#41;</span>&lt; =<span style="color: #33f;">2</span>
    <span style="color: #228B22;">% Stack too short for a multiple call</span>
    <span style="color: #0000FF;">return</span>
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% How many calls to the calling function are in the stack?</span>
  names = <span style="color: #080;">&#123;</span>s<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#41;</span>.<span style="">name</span><span style="color: #080;">&#125;</span>;
  TF = <span style="color: #0000FF;">strcmp</span><span style="color: #080;">&#40;</span>s<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>.<span style="">name</span>,names<span style="color: #080;">&#41;</span>;
  count = <span style="color: #0000FF;">sum</span><span style="color: #080;">&#40;</span>TF<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">if</span> count&gt;<span style="color: #33f;">1</span>
    <span style="color: #228B22;">% More than 1</span>
    <span style="color: #0000FF;">flag</span> = <span style="color: #0000FF;">true</span>; 
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span></pre></div></div><p>With <i><b>isMultipleCall</b></i> invoked from another function (see note below), <i><b>dbstack</b></i> will return a structure with a minimum of 2 elements – the first relating to <i><b>isMultipleCall</b></i> itself and the second to the calling function. So with <i><b>numel</b></i>(s) &lt;= 2, there can be no multiple calls and we can return false immediately thus saving time in doing any further testing.  For <i><b>numel</b></i>(s) &gt; 2 we simply check to see whether the calling functions referenced in s(2) appears anywhere else on the stack. If it does, then we return true; otherwise false.</p><p>Then, in our callback code we simply use:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">if</span> isMultipleCall<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;  <span style="color: #0000FF;">return</span>;  <span style="color: #0000FF;">end</span></pre></div></div><p>If this line is placed first in the callback function code, it essentially mimics the behavior that you might expect after setting the <b>Interruptible</b> property of the event firing object to &#8216;off&#8217;. Adding a <i><b>drawnow()</b></i> at the end of the callback will ensure that any waiting callbacks in the queue are dismissed:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> MyCallback<span style="color: #080;">&#40;</span>hObj, EventData<span style="color: #080;">&#41;</span>
  <span style="color: #228B22;">% Quick bail-out if callback code is called before another has ended</span>
  <span style="color: #0000FF;">if</span> isMultipleCall<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;  <span style="color: #0000FF;">return</span>;  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #F0F;">...</span>  <span style="color: #228B22;">% do some actual callback work here</span>
  <span style="color: #0000FF;">drawnow</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>There are several ways in which <i><b>isMultipleCall</b></i> can extend the standard MALAB functionality. First, by moving <i><b>isMultipleCall</b></i> reference from the first line of the callback we can create both an interruptible and an uninteruptible code block, e.g.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> MyCallback<span style="color: #080;">&#40;</span>hObj, EventData<span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #228B22;">%Code Block 1</span>
  <span style="color: #F0F;">...</span>
&nbsp;
  <span style="color: #0000FF;">if</span> isMultipleCall<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;  <span style="color: #0000FF;">return</span>;  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">%Code Block 2</span>
  <span style="color: #F0F;">...</span>
&nbsp;
  <span style="color: #0000FF;">drawnow</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>Second, as <i><b>isMultipleCall</b></i> controls the callbacks  &#8211; not the objects that trigger them &#8211; we can individually control the callbacks of objects which fire multiple events. That is particularly useful with Java components, which gives a third extension – <i><b>isMultipleCall</b></i> can be used in any function: not just the callbacks of standard MATLAB components, but also of Java or COM components.<br
/>  <br
/> Finally, as the callback, not the object is being controlled, we can control a callback that may be shared between multiple objects e.g. a menu component and a toolbar button.</p><p>Not bad for 13 lines of code.</p><p>Note: <i><b>isMultipleCall</b></i> must be called from a function, not from a string in the callback property.</p><p>Do you have any other favorite mechanism for controlling callback re-entrancy? If so, please <a
href="http://UndocumentedMatlab.com/blog/controlling-callback-re-entrancy/#respond">post a comment</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/continuous-slider-callback/' rel='bookmark' title='Continuous slider callback'>Continuous slider callback</a> <small>Matlab slider uicontrols do not enable a continuous-motion callback by default. This article explains how this can be achieved using undocumented features....</small></li><li><a
href='http://undocumentedmatlab.com/blog/controlling-plot-data-tips/' rel='bookmark' title='Controlling plot data-tips'>Controlling plot data-tips</a> <small>Data-tips are an extremely useful plotting tool that can easily be controlled programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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/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/controlling-callback-re-entrancy/feed/</wfw:commentRss> <slash:comments>11</slash:comments> </item> <item><title>JBoost &#8211; Integrating an external Java library in Matlab</title><link>http://undocumentedmatlab.com/blog/jboost-integrating-an-external-java-library-in-matlab/</link> <comments>http://undocumentedmatlab.com/blog/jboost-integrating-an-external-java-library-in-matlab/#comments</comments> <pubDate>Wed, 29 Jun 2011 18:00:58 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Adam Chapman]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2357</guid> <description><![CDATA[This article shows how an external Java library can be integrated in Matlab<pre> </pre>Related posts:<ol><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/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/matlab-java-memory-leaks-performance/' rel='bookmark' title='Matlab-Java memory leaks, performance'>Matlab-Java memory leaks, performance</a> <small>Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/converting-java-vectors-to-matlab-arrays/' rel='bookmark' title='Converting Java vectors to Matlab arrays'>Converting Java vectors to Matlab arrays</a> <small>Converting Java vectors to Matlab arrays is pretty simple - this article explains how....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>I would like to welcome guest blogger Adam Chapman, who has recently started a dedicated Matlab blog at <a
target="_blank" rel="nofollow" href="http://intelilab.blogspot.com/">http://intelilab.blogspot.com/</a>. Today, Adam shares his experience in integrating a 3rd-party Java library &#8211; in this case, the open-source JBoost library, in a Matlab application.</i></p><p>I had received a couple of requests asking me to create <a
target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Alternating_decision_tree">alternating decision trees</a> (ADTs) in Matlab. Due to the self-organising nature of an ADT structure, it would be an extremely slow and frustrating task to code up an ADT building algorithm in any language.</p><p>I was cautious of looking for source code that would do the job for me, as I was concerned that interfacing external code with Matlab would be another difficult task.</p><p>I had interfaced external VBA and C++ code with Matlab before, but never attempted interfacing Java code. I remember all of those previous interfacing projects being a nightmare to debug and very tricky to code into a stable solution. After some hesitation I decided that I would give the open-source <a
target="_blank" rel="nofollow" href="http://jboost.sourceforge.net/">Jboost library</a> a try.</p><p>It <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/matlab_external/f98533.html">turns out</a> that <i>&#8220;every Matlab installation includes a Java Virtual Machine (JVM), so that you can use the Java interpreter via MATLAB commands, and you can create and run programs that create and access Java objects&#8221;</i>.</p><p>That&#8217;s all very well, but I needed to know exactly how I should call the <code>main()</code> function in Jboost directly from Matlab. In this Article I&#8217;ll explain how I replicated <a
target="_blank" rel="nofollow" href="http://jboost.sourceforge.net/examples.html">the example walkthrough on the Jboost website</a>, using the spambase data set as an example.</p><p>Reading the Jboost <a
target="_blank" rel="nofollow" href="http://jboost.sourceforge.net/install.html">installation notes</a>, I found that three paths need to be added to the Java class path. Instead of adding a system environment variable like the Jboost <a
target="_blank" rel="nofollow" href="http://jboost.sourceforge.net/doc.html">documentation</a> suggests, we can simply use the Matlab&#8217;s built-in <i><b>javaaddpath</b></i> function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">JBOOST_DIR=<span style="color:#A020F0;">'C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2'</span>;
&nbsp;
newpath1=<span style="color: #080;">&#91;</span>JBOOST_DIR <span style="color:#A020F0;">'\dist\jboost.jar'</span><span style="color: #080;">&#93;</span>;
newpath2=<span style="color: #080;">&#91;</span>JBOOST_DIR <span style="color:#A020F0;">'\lib\jfreechart-1.0.10.jar'</span><span style="color: #080;">&#93;</span>;
newpath3=<span style="color: #080;">&#91;</span>JBOOST_DIR <span style="color:#A020F0;">'\lib\jcommon-1.0.8.jar'</span><span style="color: #080;">&#93;</span>;
&nbsp;
<span style="color: #228B22;">% add Jboost classes to javaclasspath</span>
javaaddpath<span style="color: #080;">&#40;</span>newpath1<span style="color: #080;">&#41;</span>
javaaddpath<span style="color: #080;">&#40;</span>newpath2<span style="color: #080;">&#41;</span>
javaaddpath<span style="color: #080;">&#40;</span>newpath3<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">% also add JBOOST_DIR to the matlab path so that matlab can look there for non-jave code and data files:</span>
<span style="color: #0000FF;">addpath</span><span style="color: #080;">&#40;</span>JBOOST_DIR<span style="color: #080;">&#41;</span></pre></div></div><p>we can check those paths were added by reading back the dynamic java classpath:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; javaclasspath<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'-dynamic'</span><span style="color: #080;">&#41;</span>
&nbsp;
DYNAMIC JAVA <span style="color: #0000FF;">PATH</span>
C<span style="color: #F0F;">:</span>\Users\Adam\Desktop\JBOOST\jboost-<span style="color: #33f;">2.2</span>\jboost-<span style="color: #33f;">2.2</span>\lib\jcommon-1.0.8.<span style="">jar</span>    
C<span style="color: #F0F;">:</span>\Users\Adam\Desktop\JBOOST\jboost-<span style="color: #33f;">2.2</span>\jboost-<span style="color: #33f;">2.2</span>\lib\jfreechart-1.0.10.<span style="">jar</span>
C<span style="color: #F0F;">:</span>\Users\Adam\Desktop\JBOOST\jboost-<span style="color: #33f;">2.2</span>\jboost-<span style="color: #33f;">2.2</span>\dist\jboost.<span style="">jar</span></pre></div></div><p>Now I had the paths set up for Matlab and the JVM to find code and files in, I had to work out how to actually run the Jboost <code>main()</code> function.</p><p>Matlab&#8217;s <i><b>methodsview</b></i> function lists information on the methods in a Java class:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">methodsview<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'jboost.controller.Controller'</span><span style="color: #080;">&#41;</span></pre></div></div><p>pops a new window, from which we can derive that the main function for Jboost expects a <code>java.lang.String</code> array object (which is standard for a <code>main()</code> function in Java):</p><p><center><div
class="wp-caption aligncenter" style="width: 610px"><a
target="_blank" href="http://UndocumentedMatlab.com/images/JBoost_methodsview.png"><img
alt="View of JBoost's class function using methodsview (click for details)" src="http://UndocumentedMatlab.com/images/JBoost_methodsview.png" title="View of JBoost's class function using methodsview (click for details)" width="600" /></a><p
class="wp-caption-text">View of JBoost's class function using <i><b>methodsview</b></i> (click for details)</p></div></center></p><p>(we could also have used Yair&#8217;s <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/17935-uiinspect-display-methods-properties-callbacks-of-an-object">UIINSPECT utility</a> on the File Exchange, to display even more information on the Java class&#8217;s methods, callbacks and properties, but for our simple usage here, the built-in <i><b>methodsview</b></i> function is sufficient)</p><p>To build a Java string array to input to Jboost, we use the built-in <i><b>java_array</b></i> function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">strArray = java_array<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'java.lang.String'</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
strArray<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = java.<span style="">lang</span>.<span style="">String</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'-S'</span><span style="color: #080;">&#41;</span>;
strArray<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> = java.<span style="">lang</span>.<span style="">String</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\demo\spambase'</span><span style="color: #080;">&#41;</span>; 
strArray<span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span> = java.<span style="">lang</span>.<span style="">String</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'-CONFIG'</span><span style="color: #080;">&#41;</span>;
strArray<span style="color: #080;">&#40;</span><span style="color: #33f;">4</span><span style="color: #080;">&#41;</span> = java.<span style="">lang</span>.<span style="">String</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'spambase.config'</span><span style="color: #080;">&#41;</span>;
strArray<span style="color: #080;">&#40;</span><span style="color: #33f;">5</span><span style="color: #080;">&#41;</span> = java.<span style="">lang</span>.<span style="">String</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'-m'</span><span style="color: #080;">&#41;</span>;
strArray<span style="color: #080;">&#40;</span><span style="color: #33f;">6</span><span style="color: #080;">&#41;</span> = java.<span style="">lang</span>.<span style="">String</span><span style="color: #080;">&#40;</span>‘MATLABTREE.<span style="">m</span>'<span style="color: #080;">&#41;</span>;</pre></div></div><p>Note how I initialized it with a size of 1 element, and let it grow as new elements are added since the array is very small there is no need to worry about the time saved by pre-allocating. The first 2 elements define the directory that Jboost is to look for data files in, the next 2 define a standard Jboost configuration file and the final 2 elements are rather special: they define the name of the m-file that will contain automatically generated Matlab code, allowing us to use the decision tree in the future without going in and out of Java.</p><p>Alternately, we could have simply used Matlab&#8217;s cell-array, as in:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">strArray = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'-S'</span>, <span style="color:#A020F0;">'C:\...'</span>, <span style="color:#A020F0;">'-CONFIG'</span>, <span style="color:#A020F0;">'spambase.config'</span>, <span style="color:#A020F0;">'-m'</span>, <span style="color:#A020F0;">'MATLABTREE.m'</span><span style="color: #080;">&#125;</span>;</pre></div></div><p>Now to construct an Alternating Decision Tree with Jboost, just call the Java jboost class containing the <code>main()</code> function, and input the array of parameters that we just constructed:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% input the configuration class into the jboost controller</span>
jboost.<span style="">controller</span>.<span style="">Controller</span>.<span style="">main</span><span style="color: #080;">&#40;</span>strArray<span style="color: #080;">&#41;</span></pre></div></div><p>With all the code so far, Matlab will invoke the Java jboost libraries to train an ADT to detect spam emails based on their contents defined in the spambase dataset. You will see the verbose java print-outs in the Matlab command window while it is running.</p><p>The jBoost developers use a Perl script to create an image of the completed ADT. That is even easier to call from Matlab than the Java routines were:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">perl<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'atree2dot2ps_hi_res.pl'</span>,<span style="color:#A020F0;">'-d'</span>,<span style="color:#A020F0;">'C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\demo\'</span>,<span style="color:#A020F0;">'-s'</span>,<span style="color:#A020F0;">'spambase.spec'</span>,<span style="color:#A020F0;">'-i'</span>,<span style="color:#A020F0;">'spambase.info'</span>,<span style="color:#A020F0;">'-t'</span>,<span style="color:#A020F0;">'spambase.output.tree'</span><span style="color: #080;">&#41;</span></pre></div></div><p>Now if you look in the folder that you defined in the &#8220;-d&#8221; parameter above, you will see that the Perl script has created a few images of the tree in different formats.  The .png file is below. I think it is incredible that an inanimate machine can be programmed to have the intelligence to create something so intricate.</p><p><center><div
class="wp-caption aligncenter" style="width: 810px"><a
target="_blank" rel="nofollow" href="http://img31.imageshack.us/img31/952/spambase0.png"><img
alt="Resulting ADT tree (click for details)" src="http://img31.imageshack.us/img31/952/spambase0.png" title="Resulting ADT tree (click for details)" width="800" /></a><p
class="wp-caption-text">Resulting ADT tree (click for details)</p></div></center></p><p>During this small project I realized that calling Java and Perl classes from Matlab is actually pretty easy. However, calling python, which jboost uses for some post-processing routines is actually very hard. I&#8217;ve decided to create my own Matlab versions of the error and margin plotting functions that Jboost uses, and will add them to the sourceforge project as soon as I&#8217;m happy with them.</p><p>I&#8217;d like to thank Aaron Arvey and Yair Altman for their friendly support during this project.</p><p><pre> </pre>Related posts:<ol><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/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/matlab-java-memory-leaks-performance/' rel='bookmark' title='Matlab-Java memory leaks, performance'>Matlab-Java memory leaks, performance</a> <small>Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/converting-java-vectors-to-matlab-arrays/' rel='bookmark' title='Converting Java vectors to Matlab arrays'>Converting Java vectors to Matlab arrays</a> <small>Converting Java vectors to Matlab arrays is pretty simple - this article explains how....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/jboost-integrating-an-external-java-library-in-matlab/feed/</wfw:commentRss> <slash:comments>0</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>UDD Properties</title><link>http://undocumentedmatlab.com/blog/udd-properties/</link> <comments>http://undocumentedmatlab.com/blog/udd-properties/#comments</comments> <pubDate>Wed, 09 Mar 2011 18:00:17 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[Hidden property]]></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[Pure Matlab]]></category> <category><![CDATA[schema]]></category> <category><![CDATA[schema.prop]]></category> <category><![CDATA[Undocumented property]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2156</guid> <description><![CDATA[UDD provides a very convenient way to add customizable properties to existing Matlab object handles<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/' rel='bookmark' title='getundoc &#8211; get undocumented object properties'>getundoc &#8211; get undocumented object properties</a> <small>getundoc is a very simple utility that displays the hidden (undocumented) properties of a specified handle object....</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-liminclude-properties/' rel='bookmark' title='Plot LimInclude properties'>Plot LimInclude properties</a> <small>The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....</small></li><li><a
href='http://undocumentedmatlab.com/blog/displaying-hidden-handle-properties/' rel='bookmark' title='Displaying hidden handle properties'>Displaying hidden handle properties</a> <small>I present two ways of checking undocumented hidden properties in Matlab Handle Graphics (HG) handles...</small></li><li><a
href='http://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties/' rel='bookmark' title='Borderless button used for plot properties'>Borderless button used for plot properties</a> <small>A borderless button can be used to add unobtrusive functionality to plot axes...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Donn Shull continues his series of articles on Matlab&#8217;s undocumented UDD mechanism. Today, Donn explains how to use and customize UDD properties.</i></p><h3 id="meta-data">Properties meta-data</h3><p>The UDD system is a class system. UDD packages, classes, events, and properties are all classes. In this section we will take a closer look at property classes.</p><p>As we have <a
target="_blank" href="http://undocumentedmatlab.com/blog/creating-a-simple-udd-class/">already shown</a>, properties are added to a UDD class by adding <code>schema.prop</code> calls to the schema.m class definition file. What this really means is that each property of a UDD class is itself a class object (<code>schema.prop</code>) with its own properties and methods. The methods of <code>schema.prop</code> are <i>loadobj()</i> and <i>saveobj()</i>, which are used to serialize objects of this class (i.e., storing them in a file or sending them elsewhere).</p><p>It is <code>schema.prop</code>&#8216;s properties (so-called <i>meta-properties</i>) that interest us most:</p><table><tr><th
bgcolor="#D0D0D0">Property</th><th
bgcolor="#D0D0D0">Data Type</th><th
bgcolor="#D0D0D0">Description</th></tr><tr><td
bgcolor="#E7E7E7">AccessFlags</td><td
bgcolor="#E7E7E7">Matlab structure</td><td
bgcolor="#E7E7E7">Controls which objects can access (read/modify) the property</td></tr><tr><td
bgcolor="#E7E7E7">CaseSensitive</td><td
bgcolor="#E7E7E7">on/off</td><td
bgcolor="#E7E7E7">Determines if the exact case is required to access the property (i.e., can we use &#8216;casesensitive&#8217; instead of &#8216;CaseSensitive&#8217;)</td></tr><tr><td
bgcolor="#E7E7E7">DataType</td><td
bgcolor="#E7E7E7">string</td><td
bgcolor="#E7E7E7">The underlying object&#8217;s property data type, set by the constructor</td></tr><tr><td
bgcolor="#E7E7E7">Description</td><td
bgcolor="#E7E7E7">string</td><td
bgcolor="#E7E7E7">This can hold a description of the property (normally empty)</td></tr><tr><td
bgcolor="#E7E7E7">FactoryValue</td><td
bgcolor="#E7E7E7">As specified by DataType</td><td
bgcolor="#E7E7E7">This is used to provide an initial or default property value</td></tr><tr><td
bgcolor="#E7E7E7">GetFunction</td><td
bgcolor="#E7E7E7">Function handle</td><td
bgcolor="#E7E7E7">A function handle that is called whenever the property value is read</td></tr><tr><td
bgcolor="#E7E7E7">Name</td><td
bgcolor="#E7E7E7">string</td><td
bgcolor="#E7E7E7">The name of the property, also set by the constructor</td></tr><tr><td
bgcolor="#E7E7E7">SetFunction</td><td
bgcolor="#E7E7E7">Function handle</td><td
bgcolor="#E7E7E7">A function handle that is called whenever the properties value is changed</td></tr><tr><td
bgcolor="#E7E7E7">Visible</td><td
bgcolor="#E7E7E7">on/off</td><td
bgcolor="#E7E7E7">Determines if a property will be displayed by the <i><b>get</b></i> method for a UDD object</td></tr></table><p>We can manipulate the values of these meta-properties to control various aspects of our property:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Create instance of simple.object</span>
&gt;&gt; a = simple.<span style="">object</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'a'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Find the Value property and list its meta-properties</span>
<span style="color: #228B22;">% We can manipulate these meta-properties within limits</span>
&gt;&gt; a.<span style="color: #0000FF;">findprop</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Value'</span><span style="color: #080;">&#41;</span>.<span style="color: #0000FF;">get</span>
            Name<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'Value'</span>
     Description<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
        DataType<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'single'</span>
    FactoryValue<span style="color: #F0F;">:</span> <span style="color: #33f;">7.3891</span>
     AccessFlags<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 <span style="color: #0000FF;">struct</span><span style="color: #080;">&#93;</span>
         Visible<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
     GetFunction<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
     SetFunction<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; prop.<span style="">Visible</span> = <span style="color:#A020F0;">'off'</span>;  <span style="color: #228B22;">% i.e. hidden property (see below)</span>
&gt;&gt; prop.<span style="">AccessFlags</span>.<span style="">PublicSet</span> = <span style="color:#A020F0;">'off'</span>;   <span style="color: #228B22;">% i.e. read-only</span>
&gt;&gt; prop.<span style="">AccessFlags</span>.<span style="">PublicGet</span> = <span style="color:#A020F0;">'on'</span>;
&nbsp;
<span style="color: #228B22;">% Find the DataType meta-property of the Value property</span>
<span style="color: #228B22;">% This meta-property and all other schema.prop base class properties are fixed</span>
&gt;&gt; a.<span style="color: #0000FF;">findprop</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Value'</span><span style="color: #080;">&#41;</span>.<span style="color: #0000FF;">findprop</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'DataType'</span><span style="color: #080;">&#41;</span>.<span style="color: #0000FF;">get</span>
            Name<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'DataType'</span>
     Description<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
        DataType<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'string'</span>
    FactoryValue<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
           <span style="color: #F0F;">...</span></pre></div></div><h3 id="new-prop">Adding properties to existing objects in run-time</h3><p><code>schema.prop</code> is a very useful tool &#8211; it can be used to add new properties to existing object handles, even after these objects have been created. For example, let&#8217;s add a new property (<b>MyFavoriteBlog</b>) to a standard figure handle:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; p=schema.<span style="">prop</span><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:#A020F0;">'MyFavoriteBlog'</span>,<span style="color:#A020F0;">'string'</span><span style="color: #080;">&#41;</span>
p =
	schema.<span style="">prop</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>,<span style="color:#A020F0;">'MyFavoriteBlog'</span>,<span style="color:#A020F0;">'UndocumentedMatlab.com'</span><span style="color: #080;">&#41;</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>,<span style="color:#A020F0;">'MyFavoriteBlog'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
UndocumentedMatlab.<span style="">com</span></pre></div></div><p>Using this simple mechanism, we can add meaningful typed user data to any handle object. A similar functionality can be achieved via the <i><b>setappdata/getappdata</b></i> functions. However, the property-based approach above is much &#8220;cleaner&#8221; and more powerful, since we have built-in type checks, property-change event listeners and other useful goodies.</p><h3 id="DataType">Property data types</h3><p>In the article on <a
target="_blank" href="http://undocumentedmatlab.com/blog/creating-a-simple-udd-class/">creating UDD objects</a> we saw that the <b>Name</b> and <b>DataType</b> meta-properties are set by the <code>schema.prop</code> constructor. <b>Name</b> must be a valid Matlab variable name (see <i><b>isvarname</b></i>).</p><p><b>DataType</b> is more interesting: There are two equivalent universal data types, <code>'mxArray'</code>, and <code>'MATLAB array'</code>. With either of these two data types a property can be set to a any Matlab type. If we use a more specific data type (e.g., &#8216;string&#8217;, &#8216;double&#8217; or &#8216;handle&#8217;), Matlab automatically ensures the type validity whenever the property value is modified. In our <code>simple.object</code> we use &#8216;double&#8217; and &#8216;string&#8217;. You can experiment with these and see that the <b>Value</b> property will only allow scalar numeric values and the <b>Name</b> property will only allow character values:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>obj, <span style="color:#A020F0;">'Value'</span>, <span style="color:#A020F0;">'abcd'</span><span style="color: #080;">&#41;</span>
??? Parameter must be scalar.
&nbsp;
&gt;&gt; obj.<span style="">Value</span>=<span style="color:#A020F0;">'abcd'</span>
??? Parameter must be scalar.
&nbsp;
&gt;&gt; obj.<span style="">Name</span>=<span style="color: #33f;">123</span>
??? Parameter must be a string.</pre></div></div><p>The following table lists the basic UDD data types:</p><table><tr><th
bgcolor="#D0D0D0">Category</th><th
bgcolor="#D0D0D0">Data Type</th></tr><tr><td
bgcolor="#E7E7E7">Universal</td><td
bgcolor="#E7E7E7">MATLAB array, mxArray</td></tr><tr><td
bgcolor="#E7E7E7">Numeric Scalars</td><td
bgcolor="#E7E7E7">bool, byte, short, int, long, float, double</td></tr><tr><td
bgcolor="#E7E7E7">Numeric Vectors</td><td
bgcolor="#E7E7E7">Nints, NReals</td></tr><tr><td
bgcolor="#E7E7E7">Specialized Numeric</td><td
bgcolor="#E7E7E7">color, point, real point, real point3, rect, real rect</td></tr><tr><td
bgcolor="#E7E7E7">Enumeration</td><td
bgcolor="#E7E7E7">on/off</td></tr><tr><td
bgcolor="#E7E7E7">Strings</td><td
bgcolor="#E7E7E7">char, string, NStrings, string vector</td></tr><tr><td
bgcolor="#E7E7E7">Handle</td><td
bgcolor="#E7E7E7">handle, handle vector, MATLAB callback, GetFunction, SetFunction</td></tr><tr><td
bgcolor="#E7E7E7">Java</td><td
bgcolor="#E7E7E7">Any java class recognized by Matlab</td></tr></table><h3 id="user-types">User-defined data types</h3><p>While this is an extensive list, there are some obvious types missing. For example there are no unsigned integer types. To handle this UDD provides two facilities for creating your own data types. One is the <code>schema.EnumType</code>. As you can see, Matlab has had a form of enumerations for a really long time not just the last few releases. The other facility is <code>schema.UserType</code>.</p><p>With these two classes you can create any specialized data type you need. One word of caution: once you have created a new UDD data type it exists for the duration of that Matlab session. There is no equivalent of the <i><b>clear classes</b></i> mechanism for removing a data type. In addition once a new data type has been defined it cannot be redefined until Matlab is restarted.</p><p>Let&#8217;s use a <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/280282">problem discussed in the CSSM forum</a> as example. The essence of the problem is the need to flag a graphic line object as either editable or not. The proposed proposed is to add a new <b>Editable</b> property to an existing line handle. We will use <code>schema.EnumType</code> to create a new type named <code>'yes/no'</code> so that the new property could accept only &#8216;yes&#8217; and &#8216;no&#8217; values:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> tline = taggedLine<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">%TAGGEDLINE create a line with Editable property</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   TLINE = TAGGEDLINE(VARARGIN) create a new handle graphics line</span>
<span style="color: #228B22;">%   and add 'Ediatable' property to line. Default property value is 'yes'.</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   INPUTS:</span>
<span style="color: #228B22;">%       VARARGIN  : property value pairs to pass to line</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   OUTPUTS:</span>
<span style="color: #228B22;">%       TLINE     : hg line object with Editable property</span>
&nbsp;
    <span style="color: #228B22;">% If undefined define yes/no datatype&lt;/font&gt;</span>
    <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>findtype<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'yes/no'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
        schema.<span style="">EnumType</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'yes/no'</span>, <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'yes'</span>, <span style="color:#A020F0;">'no'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span>
    tline = <span style="color: #0000FF;">line</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#123;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
    schema.<span style="">prop</span><span style="color: #080;">&#40;</span>tline, <span style="color:#A020F0;">'Editable'</span>, <span style="color:#A020F0;">'yes/no'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>It is necessary to test for the existence of a type before defining it, since trying to redefine a type will generate an error.</p><p>We can use this new <i>taggedLine()</i> function to create new line objects with the additional <b>Editable</b> property. Instead of adding a new property to the line class we could have defined a new class as a subclass of line:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> schema<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">%SCHEMA  hg package definition function</span>
    schema.<span style="">package</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'hg'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>We create our class definition as a subclass of the handle graphics line class:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> schema<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">%SCHEMA  hg.taggedline class definition function</span>
    <span style="color: #228B22;">% package definition</span>
    superPackage = findpackage<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'hg'</span><span style="color: #080;">&#41;</span>;
    pkg = findpackage<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'hg'</span><span style="color: #080;">&#41;</span>;
&nbsp;
    <span style="color: #228B22;">% class definition</span>
    c = schema.<span style="color: #0000FF;">class</span><span style="color: #080;">&#40;</span>pkg, <span style="color:#A020F0;">'taggedline'</span>, findclass<span style="color: #080;">&#40;</span>superPackage, <span style="color:#A020F0;">'line'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
    <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>findtype<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'yes/no'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
        schema.<span style="">EnumType</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'yes/no'</span>, <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'yes'</span>, <span style="color:#A020F0;">'no'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span>
&nbsp;
    <span style="color: #228B22;">% add properties to class</span>
    schema.<span style="">prop</span><span style="color: #080;">&#40;</span>c, <span style="color:#A020F0;">'Editable'</span>, <span style="color:#A020F0;">'yes/no'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>And our constructor is:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> self = taggedline
<span style="color: #228B22;">%OBJECT constructor for the simple.object class</span>
    self = hg.<span style="">taggedline</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>Here we have placed the <code>schema.EnumType</code> definition in the class definition function. It is usually better to place type definition code in the package definition function, which is executed prior to any of the package classes and available in all classes. But in this particular case we are extending the built-in <code>hg</code> package and because <code>hg</code> is already defined internally, our package definition code is never actually executed.</p><p>The <code>schema.UserType</code> has the following constructor syntax:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">schema.<span style="">UserType</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'newTypeName'</span>, <span style="color:#A020F0;">'baseTypeName'</span>, typeCheckFunctionHandle<span style="color: #080;">&#41;</span></pre></div></div><p>For example, to create a user-defined type for unsigned eight-bit integers we might use the following code:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">schema.<span style="">UserType</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'uint8'</span>, <span style="color:#A020F0;">'short'</span>, @check_uint8<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">function</span> check_uint8<span style="color: #080;">&#40;</span>value<span style="color: #080;">&#41;</span>
<span style="color: #228B22;">%CHECK_UINT8 Check function for uint8 type definition</span>
    <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>value<span style="color: #080;">&#41;</span> <span style="color: #F0F;">||</span> <span style="color: #080;">&#40;</span>value &lt; <span style="color: #33f;">0</span><span style="color: #080;">&#41;</span> <span style="color: #F0F;">||</span> <span style="color: #080;">&#40;</span>value &gt; <span style="color: #33f;">255</span><span style="color: #080;">&#41;</span>
        <span style="color: #0000FF;">error</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Value must be a scalar between 0 and 255'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span></pre></div></div><h3 id="hidden">Hidden properties</h3><p><b>Visible</b> is an <code>'on/off'</code> meta-property that controls whether or not a property is displayed when using the <i><b>get</b></i> function without specifying the property name. Using this mechanism we can easily detect hidden undocumented properties. For example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">for</span> prop = <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;">'Properties'</span><span style="color: #080;">&#41;</span>'
       <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">strcmpi</span><span style="color: #080;">&#40;</span>prop.<span style="">Visible</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>, <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span>prop.<span style="">Name</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">end</span>
   <span style="color: #0000FF;">end</span>
&nbsp;
BackingStore
CurrentKey
CurrentModifier
Dithermap
DithermapMode
DoubleBuffer
FixedColors
HelpFcn
HelpTopicMap
MinColormap
JavaFrame
OuterPosition
ActivePositionProperty
PrintTemplate
ExportTemplate
WaitStatus
UseHG2
PixelBounds
HelpTopicKey
Serializable
ApplicationData
Behavior
XLimInclude
YLimInclude
ZLimInclude
CLimInclude
ALimInclude
IncludeRenderer</pre></div></div><p>Note that hidden properties such as these are accessible via <i><b>get/set</b></i> just as any other property. It is simply that they are not displayed when you run <i><b>get(gcf)</b></i> or <i><b>set(gcf)</b></i> &#8211; we need to specifically refer to them by their name: <i><b>get(gcf</b>,&#8217;UseHG2&#8242;)</i>. Many other similar hidden properties are <a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/hidden-property/">described in this website</a>.</p><p>You may have noticed that the <b>CaseSensitive</b> meta-property did not show up above when we used <i><b>get</b></i> to show the meta-properties of our <b>Value</b> property. This is because <b>CaseSensitive</b> has its own <b>Visible</b> meta-property set to <code>'off'</code> (i.e., hidden).</p><h3 id="additional">Additional meta-properties</h3><p><b>FactoryValue</b> is used to set an initial value for the property whenever a new <code>simple.object</code> instance is created.</p><p><b>GetFunction</b> and <b>SetFunction</b> were described in last week&#8217;s article, <a
href="http://undocumentedmatlab.com/blog/hierarchical-systems-with-udd/">Creating a UDD Hierarchy</a>.</p><p><b>AccessFlags</b> is a Matlab structure of <code>'on/off'</code> fields that control what happens when the property is accessed:</p><table><tr><th
bgcolor="#D0D0D0">Fieldname</th><th
bgcolor="#D0D0D0">Description</th></tr><tr><td
bgcolor="#E7E7E7">PublicSet</td><td
bgcolor="#E7E7E7">Controls setting the property from code external to the class</td></tr><tr><td
bgcolor="#E7E7E7">PublicGet</td><td
bgcolor="#E7E7E7">Controls reading the property value from code external to the class</td></tr><tr><td
bgcolor="#E7E7E7">PrivateSet</td><td
bgcolor="#E7E7E7">Controls setting the property from internal class methods</td></tr><tr><td
bgcolor="#E7E7E7">PrivateGet</td><td
bgcolor="#E7E7E7">Controls reading the property value from internal class methods</td></tr><tr><td
bgcolor="#E7E7E7">Init</td><td
bgcolor="#E7E7E7">Controls initializing the property using <b>FactoryValue</b> in the class definition file</td></tr><tr><td
bgcolor="#E7E7E7">Default</td><td
bgcolor="#E7E7E7">??? (Undocumented, no examples exist)</td></tr><tr><td
bgcolor="#E7E7E7">Reset</td><td
bgcolor="#E7E7E7">Controls initializing the property using <b>FactoryValue</b> when executing the built-in <i><b>reset</b></i> function</td></tr><tr><td
bgcolor="#E7E7E7">Serialize</td><td
bgcolor="#E7E7E7">Controls whether this object can be serialized</td></tr><tr><td
bgcolor="#E7E7E7">Copy</td><td
bgcolor="#E7E7E7">Controls whether to pass the property&#8217;s current value to a copy</td></tr><tr><td
bgcolor="#E7E7E7">Listener</td><td
bgcolor="#E7E7E7">Controls whether property access events are generated or not</td></tr><tr><td
bgcolor="#E7E7E7">AbortSet</td><td
bgcolor="#E7E7E7">Controls whether property set events are generated when a <i><b>set</b></i> operation will not change the property&#8217;s value</td></tr></table><p>The <b>CaseSensitive</b> meta-property has <b>AccessFlag.Init</b> = <code>'off'</code>. This means that properties added to a class definition file are always case insensitive.</p><p>Another interesting fact is that properties can be abbreviated as long as the abbreviation is unambiguous. Using our <code>simple.object</code> as an example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; a = simple.<span style="">object</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'a'</span><span style="color: #080;">&#41;</span>;
&gt;&gt; a.<span style="">n</span>  <span style="color: #228B22;">% abbreviation of Name</span>
<span style="color: #0000FF;">ans</span> =
a
&nbsp;
&gt;&gt; a.<span style="">v</span>  <span style="color: #228B22;">% abbreviation of Value</span>
<span style="color: #0000FF;">ans</span> =
    <span style="color: #33f;">0.0000</span></pre></div></div><p>It is considered poor programming practice to use either improperly cased, or abbreviated names when writing code. It is difficult to read, debug and maintain. But show me a Matlab programmer who has never abbreviated <b>Position</b> as &#8216;pos&#8217;&#8230;</p><p><i>Note: for completeness&#8217; sake, read yesterday&#8217;s post on <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/loren/2011/03/08/common-design-considerations-for-object-properties/">MCOS properties</a> on Loren&#8217;s blog, written by Dave Foti, author of the original UDD code. Dave&#8217;s post describes the fully-documented MCOS mechanism, which is newer than the undocumented UDD mechanism described here. As mentioned earlier, whereas UDD existed (and still exists) in all Matlab 7 releases, MCOS is only available since R2008a. UDD and MCOS co-exist in Matlab since R2008a. MCOS has definite advantages over UDD, but cannot be used on pre-2008 Matlab releases. Different development and deployment requirements may dictate using either UDD or MCOS (or both). Another pre-R2008a alternative is to use Matlab&#8217;s obsolete yet documented <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/pdf_doc/matlab/pre-version_7.6_oop.pdf">class system</a>.</i></p><p>In the next installment of this series we will take a look at UDD events and listeners.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/' rel='bookmark' title='getundoc &#8211; get undocumented object properties'>getundoc &#8211; get undocumented object properties</a> <small>getundoc is a very simple utility that displays the hidden (undocumented) properties of a specified handle object....</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-liminclude-properties/' rel='bookmark' title='Plot LimInclude properties'>Plot LimInclude properties</a> <small>The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....</small></li><li><a
href='http://undocumentedmatlab.com/blog/displaying-hidden-handle-properties/' rel='bookmark' title='Displaying hidden handle properties'>Displaying hidden handle properties</a> <small>I present two ways of checking undocumented hidden properties in Matlab Handle Graphics (HG) handles...</small></li><li><a
href='http://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties/' rel='bookmark' title='Borderless button used for plot properties'>Borderless button used for plot properties</a> <small>A borderless button can be used to add unobtrusive functionality to plot axes...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/udd-properties/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Hierarchical Systems with UDD</title><link>http://undocumentedmatlab.com/blog/hierarchical-systems-with-udd/</link> <comments>http://undocumentedmatlab.com/blog/hierarchical-systems-with-udd/#comments</comments> <pubDate>Wed, 02 Mar 2011 18:00:25 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[Handle graphics]]></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[JMI]]></category> <category><![CDATA[schema]]></category> <category><![CDATA[schema.prop]]></category> <category><![CDATA[uitools]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2146</guid> <description><![CDATA[UDD objects can be grouped in structured hierarchies - this article explains how<pre> </pre>Related posts:<ol><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><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/udd-properties/' rel='bookmark' title='UDD Properties'>UDD Properties</a> <small>UDD provides a very convenient way to add customizable properties to existing Matlab object handles...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Once again I welcome guest blogger <a
target="_blank" rel="nofollow" href="http://aetoolbox.com/">Donn Shull</a>, who continues his multi-part series about Matlab’s undocumented UDD objects.</i></p><p>We have looked at the <a
target="_blank" href="http://undocumentedmatlab.com/blog/introduction-to-udd/">tools for working with UDD classes</a>, and <a
target="_blank" href="http://undocumentedmatlab.com/blog/creating-a-simple-udd-class/">created a simple UDD class</a>. Today I shall show how to create a hierarchy of UDD objects.</p><h3 id="hierarchy">Creating hierarchical structures with UDD objects</h3><p>UDD is the foundation for both Handle Graphics (HG) and Simulink. Both are hierarchical systems. It stands to reason that UDD would offer support for hierarchical structures. It is straightforward to connect UDD objects together into searchable tree structures. All that is necessary is a collection of UDD objects that don&#8217;t have any methods or properties named <code>'connect', 'disconnect', 'up', 'down', 'left', 'right'</code> or <code>'find'</code>.</p><p>We illustrate the technique by creating a hierarchy of <code>simple.object</code>s as shown in the following diagram:</p><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Sample UDD objects hierarchy" src="http://UndocumentedMatlab.com/images/UDD_structure.png" title="Sample UDD objects hierarchy" width="200" height="177" /><p
class="wp-caption-text">Sample UDD objects hierarchy</p></div></center></p><p>To begin we create five instances of the <code>simple.object</code> class from the previous article:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Remember that simple.object accepts a name and a value</span>
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>;
b = simple.<span style="">object</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'b'</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
c = simple.<span style="">object</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'c'</span>, <span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
d = simple.<span style="">object</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'d'</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
e = simple.<span style="">object</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'e'</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>To form the structure we use the <i>connect</i> method. We can use either dot notation or the Matlab syntax:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Dot-notation examples:</span>
a.<span style="">connect</span><span style="color: #080;">&#40;</span>b, <span style="color:#A020F0;">'down'</span><span style="color: #080;">&#41;</span>;
b.<span style="">connect</span><span style="color: #080;">&#40;</span>a, <span style="color:#A020F0;">'up'</span><span style="color: #080;">&#41;</span>;       <span style="color: #228B22;">% alternative to the above</span>
&nbsp;
<span style="color: #228B22;">% Matlab notation examples:</span>
connect<span style="color: #080;">&#40;</span>a, b, <span style="color:#A020F0;">'down'</span><span style="color: #080;">&#41;</span>;
connect<span style="color: #080;">&#40;</span>b, a, <span style="color:#A020F0;">'up'</span><span style="color: #080;">&#41;</span>;      <span style="color: #228B22;">% alternative to the above</span></pre></div></div><p>Next, connect node c into our hierarchy. There are several options here: We can use &#8216;down&#8217; to connect a to c. Or we could use &#8216;up&#8217; to connect c to a. Similarly, we can use either &#8216;left&#8217; or &#8216;right&#8217; to connect b and c. Here&#8217;s one of the many possible ways to create our entire hierarchy:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">b.<span style="">connect</span><span style="color: #080;">&#40;</span>a, <span style="color:#A020F0;">'up'</span><span style="color: #080;">&#41;</span>;
c.<span style="">connect</span><span style="color: #080;">&#40;</span>b, <span style="color:#A020F0;">'left'</span><span style="color: #080;">&#41;</span>;
d.<span style="">connect</span><span style="color: #080;">&#40;</span>b, <span style="color:#A020F0;">'up'</span><span style="color: #080;">&#41;</span>;
e.<span style="">connect</span><span style="color: #080;">&#40;</span>d, <span style="color:#A020F0;">'left'</span><span style="color: #080;">&#41;</span>;</pre></div></div><h3 id="inspecting">Inspecting UDD hierarchy structures</h3><p>We now have our structure and each object knows its connection to other objects. For example, we can inspect b&#8217;s connections as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; b.<span style="">up</span>
<span style="color: #0000FF;">ans</span> =
  Name<span style="color: #F0F;">:</span> a
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">1.000000</span>
&nbsp;
&gt;&gt; b.<span style="">right</span>
<span style="color: #0000FF;">ans</span> =
  Name<span style="color: #F0F;">:</span> c
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">0.000000</span>
&nbsp;
&gt;&gt; b.<span style="">down</span>
<span style="color: #0000FF;">ans</span> =
  Name<span style="color: #F0F;">:</span> d
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">1.000000</span></pre></div></div><p>We can search our structure by using an undocumented form of the built-in <i><b>find</b></i> command. When used with connected UDD structures, <i><b>find</b></i> can be used in the following form:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">objectArray = <span style="color: #0000FF;">find</span><span style="color: #080;">&#40;</span>startingNode, <span style="color:#A020F0;">'property'</span>, <span style="color:#A020F0;">'value'</span>, <span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span></pre></div></div><p>To search from the top of our hierarchy for objects of type <code>simple.object</code> we would use:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">find</span><span style="color: #080;">&#40;</span>a, <span style="color:#A020F0;">'-isa'</span>, <span style="color:#A020F0;">'simple.object'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
        simple.<span style="">object</span><span style="color: #F0F;">:</span> <span style="color: #33f;">5</span>-by-<span style="color: #33f;">1</span>    <span style="color: #228B22;">% a, b, c, d, e</span></pre></div></div><p>Which returns all the objects in our structure, since all of them are <code>simple.object</code>s. If we repeat that command starting at b we would get:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">find</span><span style="color: #080;">&#40;</span>b, <span style="color:#A020F0;">'-isa'</span>, <span style="color:#A020F0;">'simple.object'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
	simple.<span style="">object</span><span style="color: #F0F;">:</span> <span style="color: #33f;">3</span>-by-<span style="color: #33f;">1</span>    <span style="color: #228B22;">% b, d, e</span></pre></div></div><p><i><b>find</b></i> searches the structure downward from the current node. Like many Matlab functions, <i><b>find</b></i> can be used with multiple property value pairs, so if we want to find <code>simple.object</code> objects in our structure with <b>Value</b> property =0, we would use the command:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">find</span><span style="color: #080;">&#40;</span>a, <span style="color:#A020F0;">'-isa'</span>, <span style="color:#A020F0;">'simple.object'</span>, <span style="color:#A020F0;">'Value'</span>, <span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
  Name<span style="color: #F0F;">:</span> c
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">0.000000</span></pre></div></div><h3 id="visualizing">Visualizing a UDD hierarchy</h3><p>Hierarchical structures are also known as tree structures. Matlab has an undocumented function for visualizing and working with trees namely <i><b>uitree</b></i>. Yair has described <i><b>uitree</b></i> in a <a
target="_blank" href="http://UndocumentedMatlab.com/blog/uitree/">series of articles</a>. Rather than following the techniques in shown in Yair&#8217;s articles, we are going to use a different method that will allow us to introduce the following important techniques for working with UDD objects:</p><ul><li>Subclassing, building your class on the foundation of a parent class</li><li>Overloading properties and methods of the superclass</li><li>Using meta-properties <b>GetfFunction</b> and <b>SetFunction</b></li></ul><p>Because the steps shown below will subclass an HG class, they will modify our <code>simple.object</code> class and probably make it unsuitable for general use. Yair has shown that <i><b>uitree</b></i> is ready made for displaying HG trees and we saw above that HG is a UDD system. We will use the technique from <code>uitools.uibuttongroup</code> to make our <code>simple.object</code> class a subclass of the HG class <code>hg.uipanel</code>. Modify the class definition file as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% class definition</span>
superPackage = findpackage<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'hg'</span><span style="color: #080;">&#41;</span>;
superClass = findclass<span style="color: #080;">&#40;</span>superPackage, <span style="color:#A020F0;">'uipanel'</span><span style="color: #080;">&#41;</span>;
simpleClass = schema.<span style="color: #0000FF;">class</span><span style="color: #080;">&#40;</span>simplePackage, <span style="color:#A020F0;">'object'</span>,superClass<span style="color: #080;">&#41;</span>;</pre></div></div><p>Now we can either issue the <i><b>clear classes</b></i> command or restart Matlab and then recreate our structure. The first thing that you will notice is that when we create the first <code>simple.object</code> that a figure is also created. This is expected and is the reason that this technique is not useful in general. We will however use this figure to display our structure with the following commands:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">t = uitree<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color:#A020F0;">'root'</span>, a<span style="color: #080;">&#41;</span>;  <span style="color: #0000FF;">drawnow</span>;
t.<span style="">expand</span><span style="color: #080;">&#40;</span>t.<span style="">getRoot</span><span style="color: #080;">&#41;</span>;  <span style="color: #0000FF;">drawnow</span>;
t.<span style="">expand</span><span style="color: #080;">&#40;</span>t.<span style="">getRoot</span>.<span style="">getFirstChild</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 451px"><img
alt="Simple structure presented in a Matlab uitree" src="http://UndocumentedMatlab.com/images/UDD_uitree_1.png" title="Simple structure presented in a Matlab uitree" width="441" height="189" /><p
class="wp-caption-text">Simple structure presented in a Matlab <i><b>uitree</b></i></p></div></center></p><p>The label on each of our objects is &#8216;uipanel&#8217; and this is probably not what we want. If we inspect our object or its <code>hg.uipanel</code> super-class (note: this would be a great time to use Yair&#8217;s <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> utility</a>), we can see there is a <b>Type</b> property that has a value of &#8216;uipanel&#8217;. Unfortunately this property is read-only, so we cannot change it. We can however overload it by placing a <i><b>schema.prop</b></i> in our class definition named <b>Type</b>. This will allow us to overload or replace the parent&#8217;s <b>Type</b> property with our own definition:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">p = schema.<span style="">prop</span><span style="color: #080;">&#40;</span>simpleClass, <span style="color:#A020F0;">'Type'</span>, <span style="color:#A020F0;">'string'</span><span style="color: #080;">&#41;</span>;
p.<span style="">FactoryValue</span> = <span style="color:#A020F0;">'simple.object'</span>;</pre></div></div><p>Once again, issue the <i><b>clear classes</b></i> command or restart Matlab, then recreate our structure. Our tree now has each node labeled with the &#8216;simple.object&#8217; label:</p><p><center><div
class="wp-caption aligncenter" style="width: 451px"><img
alt="Corrected node names for our UDD structure" src="http://UndocumentedMatlab.com/images/UDD_uitree_2.png" title="Corrected node names for our UDD structure" width="441" height="189" /><p
class="wp-caption-text">Corrected node names for our UDD structure</p></div></center></p><p>This is a little more descriptive but what would really be nice is if we could label each node with the value of the <b>Name</b> property. As luck would have it, we can do just that. When we add a property to a UDD class we are adding an object of type <code>schema.prop</code>. So our properties have their own properties and methods (so-called <i>meta-data</i>). We are going to set the <b>GetFunction</b> property of our <b>Type</b> property. <b>GetFunction</b> holds a handle of the function to be called whenever the property is accessed:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">p = schema.<span style="">prop</span><span style="color: #080;">&#40;</span>simpleClass, <span style="color:#A020F0;">'Type'</span>, <span style="color:#A020F0;">'string'</span><span style="color: #080;">&#41;</span>;
p.<span style="">GetFunction</span> = @getType;</pre></div></div><p>The prototype for the function that <b>GetFunction</b> references has three inputs and one output: The inputs are the handle of the object possessing the property, the value of that property, and the property object. The output is the value that will be supplied when the property is accessed. So our <b>GetFunction</b> can be written to supply the value of the <b>Name</b> property whenever the <b>Type</b> property value is being read:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> propVal = getType<span style="color: #080;">&#40;</span>self, value, prop<span style="color: #080;">&#41;</span>
   propVal = self.<span style="">Name</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>Alternately, as a single one-liner in the schema definition file:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">p.<span style="">GetFunction</span> = @<span style="color: #080;">&#40;</span>self,value,prop<span style="color: #080;">&#41;</span> self.<span style="">Name</span>;</pre></div></div><p>Similarly, there is a corresponding <b>SetFunction</b> that enables us to intercept changes to a property&#8217;s value and possibly disallow invalid values.</p><p>With these changes when we recreate our <i><b>uitree</b></i> we obtain:</p><p><center><div
class="wp-caption aligncenter" style="width: 451px"><img
alt="Overloaded property GetFunction" src="http://UndocumentedMatlab.com/images/UDD_uitree_3.png" title="Overloaded property GetFunction" width="441" height="189" /><p
class="wp-caption-text">Overloaded property <b>GetFunction</b></p></div></center></p><h3 id="java">A Java class for UDD trees</h3><p>We will have more to say about the relationship between UDD and Java in a future article. For now we simply note that the <code>com.mathworks.jmi.bean.UDDObjectTreeModel</code> class in the <a
target="_blank" href="http://UndocumentedMatlab.com/blog/jmi-java-to-matlab-interface/">JMI package</a> provides some UDD tree navigation helper functions. Methods include <i>getChild, getChildCount, getIndexOfChild</i> and <i>getPathToRoot</i>. The <code>UDDObjectTreeModel</code> constructor requires one argument, an instance of your UDD tree root node:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Create a UDD tree-model instance</span>
&gt;&gt; uddTreeModel = com.<span style="">mathworks</span>.<span style="">jmi</span>.<span style="">bean</span>.<span style="">UDDObjectTreeModel</span><span style="color: #080;">&#40;</span>a<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Get index of child e and its parent b:</span>
&gt;&gt; childIndex = uddTreeModel.<span style="">getIndexOfChild</span><span style="color: #080;">&#40;</span>b, e<span style="color: #080;">&#41;</span>
childIndex =
     <span style="color: #33f;">1</span>
&nbsp;
<span style="color: #228B22;">% Get the root's first child (#0):</span>
&gt;&gt; child0 = uddTreeModel.<span style="">getChild</span><span style="color: #080;">&#40;</span>a, <span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>
child0 =
  Name<span style="color: #F0F;">:</span> b
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">1.000000</span>
&nbsp;
<span style="color: #228B22;">% Get the path from node e to the root:</span>
&gt;&gt; path2root = uddTreeModel.<span style="">getPathToRoot</span><span style="color: #080;">&#40;</span>e<span style="color: #080;">&#41;</span>
path2root =
com.<span style="">mathworks</span>.<span style="">jmi</span>.<span style="">bean</span>.<span style="">UDDObject</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #F0F;">:</span>
    <span style="color: #080;">&#91;</span>simple_objectBeanAdapter2<span style="color: #080;">&#93;</span>      <span style="color: #228B22;">% &lt;= a</span>
    <span style="color: #080;">&#91;</span>simple_objectBeanAdapter2<span style="color: #080;">&#93;</span>      <span style="color: #228B22;">% &lt;= b</span>
    <span style="color: #080;">&#91;</span>simple_objectBeanAdapter2<span style="color: #080;">&#93;</span>      <span style="color: #228B22;">% &lt;= e</span>
&nbsp;
&gt;&gt; path2root<span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
  Name<span style="color: #F0F;">:</span> e
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">1.000000</span></pre></div></div><p>We touched on a few of the things that you can do by modifying the properties of a <code>schema.prop</code> in this article. In the following article we will take a more detailed look at this essential class.</p><p><pre> </pre>Related posts:<ol><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><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/udd-properties/' rel='bookmark' title='UDD Properties'>UDD Properties</a> <small>UDD provides a very convenient way to add customizable properties to existing Matlab object handles...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/hierarchical-systems-with-udd/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Creating a simple UDD class</title><link>http://undocumentedmatlab.com/blog/creating-a-simple-udd-class/</link> <comments>http://undocumentedmatlab.com/blog/creating-a-simple-udd-class/#comments</comments> <pubDate>Wed, 23 Feb 2011 17:29:05 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Donn Shull]]></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=2130</guid> <description><![CDATA[This article explains how to create and test custom UDD packages, classes and objects<pre> </pre>Related posts:<ol><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/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/udd-properties/' rel='bookmark' title='UDD Properties'>UDD Properties</a> <small>UDD provides a very convenient way to add customizable properties to existing Matlab object handles...</small></li><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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Once again I welcome guest blogger <a
target="_blank" rel="nofollow" href="http://aetoolbox.com/">Donn Shull</a>, who continues his multi-part series about Matlab&#8217;s undocumented UDD objects.</i></p><h3 id="package">Creating a new UDD package</h3><p>To illustrate the construction of UDD classes with Matlab m-code, let&#8217;s create a simple class belonging to a new simple package. Our class will have two properties: a <b>Name</b> property of type string, and a <b>Value</b> property of type double. This class will have two methods that will illustrate overloading the built-in <i><b>disp</b></i> function, and using a <i>dialog</i> method to present a GUI. Our class will also have one event, to demonstrate UDD event handling.</p><p>To create this simple UDD class we need two directories and five m-files (downloadable <a
href="http://UndocumentedMatlab.com/files/simple.zip">here</a>): The parent directory needs to be a directory on the Matlab path. A subdirectory of the parent directory is named with the symbol @ followed by our UDD package name &#8211; this is the package directory. In this example, the subdirectory is called @simple.</p><p>Within the @simple directory, place a file named <i>schema.m</i>, which is the package definition file. This is a very simple file, that merely calls <i><b>schema.package</b></i> to create a new package called &#8216;simple&#8217;:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> schema<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">%SCHEMA simple package definition function.</span>
   schema.<span style="">package</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'simple'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>If you place additional m-files in the package directory they will be called package function files. Those files will have package scope and can be accessed with the notation <code>packagename.functionname</code>. We will not use package functions in this example, so we will only have the schema.m file shown above.</p><h3 id="class">Creating a new UDD class</h3><p>Next, create another subdirectory beneath @simple, named with an @ symbol followed by the UDD class name. In this example we will create the directory @object (i.e., /@simple/@object/). We place four m-files in this directory:</p><p>The first file is yet another schema.m file, which is the class-definition file:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> schema<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">%SCHEMA  simple.object class definition function.</span>
&nbsp;
   <span style="color: #228B22;">% Get a handle to the 'simple' package</span>
   simplePackage = findpackage<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'simple'</span><span style="color: #080;">&#41;</span>;
&nbsp;
   <span style="color: #228B22;">% Create a base UDD object</span>
   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>;
&nbsp;
   <span style="color: #228B22;">% Define the class methods:</span>
&nbsp;
   <span style="color: #228B22;">% dialog.m method</span>
   m = schema.<span style="">method</span><span style="color: #080;">&#40;</span>simpleClass, <span style="color:#A020F0;">'dialog'</span><span style="color: #080;">&#41;</span>;
   s = m.<span style="">Signature</span>;
   s.<span style="color: #0000FF;">varargin</span>    = <span style="color:#A020F0;">'off'</span>;
   s.<span style="">InputTypes</span>  = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'handle'</span><span style="color: #080;">&#125;</span>;
   s.<span style="">OutputTypes</span> = <span style="color: #080;">&#123;</span><span style="color: #080;">&#125;</span>;
&nbsp;
   <span style="color: #228B22;">% disp.m method</span>
   m = schema.<span style="">method</span><span style="color: #080;">&#40;</span>simpleClass, <span style="color:#A020F0;">'disp'</span><span style="color: #080;">&#41;</span>;
   s = m.<span style="">Signature</span>;
   s.<span style="color: #0000FF;">varargin</span>    = <span style="color:#A020F0;">'off'</span>;
   s.<span style="">InputTypes</span>  = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'handle'</span><span style="color: #080;">&#125;</span>;
   s.<span style="">OutputTypes</span> = <span style="color: #080;">&#123;</span><span style="color: #080;">&#125;</span>;
&nbsp;
   <span style="color: #228B22;">% Define the class properties:</span>
   schema.<span style="">prop</span><span style="color: #080;">&#40;</span>simpleClass, <span style="color:#A020F0;">'Name'</span>, <span style="color:#A020F0;">'string'</span><span style="color: #080;">&#41;</span>;
   schema.<span style="">prop</span><span style="color: #080;">&#40;</span>simpleClass, <span style="color:#A020F0;">'Value'</span>, <span style="color:#A020F0;">'double'</span><span style="color: #080;">&#41;</span>;
&nbsp;
   <span style="color: #228B22;">% Define the class events:</span>
   schema.<span style="">event</span><span style="color: #080;">&#40;</span>simpleClass, <span style="color:#A020F0;">'simpleEvent'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>Here, we used the built-in <i><b>findpackage</b></i> function to identify our base package (<code>simple</code>). Then we used <i><b>schema.class</b></i> to define a new class &#8216;object&#8217; within that base package. We next defined two class methods, two properties and finally an event.</p><h3 id="methods">Defining class methods</h3><p>It is not mandatory to define the method signatures as we have done in our class definition file. If you omit the method signature definitions, Matlab will automatically generate default signatures that will actually work in most applications. However, I believe that it is bad practice to omit the method signature definitions in a class definition file, and there are cases where your classes will not work as you have intended if you omit them.</p><p>Now, place a file named object.m in the @object directory. This file contains the class constructor method, which is executed whenever a new instance object of the <code>simple.object</code> class is created:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> simpleObject = object<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">%OBJECT constructor for the simple.object class</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   SIMPLEOBJECT = OBJECT(NAME, VALUE) creates an instance of the</span>
<span style="color: #228B22;">%   simple.object class with the Name property set to NAME and the</span>
<span style="color: #228B22;">%   Value property set VALUE</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   SIMPLEOBJECT = OBJECT(NAME) creates an instance of the simple.object</span>
<span style="color: #228B22;">%   class with the Name property set to NAME. The Value property will be</span>
<span style="color: #228B22;">%   given the default value of 0.</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   SIMPLEOBJECT = OBJECT creates an instance of the simple.object class</span>
<span style="color: #228B22;">%   and executes the simple.object dialog method to open a GUI for editing</span>
<span style="color: #228B22;">%   the Name and Value properties.</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   INPUTS:</span>
<span style="color: #228B22;">%       NAME          : string</span>
<span style="color: #228B22;">%       VALUE         : double</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   OUTPUTS:</span>
<span style="color: #228B22;">%       SIMPLEOBJECT  : simple.object instance</span>
   simpleObject = simple.<span style="">object</span>;
   <span style="color: #0000FF;">switch</span> <span style="color: #0000FF;">nargin</span>
      <span style="color: #0000FF;">case</span> <span style="color: #33f;">0</span>
         simpleObject.<span style="color: #0000FF;">dialog</span>;
      <span style="color: #0000FF;">case</span> <span style="color: #33f;">1</span>
         simpleObject.<span style="">Name</span> = name;
      <span style="color: #0000FF;">case</span> <span style="color: #33f;">2</span>
         simpleObject.<span style="">Name</span> = name;
         simpleObject.<span style="">Value</span> = value;
   <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span></pre></div></div><p>The two other m-files in the @object directory will be our class methods &#8211; a single file for each method. In our case they are disp.m and dialog.m:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span>self<span style="color: #080;">&#41;</span>
<span style="color: #228B22;">%DISP overloaded object disp method</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   DISP(SELF) or SELF.DISP uses the MATLAB builtin DISP function</span>
<span style="color: #228B22;">%   to display the Name and Value properties of the object.</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   INPUTS:</span>
<span style="color: #228B22;">%       SELF  : simple.object instance</span>
   <span style="color: #0000FF;">builtin</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'disp'</span>, <span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'  Name: %s\n Value: %f'</span>, self.<span style="">Name</span>, self.<span style="">Value</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
   <span style="color: #228B22;">%Alternative: fprintf('\n  Name: %s\n Value: %f', self.Name, self.Value));</span>
<span style="color: #0000FF;">end</span></pre></div></div><p>And the dialog method (in dialog.m):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> <span style="color: #0000FF;">dialog</span><span style="color: #080;">&#40;</span>self<span style="color: #080;">&#41;</span>
<span style="color: #228B22;">%DIALOG dialog method for simple.object for use by openvar</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   DIALOG(SELF) or SELF.DIALOG where self is the name of the simple.object</span>
<span style="color: #228B22;">%   instance opens a gui to edit the Name and Value properties of self.</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   INPUTS:</span>
<span style="color: #228B22;">%       SELF  : simple.object</span>
   dlgValues = <span style="color: #0000FF;">inputdlg</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#123;</span><span style="color:#A020F0;">'Name:'</span>, <span style="color:#A020F0;">'Value:'</span><span style="color: #080;">&#125;</span>, <span style="color:#A020F0;">'simple.object'</span>, <span style="color: #33f;">1</span>, <span style="color: #080;">&#123;</span>self.<span style="">Name</span>, <span style="color: #0000FF;">mat2str</span><span style="color: #080;">&#40;</span>self.<span style="">Value</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</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>dlgValues<span style="color: #080;">&#41;</span>
      self.<span style="">Name</span> = dlgValues<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span>;
      self.<span style="">Value</span> = <span style="color: #0000FF;">eval</span><span style="color: #080;">&#40;</span>dlgValues<span style="color: #080;">&#123;</span><span style="color: #33f;">2</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span></pre></div></div><h3 id="testing">Testing our new class</h3><p>Now let&#8217;s test our new class by creating an instance without using any input arguments</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">a = simple.<span style="">object</span></pre></div></div><p>This calls the object&#8217;s constructor method, which launches the input dialog GUI:<br
/><center><div
class="wp-caption aligncenter" style="width: 191px"><img
alt="UDD simple class GUI" src="http://UndocumentedMatlab.com/images/UDD_simple_object_1.jpg" title="UDD simple class GUI" width="181" height="163" /><p
class="wp-caption-text">UDD simple class GUI</p></div></center></p><p>Note the default empty string value for the <b>Name</b> property, and the default zero value for the <b>Value</b> property. In one of the following articles I will show how to control property values. For now let&#8217;s assign &#8216;a&#8217; to <b>Name</b> and 1 to <b>Value</b> using the GUI. Selecting OK updates our object and closes the GUI. Matlab then calls the object&#8217;s <i>disp</i> method to display our object in the command window:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">a = 
  Name<span style="color: #F0F;">:</span> a
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">1.000000</span></pre></div></div><p>We can reopen our object&#8217;s GUI using three methods: The most obvious is to invoke the <i>dialog</i> method using <code>a.dialog</code> or <code>dialog(a)</code>. Alternately, double click on a in the workspace explorer window &#8211; Matlab will automatically call the built-in <i><b>openvar</b></i> function with the variable name and value as arguments. Which leads us to the third method &#8211; simply call <i><b>openvar</b>(&#8216;a&#8217;, a)</i> directly:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Alternatives for programmatically displaying the GUI</span>
a.<span style="color: #0000FF;">dialog</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% or simply: a.dialog</span>
<span style="color: #0000FF;">dialog</span><span style="color: #080;">&#40;</span>a<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">openvar</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'a'</span>,a<span style="color: #080;">&#41;</span>;</pre></div></div><h3 id="help">Accessing UDD help</h3><p>You may have noticed that in our constructor and method files we have included help text. This is good practice for all Matlab files in general, and UDD is no exception. We can access the UDD class help as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="text" style="font-family:monospace;">&gt; help simple.object
 OBJECT constructor for the simple.object class
&nbsp;
    SIMPLEOBJECT = OBJECT(NAME, VALUE) creates an instance of the 
    simple.object class with the Name property set to NAME and the 
    Value property set VALUE
&nbsp;
    SIMPLEOBJECT = OBJECT(NAME) creates an instance of the simple.object
    class with the Name property set to NAME. The Value property will be
    given the default value of 0.
&nbsp;
    SIMPLEOBJECT = OBJECT creates an instance of the simple.object class 
    and executes the simple.object dialog method to open a GUI for editing
    the Name and Value properties.
&nbsp;
    INPUTS:
        NAME          : string
        VALUE         : double
&nbsp;
    OUTPUTS:
        SIMPLEOBJECT  : simple.object instance
&nbsp;
&gt;&gt; help simple.object.disp
 DISP overloaded object disp method
&nbsp;
    DISP(SELF) or SELF.DISP uses the MATLAB builtin DISP function
    to display the Name and Value properties of the object.
&nbsp;
    INPUTS:
        SELF  : simple.object instance</pre></div></div><p>One of the best ways to learn how Matlab works is to examine code written by the Matlab development team. <i><b>openvar</b></i> is a good example: By looking at it we can see that if a variable is a <i><b>handle</b></i> object and is opaque, then <i><b>openvar</b></i> will check to see if it has a <i>dialog</i> method. If so, it will use that to open the variable for editing. With this information we can guess that MCOS, UDD and even java objects can all launch their own dialog editors simply by having an appropriate <i>dialog</i> method.</p><p>An excellent source of UDD information is available in the Matlab toolbox folders. The base Matlab toolbox contains sixteen different UDD packages to explore. Yummy!</p><p>In the next article of this UDD series we will look at creating hierarchical structures using our <code>simple.object</code> and a unique UDD method.</p><p><pre> </pre>Related posts:<ol><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/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/udd-properties/' rel='bookmark' title='UDD Properties'>UDD Properties</a> <small>UDD provides a very convenient way to add customizable properties to existing Matlab object handles...</small></li><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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/creating-a-simple-udd-class/feed/</wfw:commentRss> <slash:comments>0</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-Java interface using a static control</title><link>http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/</link> <comments>http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/#comments</comments> <pubDate>Wed, 13 Oct 2010 18:15:43 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[Karthik Ponudurai]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1917</guid> <description><![CDATA[The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how.<pre> </pre>Related posts:<ol><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/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/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-memory-leaks-performance/' rel='bookmark' title='Matlab-Java memory leaks, performance'>Matlab-Java memory leaks, performance</a> <small>Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>I would like to welcome guest blogger Karthik Ponudurai. Today, Karthik will introduce an interesting technique for interfacing Java GUI to a Matlab application.</i></p><p>The current method of interfacing Java GUI controls and Matlab is by retrieving the underlying Java callback and assigning it to a Matlab callback, as shown in the following example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Create a Java button</span>
jButton = javacomponent<span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">JButton</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'click me'</span><span style="color: #080;">&#41;</span>, <span style="color: #F0F;">...</span>
                        <span style="color: #080;">&#91;</span><span style="color: #33f;">20</span>,<span style="color: #33f;">20</span>,<span style="color: #33f;">60</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Assign a Matlab callback</span>
hButton = handle<span style="color: #080;">&#40;</span>jButton, <span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>jButton, <span style="color:#A020F0;">'ActionPerformedCallback'</span>, @myCallback<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Matlab callback function</span>
<span style="color: #0000FF;">function</span> myCallback<span style="color: #080;">&#40;</span>hObject, hEventData<span style="color: #080;">&#41;</span>
    <span style="color: #228B22;">% Insert your code here</span>
<span style="color: #0000FF;">end</span></pre></div></div><p>This approach works fine for simple pure-Java GUI created using an IDE (for example, <a
target="_blank" rel="nofollow" href="http://netbeans.org/">NetBeans</a> or <a
target="_blank" rel="nofollow" href="http://eclipse.org/">Eclipse</a>) that only have a small number of controls. You can retrieve all the Java controls in Matlab and set the callbacks as shown in the above example.</p><p>Unfortunately, practical GUIs often contain numerous controls. Assigning each control a different Matlab callback can be quite tedious.</p><h3 id="">Using a callback switchyard function</h3><p>A workaround to this problem can be achieved using an invisible/hidden static Java JButton. During Matlab initialization, assign a single callback for the static Java JButton:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Return a reference to the GUI's static trigger button</span>
triggerButton = statictrigger.<span style="">triggerCls</span>.<span style="">getTrigger</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Assign Matlab switchyard callback for triggerButton</span>
buttonProps = handle<span style="color: #080;">&#40;</span>triggerButton, <span style="color:#A020F0;">'callbackproperties'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>triggerButton, <span style="color:#A020F0;">'ActionPerformedCallback'</span>,@TriggerCallback<span style="color: #080;">&#41;</span>;</pre></div></div><p>When an event is triggered by any monitored control in the Java GUI, then within the Java code set the static button&#8217;s action command string to the desired Matlab callback name (a String) and invoke the static button&#8217;s <i>doClick()</i> method to trigger the Matlab TriggerCallback callback. Based on the action command string, this callback invokes the appropriate Matlab function. In this manner, Matlab&#8217;s TriggerCallback() acts as a <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/company/newsletters/news_notes/win00/prog_patterns.html">switchyard function</a> to the actual callback functions:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> TriggerCallback<span style="color: #080;">&#40;</span>hObject, hEventData<span style="color: #080;">&#41;</span>
   actionCmd = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span> hObject.<span style="">getActionCommand</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#41;</span>;
   hgfeval<span style="color: #080;">&#40;</span>actionCmd, hObject, hEventData<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>Note how the <i><b>char</b></i> function converts the Java action command (a java.lang.String object) into Matlab char array. The requested Matlab function is then dynamically invoked using Matlab&#8217;s semi-documented <a
target="_blank" href="http://undocumentedmatlab.com/blog/hgfeval/"><i><b>hgfeval</b></i> function</a>, which Yair will describe in another article later this month.</p><p>The next few sections will look into the sample application in more detail. This application can be downloaded from <a
target="_blank" href="http://UndocumentedMatlab.com/files/StaticButtonTrigger.zip">here</a>.</p><h3 id="GUI">Java GUI</h3><p>Firstly, let&#8217;s look into the Java GUI:<br
/><center><div
class="wp-caption aligncenter" style="width: 418px"><img
alt="Main Java GUI" src="http://UndocumentedMatlab.com/images/StaticButton_Java_Frame.png" title="Main Java GUI" width="408" height="325" /><p
class="wp-caption-text">Main Java GUI</p></div></center></p><p>When the &#8220;Show Result in Matlab Dialog Box&#8221; button is clicked, the form information&#8217;s (Name, Age, Sex etc&#8230;) is saved in a hash map. This hash map is then serialized to a local file (C:\streamdata). Next, the static button&#8217;s action command string is set to &#8216;show&#8217; and finally its <i>doClick()</i> method is invoked.</p><p>Below is the Java code corresponding to button click event:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> showActionPerformed<span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">awt</span>.<span style="color: #006633;">event</span>.<span style="color: #003399;">ActionEvent</span> evt<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Add form values to hash map</span>
    hMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>name.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, name.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    hMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>age.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, age.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    hMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>sex.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span>sex.<span style="color: #006633;">getSelectedItem</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    hMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>address.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, address.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    hMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>email.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, email.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Serialize hash map</span>
    WriteObject<span style="color: #009900;">&#40;</span>hMap<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Set trigger</span>
    triggerCls.<span style="color: #006633;">setTrigger</span><span style="color: #009900;">&#40;</span>evt.<span style="color: #006633;">getActionCommand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><h3 id="Matlab">Matlab callbacks</h3><p>The Java GUI sources are compiled and packaged in the statictrigger.jar file. The Matlab StaticButtonTriggerExample.m source file adds the Java library to the dynamic Java classpath:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Add dynamic java class path</span>
javaclasspath<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #0000FF;">pwd</span> <span style="color:#A020F0;">'\javalib\statictrigger.jar'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>The main.m function contains the code for creating the Java GUI and assigning the static button callback:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% create the statictrigger object</span>
app = statictrigger.<span style="">app</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Set static trigger button listener</span>
triggerButton = statictrigger.<span style="">triggerCls</span>.<span style="">getTrigger</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
buttonProps = handle<span style="color: #080;">&#40;</span>triggerButton,<span style="color:#A020F0;">'callbackproperties'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>buttonProps,<span style="color:#A020F0;">'ActionPerformedCallback'</span>, <span style="color: #080;">&#123;</span>@StaticButtonTriggerCallback, app<span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>; 
&nbsp;
<span style="color: #228B22;">% Show the Java Frame </span>
app.<span style="">setVisible</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>statictrigger.triggerCls.<i>getTrigger()</i> is a static Java public method that returns the static button object, for which we assign our callback switchyard function.</p><p>The following is the StaticButtonTriggerCallback.m code, which displays the information in a Matlab message-box:<br
/><center><div
class="wp-caption aligncenter" style="width: 409px"><img
alt="Matlab message-box triggered by the Java GUI" src="http://UndocumentedMatlab.com/images/StaticButton_Matlab_Msgbox.png" title="Matlab message-box triggered by the Java GUI" width="399" height="133" /><p
class="wp-caption-text">Matlab message-box triggered by the Java GUI</p></div></center></p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Get action command string  </span>
actionCommand = src.<span style="">getActionCommand</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Read in serialized hash map</span>
hashMap = app.<span style="">ReadObject</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Convert hash map to Matlab structure     </span>
data = Hash2Struct<span style="color: #080;">&#40;</span>hashMap<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Display data in Matlab dialog box</span>
msg = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'Name: '</span> <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>hashMap.<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'name'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">', Age: '</span> <span style="color: #0000FF;">num2str</span><span style="color: #080;">&#40;</span>hashMap.<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'age'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">', Sex: '</span> <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>hashMap.<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'sex'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">', Address: '</span> <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>hashMap.<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'address'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">', Email: '</span> <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>hashMap.<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'email'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#93;</span>;
uiwait<span style="color: #080;">&#40;</span><span style="color: #0000FF;">helpdlg</span><span style="color: #080;">&#40;</span>msg, <span style="color:#A020F0;">'Form Details'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>You can download the example&#8217;s files from <a
target="_blank" href="http://UndocumentedMatlab.com/files/StaticButtonTrigger.zip">here</a>. Extract the zip file, <i><b>cd</b></i> to the newly-generated StaticButtonTrigger folder, then invoke the StaticButtonTriggerExample.m function to run the example.</p><p><pre> </pre>Related posts:<ol><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/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/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-memory-leaks-performance/' rel='bookmark' title='Matlab-Java memory leaks, performance'>Matlab-Java memory leaks, performance</a> <small>Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/feed/</wfw:commentRss> <slash:comments>17</slash:comments> </item> <item><title>GUI automation utilities</title><link>http://undocumentedmatlab.com/blog/gui-automation-utilities/</link> <comments>http://undocumentedmatlab.com/blog/gui-automation-utilities/#comments</comments> <pubDate>Wed, 22 Sep 2010 18:00:28 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Figure window]]></category> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Kesh Ikuma]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1877</guid> <description><![CDATA[This article explains a couple of Matlab utilities that use Java's Robot class to programmatically control mouse and keyboard actions<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/gui-automation-robot/' rel='bookmark' title='GUI automation using a Robot'>GUI automation using a Robot</a> <small>This article explains how Java's Robot class can be used to programmatically control mouse and keyboard actions...</small></li><li><a
href='http://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties/' rel='bookmark' title='Borderless button used for plot properties'>Borderless button used for plot properties</a> <small>A borderless button can be used to add unobtrusive functionality to plot axes...</small></li><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/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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><em>Once again, I welcome guest writer <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/authors/36665">Takeshi (Kesh) Ikuma</a>. Last week, Kesh described how <a
target="_blank" href="http://undocumentedmatlab.com/blog/gui-automation-robot/">Java&#8217;s Robot class can be used to automate Matlab GUI</a>. Today, Kesh will describe two Matlab utilities that help this automation process.</em></p><p><center><img
alt="" src="http://UndocumentedMatlab.com/images/robot.gif" title="" width="96" height="156" /></center></p><p>jMouseEmu and inputEmu are both available on the Matlab File Exchange. There is a large overlap between these utilities. As their name suggests, jMouseEmu is geared to control the mouse, while inputEmu emulates both mouse and keyboard inputs.</p><h3 id="jMouseEmu">jMouseEmu</h3><p>The <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/28357-jMouseEmu">jMouseEmu utility</a> is an interface function to simplify the programmatic execution of mouse actions (movements, clicks and wheel scrolls). jmouseemu features include accepting relative position with regards to a specified Handle Graphics control, supporting the Shift and Control click modifier keys, supporting double clicking and mouse dragging, controlling mouse wheel turns and allowing multiple command execution with interval timing specification.</p><p>jmouseemu has two operating modes: single- or multi-command. In this respect, a single jmouseemu command consists of a cursor position and a click option &#8211; multiple such commands can be specified to jmouseemu in the multi-command mode.</p><p>In the single-command mode, the function takes the cursor position information and click options. The click options can be any of the possible <strong>SelectionType</strong> property strings, or &#8220;none&#8221; if no clicking is desired. For example,</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jmouseemu<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #33f;">500</span>,<span style="color: #33f;">250</span><span style="color: #080;">&#93;</span>,<span style="color:#A020F0;">'normal'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>places the mouse cursor at the coordinate (500, 250) with respect to the current figure (in Matlab coordinates) and then clicks the left mouse button. If no figure is currently open, the cursor is placed at (500,250) with respect to the bottom-left corner of the screen.</p><p>The reference graphics handle can be added as the first argument. For example, if hEdit is a <i><b>uicontrol</b></i> editbox handle, then</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jmouseemu<span style="color: #080;">&#40;</span>hEdit,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,<span style="color:#A020F0;">'open'</span><span style="color: #080;">&#41;</span>;
jmouseemu<span style="color: #080;">&#40;</span>hEdit,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,<span style="color:#A020F0;">'normal'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>provides a simple way to select the entire text in the edit box: the double-click <i><b>jmouseemu</b></i> call selects the first word, then the following single-click <i><b>jmouseemu</b></i> extends the selection to the entire text. If the position argument omitted, the cursor is placed on the upper-left hand corner of the object, unless hEdit=0, in which case the cursor stays at the current position.</p><p>To turn the mouse wheel 600 notches down (towards you), run:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jmouseemu<span style="color: #080;">&#40;</span>-<span style="color: #33f;">600</span>,<span style="color:#A020F0;">'wheel'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Multi-command mode extends the single-command mode by accepting a cell array of commands along with the update interval T in seconds. The command array must have three columns, and its k-th row specifies the mouse command at time (k–1)T second.</p><p>Using the multi-command mode, the above example can be executed with a single <i><b>jmouseemu</b></i> call, placing the commands in a cell array:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jmouseemu<span style="color: #080;">&#40;</span><span style="color: #080;">&#123;</span>h,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,<span style="color:#A020F0;">'open'</span>; <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,<span style="color:#A020F0;">'normal'</span><span style="color: #080;">&#125;</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% T=0</span></pre></div></div><p>Here, if you omit handles (i.e., the first column of command array) for the second or later commands, it defaults to the handle of the previous command.</p><p>Multi-command mode enables mouse cursor to follow a predefined path. For example, to make the mouse cursor travel in a full circle:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">T = <span style="color: #33f;">0.01</span>; <span style="color: #228B22;">% update every 10 milliseconds</span>
x = <span style="color: #0000FF;">pi</span> * <span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #F0F;">:</span>T<span style="color: #F0F;">:</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>';
pos = <span style="color: #33f;">100</span>*<span style="color: #080;">&#91;</span><span style="color: #0000FF;">cos</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span>,<span style="color: #0000FF;">sin</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span> + <span style="color: #33f;">250</span>;
N = numel<span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span>;
cmds = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>N,<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% empty click option -&gt; no click</span>
cmds<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#125;</span> = <span style="color: #0000FF;">gcf</span>; <span style="color: #228B22;">% position w.r.t. current figure</span>
cmds<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span>,<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> = mat2cell<span style="color: #080;">&#40;</span>pos,<span style="color: #0000FF;">ones</span><span style="color: #080;">&#40;</span>N,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>,<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;
jmouseemu<span style="color: #080;">&#40;</span>cmds,T<span style="color: #080;">&#41;</span>;</pre></div></div><p>The command interval is realized by calling robot.<em>delay</em>(T*1e3) between commands. This example updates the mouse position every 10 msecs and completes the circle in 2 seconds.</p><p>In multi-command mode, there are two additional click options, &#8220;drag_on&#8221; and &#8220;drag_off&#8221;, available to enable mouse dragging. Dragging must be initiated and terminated within a single <i><b>jmouseemu</b></i> call, and &#8220;drag_off&#8221; and &#8220;none&#8221; are the only two acceptable click options during dragging.</p><p>Lastly, to pause mouse movement for a moment (longer than the update interval) , a &#8220;delay&#8221; command may be used:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jmouseemu<span style="color: #080;">&#40;</span><span style="color: #080;">&#123;</span><span style="color: #F0F;">...</span>; <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,<span style="color: #33f;">1.0</span>,<span style="color:#A020F0;">'delay'</span>; <span style="color: #F0F;">...</span><span style="color: #080;">&#125;</span>, T<span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% pause for 1 second</span></pre></div></div><h3 id="warning">Using jMouseEmu to trigger multiple Matlab HG callbacks</h3><p><i><b>jmouseemu</b></i> (or more broadly the Matlab handling of Java Robot commands) has a major drawback: If <i><b>jmouseemu</b></i> is used to trigger a Matlab GUI callback during a sequence of mouse maneuver, the callback will not be executed until the entire maneuver is completed. Moreover, since <b>SelectionType</b> property is not logged for every callback event, the incorrect <b>SelectionType</b> property may be reported during callback execution.</p><p>For example, add a double click at the beginning of the circular movement:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">cmds<span style="color: #080;">&#123;</span><span style="color: #F0F;">:</span>,<span style="color: #33f;">3</span><span style="color: #080;">&#125;</span> = <span style="color:#A020F0;">'open'</span>;
jmouseemu<span style="color: #080;">&#40;</span>cmds,T<span style="color: #080;">&#41;</span>;</pre></div></div><p>Running this code, you can observe the figure&#8217;s <b>WindowsButtonDownFcn</b> callback, which has been set to echo <b>SelectionType</b> property from earlier, does not kick in until the circle is completed. Now, introduce &#8220;extend&#8221; click at the end of the circle:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">cmds<span style="color: #080;">&#123;</span><span style="color: #0000FF;">end</span>,<span style="color: #33f;">3</span><span style="color: #080;">&#125;</span> = <span style="color:#A020F0;">'extend'</span>;
jmouseemu<span style="color: #080;">&#40;</span>cmds,T<span style="color: #080;">&#41;</span>;</pre></div></div><p>Again, no callback displaying <b>SelectionType</b> echo occurs during the mouse movement. When the callbacks are finally executed, they return in the order – &#8220;normal&#8221;, &#8220;extend&#8221;, &#8220;open&#8221; – instead of expected &#8220;normal&#8221;, &#8220;open&#8221;, &#8220;extend&#8221;. To correct this behavior, <i><b>jmouseemu</b></i> must be called twice:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jmouseemu<span style="color: #080;">&#40;</span>cmds<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color: #F0F;">:</span><span style="color: #080;">&#41;</span>,T<span style="color: #080;">&#41;</span>;
jmouseemu<span style="color: #080;">&#40;</span>cmds<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #F0F;">:</span><span style="color: #0000FF;">end</span>,<span style="color: #F0F;">:</span><span style="color: #080;">&#41;</span>,T<span style="color: #080;">&#41;</span>;</pre></div></div><p>The key here is for each <i><b>jmouseemu</b></i> call to have only one callback triggering mouse click command and to place it at the end of the command sequence.</p><h3 id="inputEmu">inputEmu</h3><p>The <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/28603-inputEmu">inputEmu utility</a> is similar to jMouseEmu, with extended support for keyboard inputs. The support for relative mouse cursor position with respect to an HG object, however, has been turned off in the current version of inputEmu (it may get turned on in a future version). Position information in inputEmu is always given in pixels with respect to the lower-left corner of the main display screen. Also, the user must note that the input arguments are flipped between jMouseEmu and inputEmu.</p><p>In addition to the left mouse click commands from jMouseEmu, inputEmu can emulate the right and middle mouse button clicks. For example, to perform the right click (e.g., to open a context menu):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">inputemu<span style="color: #080;">&#40;</span><span style="color: #080;">&#123;</span><span style="color:#A020F0;">'right_down'</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>; <span style="color:#A020F0;">'right_up'</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#125;</span>'<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% execute right button click</span></pre></div></div><p>inputEmu&#8217;s main feature is keyboard support. There are 4 basic commands (the behavior may be somewhat different on non-windows platforms):</p><ul><li>key_normal &#8211; Normal key press</li><li>key_ctrl	- CONTROL + key press</li><li>key_alt &#8211; ALT + key press</li><li>key_win &#8211; WIN + key press</li></ul><p>One other obvious key press, SHIFT + key press, is conspicuously missing from this list. This is because the SHIFT key is automatically pressed when a SHIFT modified character (e.g., an upper-case alphabet) is entered. For example, executing</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">inputemu<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'key_normal'</span>,<span style="color:#A020F0;">'Hello World'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% emulates typing &quot;Hello World&quot;</span></pre></div></div><p>directly in Matlab prompt displays &#8220;Hello World&#8221; in the next command line. The text is first decoded to a sequence of key press and release pairs, including SHIFT key presses and releases as needed, then the sequence of key commands are sent to the java Robot object.</p><p>In addition to all the characters on the keyboard, non-character keys can be stroked by using escape characters:</p><ul><li>&#8216;\SHIFT&#8217;</li><li>&#8216;\CTRL&#8217;</li><li>&#8216;\ALT&#8217;</li><li>&#8216;\WINDOWS&#8217;</li><li>&#8216;\BACKSPACE&#8217;</li><li>&#8216;\TAB&#8217;</li><li>&#8216;\ENTER&#8217;</li><li>&#8216;\ESC&#8217;</li><li>&#8216;\PAGEUP&#8217;</li><li>&#8216;\PAGEDOWN&#8217;</li><li>&#8216;\END&#8217;</li><li>&#8216;\HOME&#8217;</li><li>&#8216;\LEFT&#8217;</li><li>&#8216;\UP&#8217;</li><li>&#8216;\RIGHT&#8217;</li><li>&#8216;\DOWN&#8217;</li><li>&#8216;\INSERT&#8217;</li><li>&#8216;\DELETE&#8217;</li><li>&#8216;\CAPSLOCK&#8217;</li><li>&#8216;\NUMLOCK&#8217;</li><li>&#8216;\SCROLLLOCK&#8217;</li><li>&#8216;\PRINTSCREEN&#8217;</li><li>&#8216;\PAUSE&#8217;</li></ul><p>Also, use &#8216;\\&#8217; for the forward slash character and &#8216;\F1&#8242; – &#8216;\F12&#8242; for the Function keys. For example, to execute a Matlab &#8220;<i><b>ver</b></i>&#8221; command automatically via command window, run</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">inputemu<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'key_normal'</span>,<span style="color:#A020F0;">'ver\ENTER'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% runs the ver command</span></pre></div></div><p>Lastly, as mentioned in the previous article, java.awt.Robot is sensitive to the keyboard locale, and the inputEmu utility is programmed specifically for the US QWERTY keyboard. To use the inputEmu for another keyboard layout, the program has to be modified accordingly. If anyone&#8217;s interested using this utility for different keyboard layout, I&#8217;d be glad to provide assistance.</p><p>Do you use Java&#8217;s Robot class or any of the utilities above in your work? Yair and I would love to hear about your experience in a short <a
href="http://undocumentedmatlab.com/blog/gui-automation-utilities/#respond">comment</a> below.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/gui-automation-robot/' rel='bookmark' title='GUI automation using a Robot'>GUI automation using a Robot</a> <small>This article explains how Java's Robot class can be used to programmatically control mouse and keyboard actions...</small></li><li><a
href='http://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties/' rel='bookmark' title='Borderless button used for plot properties'>Borderless button used for plot properties</a> <small>A borderless button can be used to add unobtrusive functionality to plot axes...</small></li><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/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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/gui-automation-utilities/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           category
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-02-09 04:42:31 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/category/guest-bloggers/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      3.375s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1328787748; expires=Fri, 08-Feb-2013 11:42:28 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Thu, 09 Feb 2012 11:42:31 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Thu, 09 Feb 2012 12:42:31 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               9a051d6ed4efe42b2ec46c8c0969bb68
Content-Encoding:   gzip
-->
