<?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; Listeners</title> <atom:link href="http://undocumentedmatlab.com/blog/category/listeners/feed/" rel="self" type="application/rss+xml" /><link>http://undocumentedmatlab.com</link> <description>Charting Matlab's unsupported hidden underbelly</description> <lastBuildDate>Thu, 17 May 2012 12:01:26 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.1</generator> <item><title>Setting axes tick labels format</title><link>http://undocumentedmatlab.com/blog/setting-axes-tick-labels-format/</link> <comments>http://undocumentedmatlab.com/blog/setting-axes-tick-labels-format/#comments</comments> <pubDate>Wed, 18 Apr 2012 18:00:17 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Listener]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[schema.prop]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2856</guid> <description><![CDATA[Matlab plot axes ticks can be customized in a way that will automatically update whenever the tick values change.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/axes-looseinset-property/' rel='bookmark' title='Axes LooseInset property'>Axes LooseInset property</a> <small>Matlab plot axes have an undocumented LooseInset property that sets empty margins around the axes, and can be set to provide a tighter fit of the axes to their surroundings....</small></li><li><a
href='http://undocumentedmatlab.com/blog/determining-axes-zoom-state/' rel='bookmark' title='Determining axes zoom state'>Determining axes zoom state</a> <small>The information of whether or not an axes is zoomed or panned can easily be inferred from an internal undocumented object....</small></li><li><a
href='http://undocumentedmatlab.com/blog/fig-files-format/' rel='bookmark' title='FIG files format'>FIG files format</a> <small>FIG files are actually MAT files in disguise. This article explains how this can be useful in Matlab applications....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Have you ever tried to customize the way in which tick labels appear in Matlab plot axes?</p><p>For example, setting the numerical precision of the labels, or adding some short descriptive text (for example, the units)? If you have, then I bet that you have encountered the following dilemma: Once we modify the tick labels (for discussion sake, let&#8217;s assume the Y axis, so this is done by updating the <b>YTickLabel</b> property), then the corresponding <b>YTickLabelMode</b> property changes from &#8216;auto&#8217; to &#8216;manual&#8217; and loses its relationship to the tick values (<b>YTick</b>). So, if we now zoom or pan the plot, our new labels remain unchanged although the tick values have changed, causing much panic and frustration&#8230; If we also set the tick values manually, this solves <i>that</i> problem but leaves us with another: now, when we zoom or pan, we no longer see any ticks or tick labels at all!</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
src="http://UndocumentedMatlab.com/images/PlotLabels_orig.png" alt="Original plot" title="Original plot" width="134" height="143" /> <img
src="http://UndocumentedMatlab.com/images/PlotLabels_1b.png" alt="Manual labels, auto ticks" title="Manual labels, auto ticks" width="134" height="143" /> <img
src="http://UndocumentedMatlab.com/images/PlotLabels_1c.png" alt="Manual labels, manual ticks" title="Manual labels, manual ticks" width="134" height="143" /><p
class="wp-caption-text">Original plot (left)<br
/>Manual labels, auto ticks (center)<br
/>Manual labels, manual ticks (right)</p></div></center></p><p>Of course, we can always trap the zoom and pan callback functions to update the tick labels dynamically while keeping the tick values automatically. This will work for these cases, but we need to do it separately for zoom and pan. Also, if we modify the axes limits explicitly (via the corresponding <b>YLim</b> property) or indirectly (by modifying the displayed plot data), then the callbacks are not called and the labels are not updated.</p><h3 id="solution">The solution &#8211; using a property change listener</h3><p>A better way to solve this problem is to simply trap changes to the displayed tick values, and whenever these occur to call our dedicated function to update the labels according to the new tick values. This can be done by using UDD, or more precisely the <a
target="_blank" href="http://undocumentedmatlab.com/blog/udd-events-and-listeners/">ability to trap update events on any property</a> (in our case, <b>YTick</b>). Such a mechanism was already <a
target="_blank" href="http://undocumentedmatlab.com/blog/continuous-slider-callback/#Property_Listener">demonstrated here</a> in 2010, as one way to achieve continuous slider feedback. The idea is to use the built-in <i><b>handle.listener</b></i> function with the PropertyPostSet event, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hhAxes = handle<span style="color: #080;">&#40;</span>hAxes<span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% hAxes is the Matlab handle of our axes</span>
hProp = <span style="color: #0000FF;">findprop</span><span style="color: #080;">&#40;</span>hhAxes,<span style="color:#A020F0;">'YTick'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% a schema.prop object</span>
hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hhAxes, hProp, <span style="color:#A020F0;">'PropertyPostSet'</span>, @myCallbackFunction<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">setappdata</span><span style="color: #080;">&#40;</span>hAxes, <span style="color:#A020F0;">'YTickListener'</span>, hListener<span style="color: #080;">&#41;</span>;</pre></div></div><p>Note that we have used <i><b>setappdata</b></i> to store the <code>hListener</code> handle in the axes. This ensures that the listener exists for exactly as long as the axes does. If we had not stored this listener handle somewhere, then Matlab would have immediately deleted the listener hook and our callback function would not have been called upon tick value updates. Forgetting to store listener handles is a common pitfall when using them. If you take a look at the <i><b>addlistener</b></i> function&#8217;s code, you will see that it also uses <i><b>setappdata</b></i> after creating the listener, for exactly this reason. Unfortunately, <i><b>addlistsner</b></i> cannot always be used, and I keep forgetting under which circumstances, so I generally use <i><b>handle.listener</b></i> directly as above: It&#8217;s simple enough to use that I find I never really need to use the simple <i><b>addlistener</b></i> wrapper, but you are welcome to try it yourself.</p><p>That&#8217;s all there is to it: Whenever <b>YTick</b> changes its value(s), our callback function (<i>myCallbackFunction</i>) will automatically be called. It is quite simple to set up. While we cannot use TeX in tick labels yet (this will change in the upcoming <a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-hg2/">HG2</a>), using <i><b>sprintf</b></i> formatting does enable quite a bit of flexibility in formatting the labels. For example, let&#8217;s say I want my tick labels to have the format &#8216;%.1fV&#8217; (i.e., always one decimal, plus the Volts units):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> myCallbackFunction<span style="color: #080;">&#40;</span>hProp,eventData<span style="color: #080;">&#41;</span>    <span style="color: #228B22;">%#ok - hProp is unused</span>
   hAxes = eventData.<span style="">AffectedObject</span>;
   tickValues = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hAxes,<span style="color:#A020F0;">'YTick'</span><span style="color: #080;">&#41;</span>;
   newLabels = arrayfun<span style="color: #080;">&#40;</span>@<span style="color: #080;">&#40;</span>value<span style="color: #080;">&#41;</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'%.1fV'</span>,value<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, tickValues, <span style="color:#A020F0;">'UniformOutput'</span>,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hAxes, <span style="color:#A020F0;">'YTickLabel'</span>, newLabels<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% myCallbackFunction</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 310px"><img
src="http://UndocumentedMatlab.com/images/PlotLabels_2a.png" alt="Manual labels, automatically updated" title="Manual labels, automatically updated" width="134" height="143" /> <img
src="http://UndocumentedMatlab.com/images/PlotLabels_2b.png" alt="Manual labels, automatically updated" title="Manual labels, automatically updated" width="134" height="143" /><p
class="wp-caption-text">Manual labels, automatically updated</p></div></center></p><h3 id="duplicates">Handling duplicate tick labels</h3><p>Of course, &#8216;%.1fV&#8217; may not be a good format when we zoom in to such a degree that the values differ by less than 0.1 &#8211; in this case all the labels will be the same. So let&#8217;s modify our callback function to add extra decimals until the labels become distinct:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> myCallbackFunction<span style="color: #080;">&#40;</span>hProp,eventData<span style="color: #080;">&#41;</span>    <span style="color: #228B22;">%#ok - hProp is unused</span>
   hAxes = eventData.<span style="">AffectedObject</span>;
   tickValues = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hAxes,<span style="color:#A020F0;">'YTick'</span><span style="color: #080;">&#41;</span>;
&nbsp;
   <span style="color: #228B22;">%newLabels = arrayfun(@(value)(sprintf('%.1fV',value)), tickValues, 'UniformOutput',false);</span>
   digits = <span style="color: #33f;">0</span>;
   labelsOverlap = <span style="color: #0000FF;">true</span>;
   <span style="color: #0000FF;">while</span> labelsOverlap
      <span style="color: #228B22;">% Add another decimal digit to the format until the labels become distinct</span>
      digits = digits + <span style="color: #33f;">1</span>;
      <span style="color: #0000FF;">format</span> = <span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'%%.%dfV'</span>,digits<span style="color: #080;">&#41;</span>;
      newLabels = arrayfun<span style="color: #080;">&#40;</span>@<span style="color: #080;">&#40;</span>value<span style="color: #080;">&#41;</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">format</span>,value<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, tickValues, <span style="color:#A020F0;">'UniformOutput'</span>,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
      labelsOverlap = <span style="color: #080;">&#40;</span><span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>newLabels<span style="color: #080;">&#41;</span> &gt; <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">unique</span><span style="color: #080;">&#40;</span>newLabels<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
      <span style="color: #228B22;">% prevent endless loop if the tick values themselves are non-unique</span>
      <span style="color: #0000FF;">if</span> labelsOverlap <span style="color: #F0F;">&amp;&amp;</span> <span style="color: #0000FF;">max</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">diff</span><span style="color: #080;">&#40;</span>tickValues<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>&lt; <span style="color: #33f;">16</span>*<span style="color: #0000FF;">eps</span>
         <span style="color: #0000FF;">break</span>;
      <span style="color: #0000FF;">end</span>
   <span style="color: #0000FF;">end</span>
&nbsp;
   <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hAxes, <span style="color:#A020F0;">'YTickLabel'</span>, newLabels<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% myCallbackFunction</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 310px"><img
src="http://UndocumentedMatlab.com/images/PlotLabels_3a.png" alt="non-distinct labels" title="non-distinct labels" width="134" height="143" /> <img
src="http://UndocumentedMatlab.com/images/PlotLabels_3b.png" alt="distinct labels" title="distinct labels" width="134" height="143" /><p
class="wp-caption-text">Non-distinct labels &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; distinct labels</p></div></center></pre><h3 id="ticklabelformat"><i>ticklabelformat</i></h3><p>Based on a file that I received from an anonymous reader a few years ago, I have prepared a utility called <i><b>ticklabelformat</b></i> that automates much of the set-up above. Feel free to <a
target="_blank" href="http://www.mathworks.com/matlabcentral/fileexchange/36254-ticklabelformat">download</a> this utility and modify it for your needs - it's quite simple to read and follow. The usage syntax is as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">ticklabelformat<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'y'</span>,<span style="color:#A020F0;">'%.6g V'</span><span style="color: #080;">&#41;</span>  <span style="color: #228B22;">% sets y axis on current axes to display 6 significant digits</span>
ticklabelformat<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'xy'</span>,<span style="color:#A020F0;">'%.2f'</span><span style="color: #080;">&#41;</span>   <span style="color: #228B22;">% sets x &amp; y axes on current axes to display 2 decimal digits</span>
ticklabelformat<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'z'</span>,@myCbFcn<span style="color: #080;">&#41;</span>  <span style="color: #228B22;">% sets a function to update the Z tick labels on current axes</span>
ticklabelformat<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'z'</span>,<span style="color: #080;">&#123;</span>@myCbFcn,extraData<span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>  <span style="color: #228B22;">% sets an update function as above, with extra data</span></pre></div></div><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/axes-looseinset-property/' rel='bookmark' title='Axes LooseInset property'>Axes LooseInset property</a> <small>Matlab plot axes have an undocumented LooseInset property that sets empty margins around the axes, and can be set to provide a tighter fit of the axes to their surroundings....</small></li><li><a
href='http://undocumentedmatlab.com/blog/determining-axes-zoom-state/' rel='bookmark' title='Determining axes zoom state'>Determining axes zoom state</a> <small>The information of whether or not an axes is zoomed or panned can easily be inferred from an internal undocumented object....</small></li><li><a
href='http://undocumentedmatlab.com/blog/fig-files-format/' rel='bookmark' title='FIG files format'>FIG files format</a> <small>FIG files are actually MAT files in disguise. This article explains how this can be useful in Matlab applications....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/setting-axes-tick-labels-format/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Blurred Matlab figure window</title><link>http://undocumentedmatlab.com/blog/blurred-matlab-figure-window/</link> <comments>http://undocumentedmatlab.com/blog/blurred-matlab-figure-window/#comments</comments> <pubDate>Wed, 20 Apr 2011 18:58:12 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Figure window]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[JavaFrame]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2259</guid> <description><![CDATA[Matlab figure windows can be blurred using a semi-transparent overlaid window - this article explains how<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/transparent-matlab-figure-window/' rel='bookmark' title='Transparent Matlab figure window'>Transparent Matlab figure window</a> <small>Matlab figure windows can be made fully or partially transparent/translucent or blurred - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/minimize-maximize-figure-window/' rel='bookmark' title='Minimize/maximize figure window'>Minimize/maximize figure window</a> <small>Matlab figure windows can easily be maximized, minimized and restored using a bit of undocumented magic powder...</small></li><li><a
href='http://undocumentedmatlab.com/blog/disable-entire-figure-window/' rel='bookmark' title='Enable/disable entire figure window'>Enable/disable entire figure window</a> <small>Disabling/enabling an entire figure window is impossible with pure Matlab, but is very simple using the underlying Java. 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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Following my post two weeks ago about <a
target="_blank" href="http://undocumentedmatlab.com/blog/disable-entire-figure-window/">disabling an entire Matlab figure window</a>, and my article last week about <a
target="_blank" href="http://undocumentedmatlab.com/blog/transparent-matlab-figure-window/">setting the transparency of a figure window</a>, I would like to combine these two functionalities into a blurred-window effect for a disabled window.</p><h3 id="basic">The basic idea</h3><p>The basic idea, as reader Mory <a
target="_blank" href="http://undocumentedmatlab.com/blog/transparent-matlab-figure-window/#comment-40154">pointed out</a> in a comment, is to overlay a semi-transparent empty figure window, having just the right size and position, on top of the main (disabled) figure window:</p><p><center><div
class="wp-caption aligncenter" style="width: 640px"><img
alt="Main (unblurred) window" src="http://UndocumentedMatlab.com/images/blurFigure1a.png" title="Main (unblurred) window" width="190" /> + <img
alt="Semi-transparent window" src="http://UndocumentedMatlab.com/images/blurFigure1b.png" title="Semi-transparent window" width="190" /> = <img
alt="Blurred window effect" src="http://UndocumentedMatlab.com/images/blurFigure1c.png" title="Blurred window effect" width="190" /><p
class="wp-caption-text">Main (unblurred) window + semi-transparent window = blurred window effect</p></div></center></p><p>In addition to ensuring the correct figure size and position, there are several other things we should care for: We need to synchronize the figure/blurring color, menubar/toolbar, and title. We also need to handle docking, resizing and figure movement. Finally we need to connect the two figures so that whenever one is closed then so is the other.</p><h3 id="blurFigure">blurFigure</h3><p>My <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/30666-blurfigure-blurs-and-prevents-interaction-on-a-figure-window"><i><b>blurFigure</b></i> utility</a> on the Matlab File Exchange attempts to handle all these setups for the user. The utility is quite simple to use:</p><p><i><b>blurFigure</b>(hFig)</i> blurs figure <i>hFig</i> and prevents interaction with it. The only interaction possible is with user-created controls on the blurring panel (see below).</p><p>hFigBlur = <i><b>blurFigure</b>(hFig)</i> returns the overlaid blurred figure pane. This is useful to present a progress bar or other GUI controls, for user interaction during the blur phase (see the demo below).</p><p><i><b>blurFigure</b>(hFig,STATE)</i> sets the blur status of figure <i>hFig</i> to <i>STATE</i>, where state is &#8216;on&#8217;,'off&#8217;,true or false (default=&#8217;on&#8217;/true). <i><b>blurFigure</b>(hFig,&#8217;on&#8217;)</i> or <i><b>blurFigure</b>(hFig,true)</i> is the same as: <i><b>blurFigure</b>(hFig)</i>; <i><b>blurFigure</b>(hFig,&#8217;off&#8217;)</i> or <i><b>blurFigure</b>(hFig,false)</i> is the same as: <i><b>close</b>(hFigBlur)</i>.</p><p><i><b>blurFigure</b>(&#8216;demo&#8217;)</i> displays a simple demo of the blurring. In fact, this runs the following simple code:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Create the main (blurred) figure window</span>
hFigMain = <span style="color: #0000FF;">figure</span>;
&nbsp;
<span style="color: #228B22;">% Display some GUI controls in the main window</span>
<span style="color: #0000FF;">try</span> oldWarn = <span style="color: #0000FF;">warning</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'off'</span>,<span style="color:#A020F0;">'MATLAB:uitree:MigratingFunction'</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">catch</span>, <span style="color: #0000FF;">end</span>
hTree = uitree<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'root'</span>,<span style="color:#A020F0;">'c:\'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">drawnow</span>;
<span style="color: #0000FF;">try</span> hTree.<span style="">getTree</span>.<span style="">expandRow</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">catch</span>, <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">try</span> <span style="color: #0000FF;">warning</span><span style="color: #080;">&#40;</span>oldWarn<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">catch</span>, <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'string'</span>,<span style="color:#A020F0;">'click me!'</span>, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'pixel'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">300</span>,<span style="color: #33f;">50</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">axes</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,<span style="color: #0000FF;">gcf</span>, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'pixel'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">230</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">300</span>,<span style="color: #33f;">300</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">surf</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">peaks</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>,<span style="color:#A020F0;">'ToolBar'</span>,<span style="color:#A020F0;">'figure'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% restore the toolbar that was removed by the uicontrol() call above</span>
&nbsp;
<span style="color: #228B22;">% Call blurFigure() to add a semi-transparent overlaid window</span>
hFigBlurTemp = blurFigure<span style="color: #080;">&#40;</span>hFigMain<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Add some non-blurred controls on top of the blur</span>
<span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hFigBlurTemp, <span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'text'</span>, <span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'pixel'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">130</span>,<span style="color: #33f;">85</span>,<span style="color: #33f;">390</span>,<span style="color: #33f;">80</span><span style="color: #080;">&#93;</span>, <span style="color: #F0F;">...</span>
          <span style="color:#A020F0;">'string'</span>,<span style="color:#A020F0;">'Processing - please wait...'</span>, <span style="color:#A020F0;">'FontSize'</span>,<span style="color: #33f;">12</span>, <span style="color:#A020F0;">'FontWeight'</span>,<span style="color:#A020F0;">'bold'</span>,<span style="color:#A020F0;">'Fore'</span>,<span style="color:#A020F0;">'red'</span>,<span style="color:#A020F0;">'Back'</span>,<span style="color:#A020F0;">'yellow'</span><span style="color: #080;">&#41;</span>;
jProgressBar = javacomponent<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javax.swing.JProgressBar'</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">180</span>,<span style="color: #33f;">115</span>,<span style="color: #33f;">310</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, hFigBlurTemp<span style="color: #080;">&#41;</span>;
jProgressBar.<span style="">setValue</span><span style="color: #080;">&#40;</span><span style="color: #33f;">67</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hFigBlurTemp, <span style="color:#A020F0;">'string'</span>,<span style="color:#A020F0;">'Cancel'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">280</span>,<span style="color: #33f;">90</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'Callback'</span>,<span style="color:#A020F0;">'close(gcbf)'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 410px"><img
alt="Non-blurred controls displayed over a blurred figure window" src="http://UndocumentedMatlab.com/images/blurFigure.png" title="Non-blurred controls displayed over a blurred figure window" width="400" /><p
class="wp-caption-text">Non-blurred controls displayed over a blurred figure window</p></div></center></p><p>Do you have some other interesting uses for window transparency in Matlab? If so, please share your thought in a <a
href="http://undocumentedmatlab.com/blog/blurred-matlab-figure-window/#respond">comment</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/transparent-matlab-figure-window/' rel='bookmark' title='Transparent Matlab figure window'>Transparent Matlab figure window</a> <small>Matlab figure windows can be made fully or partially transparent/translucent or blurred - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/minimize-maximize-figure-window/' rel='bookmark' title='Minimize/maximize figure window'>Minimize/maximize figure window</a> <small>Matlab figure windows can easily be maximized, minimized and restored using a bit of undocumented magic powder...</small></li><li><a
href='http://undocumentedmatlab.com/blog/disable-entire-figure-window/' rel='bookmark' title='Enable/disable entire figure window'>Enable/disable entire figure window</a> <small>Disabling/enabling an entire figure window is impossible with pure Matlab, but is very simple using the underlying Java. 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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/blurred-matlab-figure-window/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>UDD and Java</title><link>http://undocumentedmatlab.com/blog/udd-and-java/</link> <comments>http://undocumentedmatlab.com/blog/udd-and-java/#comments</comments> <pubDate>Wed, 23 Mar 2011 21:04:35 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Donn Shull]]></category> <category><![CDATA[Listener]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2213</guid> <description><![CDATA[UDD provides built-in convenience methods to facilitate the integration of Matlab UDD objects with Java code - this article explains how<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/' rel='bookmark' title='Matlab callbacks for Java events'>Matlab callbacks for Java events</a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/jmi-java-to-matlab-interface/' rel='bookmark' title='JMI &#8211; Java-to-Matlab Interface'>JMI &#8211; Java-to-Matlab Interface</a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/extending-a-java-class-with-udd/' rel='bookmark' title='Extending a Java class with UDD'>Extending a Java class with UDD</a> <small>Java classes can easily be extended in Matlab, using pure Matlab code. ...</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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Once again I welcome Donn Shull, who concludes his series on Matlab&#8217;s undocumented UDD mechanism with a discussion of the UDD-Java relationship.</i></p><h3 id="Introduction">Introduction to the UDD-Java relationship</h3><p>Over the course of this series we have mentioned connections between UDD and Java. In <a
target="_blank" href="http://undocumentedmatlab.com/blog/udd-events-and-listeners/">UDD Events and Listeners</a> we described how in Matlab each Java object can have a UDD companion. In <a
target="_blank" href="http://undocumentedmatlab.com/blog/hierarchical-systems-with-udd/">Hierarchical Systems with UDD</a> we briefly noted that a UDD hierarchy may be passed to Java. This suggests that there is a two way relationship between between UDD and Java.</p><p>In this article we will use some undocumented built-in methods such as <b><i>java</i></b> and <b><i>classhandle</i></b> to explore the UDD-Java relationship. We have used built-in methods for UDD objects before. We have also mentioned the importance of studying code from The MathWorks. When you come across something that looks like it may be a UDD method you can check with the <b><i>which</i></b> command:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">which</span> java -<span style="color: #0000FF;">all</span>
C<span style="color: #F0F;">:</span>\MATLAB\R2010b\toolbox\matlab\general\java.<span style="">m</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% javahandle.com.mathworks.hg.peer.Echo method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% ui.figure method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% hg2utils.HGHandle method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% JavaVisible method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% hg.figure method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% hg.GObject method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% schema.class method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% handle.handle method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% schema.method method</span>
C<span style="color: #F0F;">:</span>\MATLAB\R2010b\toolbox\rptgen\rptgen\@sgmltag\java.<span style="">m</span>  <span style="color: #228B22;">% sgmltag method</span></pre></div></div><p>If you find <code>schema.class</code> in the comments for built-in methods, then the method is a general UDD method.</p><h3 id="javahandle">UDD javahandle companions for Java object</h3><p>Whenever a Java class is instantiated in Matlab, it is possible to <a
target="_blank" href="http://undocumentedmatlab.com/blog/udd-events-and-listeners/#Java">create a companion UDD object</a>. The created companion can be in either the <code>javahandle</code> or the <code>javahandle_withcallbacks</code> package. The primary reason for creating the companion object is to avoid <a
rel="nofollow" target="_blank" href="http://mathforum.org/kb/message.jspa?messageID=5950839">memory leaks</a> when attaching a Matlab callback to a callback property. It makes sense in general to use the <code>javahandle_withcallbacks</code> package.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #228B22;">% creage a java instance and the companion UDD object</span>
&gt;&gt; javaFrame = javax.<span style="">swing</span>.<span style="">JFrame</span>;
&nbsp;
&gt;&gt; <span style="color: #228B22;">% dot notation</span>
&gt;&gt; javaFrameUDD = javaFrame.<span style="">handle</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;
&nbsp;
&gt;&gt; <span style="color: #228B22;">% or Matlab notation</span>
&gt;&gt; javaFrameUDD = handle<span style="color: #080;">&#40;</span>javaFrame,<span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>We can use the built-in <b><i>classhandle</i></b> method to inspect our UDD companion object. This can be used, for example, to obtain a list of the events that the Java class generates:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #228B22;">% use classhandle to list a java classes events</span>
&gt;&gt; jch = javaFrame.<span style="">handle</span>.<span style="">classhandle</span>;
&nbsp;
&gt;&gt; <span style="color: #0000FF;">for</span> index = <span style="color: #33f;">1</span><span style="color: #F0F;">:</span>numel<span style="color: #080;">&#40;</span>jch.<span style="">Events</span><span style="color: #080;">&#41;</span>, <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span>jch.<span style="">Events</span><span style="color: #080;">&#40;</span>index<span style="color: #080;">&#41;</span>.<span style="">Name</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">end</span>
MouseWheelMoved
MouseClicked
MouseEntered
MouseExited
MousePressed
MouseReleased
WindowGainedFocus
WindowLostFocus
WindowActivated
WindowClosed
WindowClosing
WindowDeactivated
WindowDeiconified
WindowIconified
WindowOpened
ComponentHidden
ComponentMoved
ComponentResized
ComponentShown
MouseDragged
MouseMoved
ComponentAdded
ComponentRemoved
AncestorMoved
AncestorResized
FocusGained
FocusLost
WindowStateChanged
HierarchyChanged
CaretPositionChanged
InputMethodTextChanged
PropertyChange
KeyPressed
KeyReleased
KeyTyped
&nbsp;
&gt;&gt; <span style="color: #228B22;">% Use one of the object's callbacks</span>
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>javaFrameUDD,<span style="color:#A020F0;">'WindowGainedFocusCallback'</span>,@myCallbackFcn<span style="color: #080;">&#41;</span>;</pre></div></div><p>If we do not wish to use callback properties, then we can create our UDD companion in the <code>javahandle</code> package and use <b><i>handle.listener</i></b> to respond to events.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">javaFrame = javax.<span style="">swing</span>.<span style="">JFrame</span>;
javaFrameUDD = javaFrame.<span style="">handle</span>;
lis = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>javaFrameUDD,<span style="color:#A020F0;">'WindowGainedFocus'</span>,@myCallbackFcn<span style="color: #080;">&#41;</span>;</pre></div></div><h3 id="Passing">Passing UDD objects to Java code</h3><p>You can pass any UDD object to your Java classes in Matlab. Matlab will create a Java bean adapter for the UDD object. The bean adapter created is a subclass of <code>com.MathWorks.jmi.bean.UDDObject</code>. <code>UDDObject</code> implements the Java interfaces <code>com.MathWorks.jmi.bean.DynamicProperties</code>, <code>com.MathWorks.jmi.bean.MTObject</code>, <code>com.MathWorks.jmi.bean.TreeObject</code>, and <code>com.mathworks.services.Browseable</code>.</p><p>The generated bean adapter will have the methods of the parent class the methods of the UDD class, as well as <i>set</i> and <i>get</i> methods for the class properties. To understand how this works, let&#8217;s start with our <code>simple.object</code> and use the <b><i>java</i></b> method to inspect the bean adapter:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myObj = simple.<span style="">object</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'myObj'</span>, <span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;
&nbsp;
&gt;&gt; <span style="color: #228B22;">% using dot notation with the java method </span>
&gt;&gt; myObj.<span style="">java</span>.<span style="">getClass</span>
<span style="color: #0000FF;">ans</span> =
<span style="color: #0000FF;">class</span> objectBeanAdapter0
&nbsp;
&gt;&gt; myObj.<span style="">java</span>.<span style="color: #0000FF;">methods</span>
&nbsp;
<span style="color: #0000FF;">Methods</span> <span style="color: #0000FF;">for</span> <span style="color: #0000FF;">class</span> objectBeanAdapter0<span style="color: #F0F;">:</span>
&nbsp;
acquireReference                    createNullMatlabObjectListener      lastDown                            
addBelow                            createNullPropertyChangeListener    left                                
addBrowseableListener               <span style="color: #0000FF;">dialog</span>                              notify                              
addFirstBelow                       <span style="color: #0000FF;">disp</span>                                notifyAll                           
addLeft                             dispose                             objectBeanAdapter0                  
addMatlabObjectListener             equals                              releaseReference                    
addObjectPropertyChangeListener     findProperty                        removeBrowseableListener            
addRight                            firstDown                           removeMatlabObjectListener          
browseableCanHaveChildren           getChildAt                          removeObjectPropertyChangeListener  
browseableChild                     getChildCount                       right                               
browseableChildCount                getClass                            setDirtyFlag                        
browseableChildFetchCount           getClassName                        setDynamicPropertyValue             
browseableChildren                  getDynamicProperties                setName                             
browseableDataObject                getDynamicPropertyValue             setPropertyValue                    
browseableDisplayObject             getIndex                            setThreadSafetyCheckLevel           
browseableHasChildren               getIndexOfChild                     setValue                            
browseableNChildren                 getName                             toString                            
browseableNextNSiblings             getNewInstance                      up                                  
browseableNextSibling               getPropertyValue                    updateCache                         
browseableParent                    getValue                            updateChildCount                    
browseablePrevNSiblings             hashCode                            updateIndex                         
browseablePrevSibling               isDirty                             wait                                
checkThreadSafety                   isLeaf                              
clearDirtyFlag                      isObservable                        
compareTo                           isValid</pre></div></div><p>The parent class has added a large number of methods to the bean adapter for our original class. By looking at the list we can see our <i>dialog</i> and <i>disp</i> methods. There are also <i>getName</i>, <i>setName</i>, <i>getValue</i>, and <i>setValue</i> methods for our classes properties. The rest of the methods were inherited from the base <code>UDDObject</code> superclass. We can use any superclass method directly with the bean adapter object. For example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myObj.<span style="">java</span>.<span style="">getPropertyValue</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Name'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
myObj</pre></div></div><h3 id="Interface">Java interface class</h3><p>To be able to use our UDD object in user-written Java code, we need a Java interface class for it. While we could manually write an interface file, UDD provides a very handy convenience method to automatically create the interface file. For this, we use the <b><i>classhandle</i></b> method again. The <code>schema.class</code> object obtained using <b><i>classhandle</i></b> has a method called <i>createJavaInterface</i> that takes two string arguments: the Java interface classname, and the folder in which to place the interface file. The steps to create and use this interface file are:</p><ol><li>Create and test your UDD class</li><li>Create a Java interface file using <code>schema.class</code>&#8216;s <i>CreateJavaInterface</i></li><li>Modify your UDD class definition file (schema.m) to reference the Java interface file</li><li>Create the Java code that uses your class</li></ol><p>For example, to create a Java interface file for the simple object we created above, use the following commands in Matlab:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">classH = classhandle<span style="color: #080;">&#40;</span>myObj<span style="color: #080;">&#41;</span>;
classH.<span style="">createJavaInterface</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'simpleObjectInterface'</span>,<span style="color: #0000FF;">pwd</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>This will create the following simpleObjectInterface.java file in the current working directory:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> simpleObjectInterface 
       <span style="color: #000000; font-weight: bold;">extends</span> com.<span style="color: #006633;">mathworks</span>.<span style="color: #006633;">jmi</span>.<span style="color: #006633;">bean</span>.<span style="color: #006633;">TreeObject</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/* Properties */</span>
    <span style="color: #000000; font-weight: bold;">public</span> java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> getValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setValue<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/* Methods */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> dialog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> disp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>The interface file contains <i>set</i> and <i>get</i> accessor methods for our UDD object properties, and Java prototypes for the UDD methods (in our case, <i>dialog</i> and <i>disp</i>).</p><p>The next step is to modify our class definition file (schema.m) to reference the Java interface file we have created. This modification provides the information that Matlab needs to create the bean adapter that implements the Java interface:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">simpleClass = schema.<span style="color: #0000FF;">class</span><span style="color: #080;">&#40;</span>simplePackage, <span style="color:#A020F0;">'object'</span><span style="color: #080;">&#41;</span>;
simpleClass.<span style="">JavaInterfaces</span> = <span style="color: #080;">&#123;</span> <span style="color:#A020F0;">'simpleObjectInterface'</span> <span style="color: #080;">&#125;</span>;</pre></div></div><p>We can verify that the generated bean adapter implements the interface using Java Reflection techniques. As always when we have made changes to the class definition file, we need to use the <b><i>clear classes</i></b> command, and then recreate our objects:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myObj = simple.<span style="">object</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'myObj'</span>, <span style="color: #0000FF;">pi</span><span style="color: #080;">&#41;</span>
myObj =
  Name<span style="color: #F0F;">:</span> myObj
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">3.141593</span>
&nbsp;
&gt;&gt; myObjBean = java<span style="color: #080;">&#40;</span>myObj<span style="color: #080;">&#41;</span> 
myObjBean =
  Name<span style="color: #F0F;">:</span> myObj
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">3.141593</span>
&nbsp;
&gt;&gt; interfaces = myObjBean.<span style="">getClass</span>.<span style="">getInterfaces</span> 
interfaces =
java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #F0F;">:</span>
    <span style="color: #080;">&#91;</span>java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span><span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; interfaces<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> 
<span style="color: #0000FF;">ans</span> =
interface simpleObjectInterface</pre></div></div><h3 id="Usage">Using UDD in Java</h3><p>Let&#8217;s create a simple Java class that illustrates passing a UDD object to Java. Here we will just have two methods: The first gets the Value property from a class instance and doubles it; the second launches the class instance dialog:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> accessUDDClass 
<span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">double</span> localValue<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> accessUDDClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doubleValue<span style="color: #009900;">&#40;</span>simpleObjectInterface UDDObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    localValue <span style="color: #339933;">=</span> UDDObj.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    UDDObj.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">*</span>localValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> launchDialog<span style="color: #009900;">&#40;</span>simpleObjectInterface UDDObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    UDDObj.<span style="color: #006633;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>If we have set up the Java compiler and environment variables correctly, we can compile our interface and Java class files from inside Matlab using the system command (alternately, we can compile using any external Java compiler or IDE):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; system<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javac accessUDDClass.java simpleObjectInterface.java'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
     <span style="color: #33f;">0</span></pre></div></div><p>Now test our simple Java class with the UDD object created earlier:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; javaObj = accessUDDClass
javaObj =
accessUDDClass@eb9b73
&nbsp;
&gt;&gt; javaObj.<span style="">doubleValue</span><span style="color: #080;">&#40;</span>myObj<span style="color: #080;">&#41;</span>  <span style="color: #228B22;">% pi =&gt; 2*pi</span>
&nbsp;
&gt;&gt; myObj
myObj =
  Name<span style="color: #F0F;">:</span> myObj
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">6.283185</span></pre></div></div><p>This concludes the UDD series. I would like to thank Yair for his help in preparing and presenting this information.</p><h3 id="Editor">Editor&#8217;s note</h3><p><i>I would like to thank Donn for his enourmously detailed work on UDD, and for preparing it in easy-to-follow articles. I can personally attest to the huge time investment it has taken him. I trully believe he deserves a warm &#8220;thank you&#8221; from the Matlab community. Please visit <a
target="_blank" rel="nofollow" href="http://aetoolbox.com/default.aspx">Donn&#8217;s website</a>, or add a short <a
href="http://UndocumentedMatlab.com/blog/udd-and-java/#respond">comment</a> below.</p><p>In the following weeks, I return to the regular stuff that made this website famous: solving day-to-day Matlab problems using simple undocumented built-in Matlab gems.<br/>- Yair</i></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/' rel='bookmark' title='Matlab callbacks for Java events'>Matlab callbacks for Java events</a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/jmi-java-to-matlab-interface/' rel='bookmark' title='JMI &#8211; Java-to-Matlab Interface'>JMI &#8211; Java-to-Matlab Interface</a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/extending-a-java-class-with-udd/' rel='bookmark' title='Extending a Java class with UDD'>Extending a Java class with UDD</a> <small>Java classes can easily be extended in Matlab, using pure Matlab code. ...</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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/udd-and-java/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>UDD Events and Listeners</title><link>http://undocumentedmatlab.com/blog/udd-events-and-listeners/</link> <comments>http://undocumentedmatlab.com/blog/udd-events-and-listeners/#comments</comments> <pubDate>Wed, 16 Mar 2011 18:43:16 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Donn Shull]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listener]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[schema]]></category> <category><![CDATA[schema.prop]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2200</guid> <description><![CDATA[UDD event listeners can be used to listen to property value changes and other important events of Matlab objects<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/' rel='bookmark' title='Matlab callbacks for Java events'>Matlab callbacks for Java events</a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/detecting-window-focus-events/' rel='bookmark' title='Detecting window focus events'>Detecting window focus events</a> <small>Matlab does not have any documented method to detect window focus events (gain/loss). This article describes an undocumented way to detect such events....</small></li><li><a
href='http://undocumentedmatlab.com/blog/introduction-to-udd/' rel='bookmark' title='Introduction to UDD'>Introduction to UDD</a> <small>UDD classes underlie many of Matlab's handle-graphics objects and functionality. This article introduces these classes....</small></li><li><a
href='http://undocumentedmatlab.com/blog/creating-a-simple-udd-class/' rel='bookmark' title='Creating a simple UDD class'>Creating a simple UDD class</a> <small>This article explains how to create and test custom UDD packages, classes and objects...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Donn Shull continues his exploration of the undocumented UDD mechanism, today discussing the important and extremely useful topic of UDD events</i></p><h3 id="model">The UDD event model</h3><p>The UDD event model is very similar to the MCOS event model. There is an excellent <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/matlab_oop/bqvggvt.html">discussion</a> of the MCOS event model in Matlab&#8217;s official documentation. Most of the MCOS information also applies to UDD if you make the following substitutions:</p><table><tbody><tr><th
bgcolor="#D0D0D0">MCOS Event Model</th><th
bgcolor="#D0D0D0">UDD Event Model</th></tr><tr><td
bgcolor="#E7E7E7">notify</td><td
bgcolor="#E7E7E7">send</td></tr><tr><td
bgcolor="#E7E7E7">event.EventData</td><td
bgcolor="#E7E7E7">handle.EventData</td></tr><tr><td
bgcolor="#E7E7E7">events block</td><td
bgcolor="#E7E7E7">schema.event</td></tr><tr><td
bgcolor="#E7E7E7">event.listener</td><td
bgcolor="#E7E7E7">handle.listener</td></tr><tr><td
bgcolor="#E7E7E7">PreGet, PreSet</td><td
bgcolor="#E7E7E7">PropertyPreGet, PropertPreSet</td></tr><tr><td
bgcolor="#E7E7E7">PostGet, PostSet</td><td
bgcolor="#E7E7E7">PropertyPostGet, PropertyPostSet</td></tr></tbody></table><h3 id="handler">Event handler functions</h3><p>To begin the UDD event model discussion we will start at the end, with the event handler. The event handler function requires at least two input arguments: the source object which triggered the event, and an object of type <code>handle.EventData</code> or a subclass of <code>handle.EventData</code>.</p><p>To demonstrate how this works, let&#8217;s write a simple event handler function. This event handler will display the class of the source event and the class of the event data:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> displayEventInfo<span style="color: #080;">&#40;</span>source, eventData<span style="color: #080;">&#41;</span>
<span style="color: #228B22;">%DISPLAYEVENTINFO display the classes of source, data objects</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   DISPLAYEVENTINFO(SOURCE, EVENTDATA) returns the classes</span>
<span style="color: #228B22;">%   of the source object and the event data object</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   INPUTS:</span>
<span style="color: #228B22;">%       SOURCE    : the event source</span>
<span style="color: #228B22;">%       EVENTDATA : the event data</span>
  <span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>source<span style="color: #080;">&#41;</span>
    <span style="color: #0000FF;">fprintf</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>, <span style="color:#A020F0;">'The source object class is: %s'</span>,<span style="color: #0000FF;">class</span><span style="color: #080;">&#40;</span>source<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>eventData<span style="color: #080;">&#41;</span>
    <span style="color: #0000FF;">fprintf</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>, <span style="color:#A020F0;">'The event data class is: %s'</span>,<span style="color: #0000FF;">class</span><span style="color: #080;">&#40;</span>eventData<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span></pre></div></div><h3 id="listener">Creating a listener</h3><p>In the section on <a
target="_blank" href="http://undocumentedmatlab.com/blog/creating-a-simple-udd-class/">Creating a Simple UDD Class</a> we used <code>schema.event</code> in our <code>simple.object</code> class definition file to create a <code>simpleEvent</code> event. We now create an instance of <code>simple.object</code>, then use <b><i>handle.listener</i></b> to wait (&#8220;listen&#8221;) for the <code>simpleEvent</code> event to occur and call the <i>displayEventInfo</i> event handler function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">a = simple.<span style="">object</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'a'</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>a,<span style="color:#A020F0;">'simpleEvent'</span>,@displayEventInfo<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">setappdata</span><span style="color: #080;">&#40;</span>a, <span style="color:#A020F0;">'listeners'</span>, hListener<span style="color: #080;">&#41;</span>;</pre></div></div><p><b><u>Important:</u></b> The <code>hListener</code> handle must remain stored somewhere in Matlab memory, or the listener will not be used. For this reason, it is good practice to attach the listener handle to the listened object, using the <i><b>setappdata</b></i> function, as was done above. The listener will then be alive for exactly as long as its target object is alive.</p><h3 id="EventData">Creating an EventData object</h3><p>Next, create the <code>handle.EventData</code> object. The <code>handle.EventData</code> object constructor requires two arguments: an instance of the events source object, and the name of the event:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">evtData = handle.<span style="">EventData</span><span style="color: #080;">&#40;</span>a, <span style="color:#A020F0;">'simpleEvent'</span><span style="color: #080;">&#41;</span></pre></div></div><h3 id="event">Generating an event</h3><p>The last step is actually triggering an event. This is done by issuing the <i><b>send</b></i> command for the specified object, event name and event data:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; a.<span style="">send</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'simpleEvent'</span>, evtData<span style="color: #080;">&#41;</span>
The source object <span style="color: #0000FF;">class</span> <span style="color: #0000FF;">is</span><span style="color: #F0F;">:</span> simple.<span style="">object</span>
The event data <span style="color: #0000FF;">class</span> <span style="color: #0000FF;">is</span><span style="color: #F0F;">:</span> handle.<span style="">EventData</span></pre></div></div><p>If there is other information that you wish to pass to the callback function you can create a subclass of the <code>handle.EventData</code>. Add properties to hold your additional information and use your subclass as the second argument of the <i><b>send</b></i> method.</p><h3 id="builtin">Builtin UDD events</h3><p>The builtin <code>handle</code> package has six event data classes which are subclasses of the base <code>handle.EventData</code> class. Each of these classes is paired with specific UDD events that Matlab generates. Actions that trigger these events include creating/destroying an object, adding/removing objects from a hierarchy, and getting/setting property values. The following table lists the event names and <code>handle.*EventData</code> data types returned for these events:</p><table><tbody><tr><th
bgcolor="#D0D0D0">event data type</th><th
bgcolor="#D0D0D0">event trigger</th></tr><tr><td
bgcolor="#E7E7E7">handle.ClassEventData</td><td
bgcolor="#E7E7E7">ClassInstanceCreated</td></tr><tr><td
bgcolor="#E7E7E7">handle.EventData</td><td
bgcolor="#E7E7E7">ObjectBeingDestroyed</td></tr><tr><td
bgcolor="#E7E7E7">handle.ChildEventData</td><td
bgcolor="#E7E7E7">ObjectChildAdded, ObjectChildRemoved</td></tr><tr><td
bgcolor="#E7E7E7">handle.ParentEventData</td><td
bgcolor="#E7E7E7">ObjectParentChanged</td></tr><tr><td
bgcolor="#E7E7E7">handle.PropertyEventData</td><td
bgcolor="#E7E7E7">PropertyPreGet, PropertyPostGet</td></tr><tr><td
bgcolor="#E7E7E7">handle.PropertySetEventData</td><td
bgcolor="#E7E7E7">PropertyPreSet, PropertyPostSet</td></tr></tbody></table><p>As an example of some of these events let&#8217;s look at a <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/303232">question</a> recently asked on the CSSM newsgroup. The basic idea is that we want to monitor an axis, automatically make any added lines to be green in color, and prevent patches from being added.</p><p>The solution is to monitor the <code>ObjectChildAdded</code> event for an axis. We will write an event handler which checks the <code>handle.ChildEventData</code> to see what type of child was added. In the case of lines we will set their color to green; patch objects will be deleted from the axis. Here is our event handler function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> modifyAxesChildren<span style="color: #080;">&#40;</span>~, eventData<span style="color: #080;">&#41;</span>
<span style="color: #228B22;">%MODIFYAXESCHILDREN monitor and axis and modify added children</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   MODIFYAXESCHILDREN(SOURCE,EVENTDATA) is an event handler to</span>
<span style="color: #228B22;">%   change newly-added lines to green and remove added patches</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">%   INPUTS:</span>
<span style="color: #228B22;">%       EVENTDATA : handle.ChildEventData object</span>
   <span style="color: #0000FF;">switch</span> eventData.<span style="">Child</span>.<span style="">classhandle</span>.<span style="">Name</span>
      <span style="color: #0000FF;">case</span> <span style="color:#A020F0;">'line'</span>
         eventData.<span style="">Child</span>.<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Color'</span>, <span style="color:#A020F0;">'green'</span><span style="color: #080;">&#41;</span>;
         <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Color changed to green.'</span><span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">case</span> <span style="color:#A020F0;">'patch'</span>
         eventData.<span style="">Child</span>.<span style="color: #0000FF;">delete</span>;
         <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Patch removed.'</span><span style="color: #080;">&#41;</span>
   <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span></pre></div></div><p>Next create an axis, and a listener which is triggered when children are added:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% create a new axes and get its handle</span>
a = hg.<span style="color: #0000FF;">axes</span>;
&nbsp;
<span style="color: #228B22;">% create the listener</span>
listen = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>a, <span style="color:#A020F0;">'ObjectChildAdded'</span>, @modifyAxesChildren<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% add a line</span>
&gt;&gt; hg.<span style="color: #0000FF;">line</span>;
Color changed to green.
&nbsp;
<span style="color: #228B22;">% try to add a patch</span>
&gt;&gt; hg.<span style="color: #0000FF;">patch</span>;
<span style="color: #0000FF;">Patch</span> removed.</pre></div></div><p>Removing a child with either the <i>delete</i> or the <i>disconnect</i> method generates an <code>ObjectChildRemoved</code> event. The <i>delete</i> method also generates the <code>ObjectBeingDestroyed</code> event. Changing a child&#8217;s parent with the <i>up</i> method generates an <code>ObjectParentChanged</code> event.</p><p>Reading an object&#8217;s properties with either dot notation or with the <i>get</i> method generates <code>PropertyPreGet</code> and <code>PropertyPostGet</code> events.</p><p>Changing the value of a property generates the <code>PropertyPreSet</code> and <code>PropertyPostSet</code> events. As we saw in the section on <a
target="_Blank" href="http://undocumentedmatlab.com/blog/udd-properties/">UDD properties</a>, when the <b>AbortSet</b> access flag is &#8216;on&#8217;, property set events are only generated when a <i><b>set</b></i> operation actually changes the value of the property (as opposed to leaving it unchanged).</p><p>Note that the <b><i>handle.listener</i></b> syntax is slightly different for property events:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hProp = <span style="color: #0000FF;">findprop</span><span style="color: #080;">&#40;</span>a, <span style="color:#A020F0;">'Value'</span><span style="color: #080;">&#41;</span>;
hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>a,hProp,<span style="color:#A020F0;">'PropertyPreGet'</span>,@displayEventInfo<span style="color: #080;">&#41;</span>;</pre></div></div><h3 id="Java">Java events</h3><p>The final specialized event data object in the handle package is <code>handle.JavaEventData</code>. In Matlab, Java classes are not UDD classes, but each Java instance can have a UDD <i>peer</i>. The peer is created using the <i><b>handle</b></i> function. The Java peers are created in either UDD&#8217;s <code>javahandle</code> package or the <code>javahandle_withcallbacks</code> package. As their names imply, the latter enables listening to Java-triggered events using a Matlab callback.</p><p>To illustrate how this works we will create a Java Swing <code>JFrame</code> and listen for <code>MouseClicked</code> events:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Create the Java Frame</span>
javaFrame = javax.<span style="">swing</span>.<span style="">JFrame</span>;
javaFrame.<span style="">setSize</span><span style="color: #080;">&#40;</span><span style="color: #33f;">200</span>, <span style="color: #33f;">200</span><span style="color: #080;">&#41;</span>;
javaFrame.<span style="">show</span>;
&nbsp;
<span style="color: #228B22;">% Create a UDD peer for the new JFrame (two alternatives)</span>
javaFramePeer = javaFrame.<span style="">handle</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% alternative #1</span>
javaFramePeer = handle<span style="color: #080;">&#40;</span>javaFrame, <span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% alternative #2</span>
&nbsp;
<span style="color: #228B22;">% Create the a listener for the Java MouseClicked event</span>
listen = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>javaFramePeer, <span style="color:#A020F0;">'MouseClicked'</span>, @displayEventInfo<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="a simple Java Swing JFrame" src="http://UndocumentedMatlab.com/images/UDD_Java_Frame.png" title="a simple Java Swing JFrame" width="200" height="200" /><p
class="wp-caption-text">a simple Java Swing JFrame</p></div></center></p><p>When we click on the JFrame, our UDD peer triggers the callback:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;">The source object <span style="color: #000000; font-weight: bold;">class</span> is<span style="color: #339933;">:</span> javahandle_withcallbacks.<span style="color: #006633;">javax</span>.<span style="color: #006633;">swing</span>.<span style="color: #003399;">JFrame</span>
The event data <span style="color: #000000; font-weight: bold;">class</span> is<span style="color: #339933;">:</span> handle.<span style="color: #006633;">JavaEventData</span></pre></div></div><p>Since we created our peer in the <code>javahandle_withcallbacks</code> package, it is not necessary to create a listener using <i><b>handle.listener</b></i>. If we place our callback function handle in the <b>MouseClickedCallback</b> property it will be executed whenever the <code>MouseClicked</code> event is triggered. Such <b>*Callback</b> properties are automatically generated by Matlab when it creates the UDD peer (<a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/">details</a>).</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">clear</span> listen
javaFramePeer.<span style="">MouseClickedCallback</span> = @displayEventInfo</pre></div></div><p>This will work the same as before without the need to create and maintain a <b><i>handle.listener</i></b> object. If we had created our UDD peer in the <code>javahandle</code> package rather than <code>javahandle_withcallbacks</code>, we would not have the convenience of the <b>MouseClickedCallback</b> property, but we could still use the <b><i>handle.listener</i></b> mechanism to monitor events.</p><h3 id="custom">Creating callback properties for custom UDD classes</h3><p>It is easy to add callback properties to user created UDD objects. The technique involves embedding a <code>handle.listener</code> object in the UDD object. To illustrate this, we add a <b>SimpleEventCallback</b> property to our <code>simple.object</code>, then use a <b>SimpleEventListener</b> property to hold our embedded <b><i>handle.listener</i></b>. Add the following to <code>simple.object</code>&#8216;s schema.m definition file:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">   <span style="color: #228B22;">% Property to hold our callback handle</span>
   prop = schema.<span style="">prop</span><span style="color: #080;">&#40;</span>simpleClass, <span style="color:#A020F0;">'SimpleEventCallback'</span>, <span style="color:#A020F0;">'MATLAB callback'</span><span style="color: #080;">&#41;</span>;
   prop.<span style="">setFunction</span> = @setValue;
&nbsp;
   <span style="color: #228B22;">% hidden property to hold the listener for our callback</span>
   prop = schema.<span style="">prop</span><span style="color: #080;">&#40;</span>simpleClass, <span style="color:#A020F0;">'SimpleEventListener'</span>, <span style="color:#A020F0;">'handle'</span><span style="color: #080;">&#41;</span>;
   prop.<span style="">Visible</span> = <span style="color:#A020F0;">'off'</span>;
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #0000FF;">function</span> propVal = setValue<span style="color: #080;">&#40;</span>self, value<span style="color: #080;">&#41;</span>
   <span style="color: #228B22;">%SETVALUE function to transfer function handle from callback property to listener</span>
   self.<span style="">SimpleEventListener</span>.<span style="">Callback</span> = value;
   propVal = value;
<span style="color: #0000FF;">end</span></pre></div></div><p>Next we add the following to our simple.object constructor file:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% set the hidden listener property to a handle.listener</span>
simpleObject.<span style="">SimpleEventListener</span> = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>simpleObject, <span style="color:#A020F0;">'simpleEvent'</span>, <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Now if we set the <b>SimpleObjectCallback</b> property to a function handle, the handle is transferred to the embedded <b><i>handle.listener</i></b> Callback property. When a <code>simpleEvent</code> event is generated, our <code>SimpleEventCallback</code> function will be executed.</p><p>This series will conclude next week with a look at the special relationship between UDD and Java.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/' rel='bookmark' title='Matlab callbacks for Java events'>Matlab callbacks for Java events</a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/detecting-window-focus-events/' rel='bookmark' title='Detecting window focus events'>Detecting window focus events</a> <small>Matlab does not have any documented method to detect window focus events (gain/loss). This article describes an undocumented way to detect such events....</small></li><li><a
href='http://undocumentedmatlab.com/blog/introduction-to-udd/' rel='bookmark' title='Introduction to UDD'>Introduction to UDD</a> <small>UDD classes underlie many of Matlab's handle-graphics objects and functionality. This article introduces these classes....</small></li><li><a
href='http://undocumentedmatlab.com/blog/creating-a-simple-udd-class/' rel='bookmark' title='Creating a simple UDD class'>Creating a simple UDD class</a> <small>This article explains how to create and test custom UDD packages, classes and objects...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/udd-events-and-listeners/feed/</wfw:commentRss> <slash:comments>10</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>2</slash:comments> </item> <item><title>Matlab callbacks for Java events</title><link>http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/</link> <comments>http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/#comments</comments> <pubDate>Tue, 30 Nov 2010 22:09:35 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[Listener]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1987</guid> <description><![CDATA[Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/udd-events-and-listeners/' rel='bookmark' title='UDD Events and Listeners'>UDD Events and Listeners</a> <small>UDD event listeners can be used to listen to property value changes and other important events of Matlab objects...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool-callbacks/' rel='bookmark' title='uisplittool &amp; uitogglesplittool callbacks'>uisplittool &#038; uitogglesplittool callbacks</a> <small>Matlab's undocumented uisplittool and uitogglesplittool are powerful toolbar controls - this article explains how to customize their behavior...</small></li><li><a
href='http://undocumentedmatlab.com/blog/udd-and-java/' rel='bookmark' title='UDD and Java'>UDD and Java</a> <small>UDD provides built-in convenience methods to facilitate the integration of Matlab UDD objects with Java code - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A few days ago, a user <a
target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/4291769/how-do-i-notify-a-matlab-object-of-an-event-in-a-java-object">posted a question</a> on StackOverflow asking whether it is possible to trap a Java-based event in a Matlab callback.</p><p>It so happens that only a few weeks ago I completed a consulting project which required exactly this. The project was to integrate a Matlab computational engine with a Java interface to <a
target="_blank" rel="nofollow" href="http://www.interactivebrokers.com/">Interactive Brokers</a> (IB) &#8211; a well-known online brokerage firm. The idea was to use the Java interface to fetch real-time data about securities (stocks, bonds, options etc.), use a Matlab processing utility, then use the Java interface again to send Buy or Sell orders back to IB.</p><p>If you are interested in the final result (i.e., a complete and field-tested Matlab-IB interface), <a
target="_blank" href="http://undocumentedmatlab.com/ib-matlab/">look here</a>.</p><h3 id="challenge">The challenge</h3><p>A big challenge in this project (aside from handling quite a few IB interface quirks), was to propagate events from the Java interface to the Matlab application. This had to be done asynchronously, since events such as order execution can occur at any time following the order placement. Moreover, even simple requests such as retrieving security information (bid/ask prices for example) is handled by IB via Java events, not as simple function return values.</p><p>Handling Java-based events in Matlab is not a trivial task. Not only merely undocumented, but it is also not intuitive. I have spent quite a few hours trying to crack this issue. In fact, I believe it was one of my more challenging tasks in figuring out the undocumented aspects of the Matlab-Java interface. Few other challenges were as difficult, yet with a happy ending (Drag &#038; Drop is a similar issue &#8211; I will describe it in another article sometime).</p><h3 id="solution">The solution</h3><p>Fast-forward all the fruitless attempted variations, here is the bottom line. Refer to the following simple Java class example:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EventTest
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Vector</span> data <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Vector</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> addMyTestListener<span style="color: #009900;">&#40;</span>MyTestListener lis<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        data.<span style="color: #006633;">addElement</span><span style="color: #009900;">&#40;</span>lis<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> removeMyTestListener<span style="color: #009900;">&#40;</span>MyTestListener lis<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        data.<span style="color: #006633;">removeElement</span><span style="color: #009900;">&#40;</span>lis<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> MyTestListener <span style="color: #000000; font-weight: bold;">extends</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">EventListener</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">void</span> testEvent<span style="color: #009900;">&#40;</span>MyTestEvent event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyTestEvent <span style="color: #000000; font-weight: bold;">extends</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">EventObject</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">float</span> oldValue,newValue<span style="color: #339933;">;</span>        
        MyTestEvent<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> obj, <span style="color: #000066; font-weight: bold;">float</span> oldValue, <span style="color: #000066; font-weight: bold;">float</span> newValue<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">oldValue</span> <span style="color: #339933;">=</span> oldValue<span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">newValue</span> <span style="color: #339933;">=</span> newValue<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> notifyMyTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Vector</span> dataCopy<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">synchronized</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            dataCopy <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Vector</span><span style="color: #009900;">&#41;</span>data.<span style="color: #006633;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>dataCopy.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            MyTestEvent event <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyTestEvent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>MyTestListener<span style="color: #009900;">&#41;</span>dataCopy.<span style="color: #006633;">elementAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">testEvent</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>When compiling EventTest.java, three class files are created: EventTest.class, EventTest$MyTestEvent.class and EventTest$MyTestListener.class. Place them on Matlab&#8217;s Java static classpath, using <i><b>edit</b>(&#8216;classpath.txt&#8217;)</i> (using the dynamic classpath causes many problems that using the static classpath solves). They can now be accessed as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">which</span> EventTest
EventTest <span style="color: #0000FF;">is</span> a Java method  <span style="color: #228B22;">% EventTest constructor</span>
&nbsp;
&gt;&gt; evt = EventTest
evt =
EventTest@16166fc
&nbsp;
&gt;&gt; evt.<span style="color: #0000FF;">get</span>
	<span style="color: #0000FF;">Class</span> = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">1</span> by <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span> array<span style="color: #080;">&#93;</span>
	TestEventCallback = 
	TestEventCallbackData = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
&nbsp;
	BeingDeleted = off
	ButtonDownFcn = 
	Children = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	Clipping = on
	CreateFcn = 
	DeleteFcn = 
	BusyAction = queue
	HandleVisibility = on
	HitTest = on
	Interruptible = on
	Parent = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	Selected = off
	SelectionHighlight = on
	Tag = 
	<span style="color: #0000FF;">Type</span> = EventTest
	<span style="color: #0000FF;">UIContextMenu</span> = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	UserData = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	Visible = on
&nbsp;
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>evt<span style="color: #080;">&#41;</span>
	<span style="color: #0000FF;">Class</span>
	TestEventCallback<span style="color: #F0F;">:</span> string -or- <span style="color: #0000FF;">function</span> handle -or- <span style="color: #0000FF;">cell</span> array
	<span style="color: #F0F;">...</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>evt,<span style="color:#A020F0;">'TestEventCallback'</span>,@<span style="color: #080;">&#40;</span>h,e<span style="color: #080;">&#41;</span><span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span>h<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>evt<span style="color: #080;">&#41;</span>
	<span style="color: #0000FF;">Class</span> = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">1</span> by <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span> array<span style="color: #080;">&#93;</span>
	TestEventCallback = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">1</span> by <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> function_handle array<span style="color: #080;">&#93;</span>   &lt;= ok
	TestEventCallbackData = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	<span style="color: #F0F;">...</span>
&nbsp;
&gt;&gt; evt.<span style="">notifyMyTest</span>   <span style="color: #228B22;">% invoke Java event</span>
              <span style="color: #33f;">0.0009765625</span>   <span style="color: #228B22;">% &lt;= Matlab callback</span></pre></div></div><p>Note how Matlab automatically converted the Java event testEvent, declared in interface MyTestListener, into a Matlab callback TestEventCallback (the first character is always capitalized). All Java events are automatically converted in this fashion, by appending a &#8216;Callback&#8217; suffix. Here is a code snippet from R2008a&#8217;s \toolbox\matlab\uitools\@opaque\addlistener.m that shows this (slightly edited):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hSrc = handle<span style="color: #080;">&#40;</span>jobj,<span style="color:#A020F0;">'callbackproperties'</span><span style="color: #080;">&#41;</span>;
allfields = <span style="color: #0000FF;">sortrows</span><span style="color: #080;">&#40;</span>fields<span style="color: #080;">&#40;</span><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hSrc<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">for</span> <span style="color: #0000FF;"><span style="color: #33f;">i</span></span> = <span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>allfields<span style="color: #080;">&#41;</span>
   fn = allfields<span style="color: #080;">&#123;</span><span style="color: #0000FF;"><span style="color: #33f;">i</span></span><span style="color: #080;">&#125;</span>;
   <span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">findstr</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Callback'</span>,fn<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">strrep</span><span style="color: #080;">&#40;</span>fn,<span style="color:#A020F0;">'Callback'</span>,<span style="color:#A020F0;">''</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>
&nbsp;
callback = @<span style="color: #080;">&#40;</span>o,e<span style="color: #080;">&#41;</span> cbBridge<span style="color: #080;">&#40;</span>o,e,response<span style="color: #080;">&#41;</span>;
hdl = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>jobj<span style="color: #080;">&#41;</span>, eventName, callback<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">function</span> cbBridge<span style="color: #080;">&#40;</span>o,e,response<span style="color: #080;">&#41;</span>
   hgfeval<span style="color: #080;">&#40;</span>response, java<span style="color: #080;">&#40;</span>o<span style="color: #080;">&#41;</span>, e.<span style="">JavaEvent</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">end</span></pre></div></div><p>Note that <i><b>hgfeval</b></i>, which is used within the cbBridge callback function, is a semi-documented pure-Matlab built-in function, which I <a
target="_blank" href="http://UndocumentedMatlab.com/blog/hgfeval/">described</a> a few weeks ago.</p><p>If several events have the same case-insensitive name, then the additional callbacks will have an appended underscore character (e.g., &#8216;TestEventCallback_&#8217;):</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// In the Java class:</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> MyTestListener <span style="color: #000000; font-weight: bold;">extends</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">EventListener</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">void</span> testEvent<span style="color: #009900;">&#40;</span>MyTestEvent e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">void</span> testevent<span style="color: #009900;">&#40;</span>TestEvent2 e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% …and back in Matlab:</span>
&gt;&gt; evt=EventTest; evt.<span style="color: #0000FF;">get</span>
	<span style="color: #0000FF;">Class</span> = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">1</span> by <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span> array<span style="color: #080;">&#93;</span>
	TestEventCallback = 
	TestEventCallbackData = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	TestEventCallback_ = 
	TestEventCallback_Data = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span> 
	<span style="color: #F0F;">...</span></pre></div></div><p>To complete this discussion, it should be noted that Matlab also automatically defines corresponding Events in the Java object’s <i><b>classhandle</b></i>. Unfortunately, <i><b>classhandle</b></i> events are not differentiated in a similar manner – in this case only a single event is created, named Testevent. <i><b>classhandle</b></i> events, and their relationship to the preceding discussion, will be described in Donn Scull&#8217;s upcoming series on UDD.</p><p>An alternative to using callbacks on Java events, as shown above, is to use undocumented <a
target="_blank" href="http://UndocumentedMatlab.com/blog/continuous-slider-callback/#Event_Listener"><i><b>handle.listener</b></i>s</a>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>evt<span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'TestEvent'</span>,callback<span style="color: #080;">&#41;</span>;</pre></div></div><p>There are several other odds and ends, but this article should be sufficient for implementing a fully-functional Java event handling mechanism in Matlab. Good luck!</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/udd-events-and-listeners/' rel='bookmark' title='UDD Events and Listeners'>UDD Events and Listeners</a> <small>UDD event listeners can be used to listen to property value changes and other important events of Matlab objects...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool-callbacks/' rel='bookmark' title='uisplittool &amp; uitogglesplittool callbacks'>uisplittool &#038; uitogglesplittool callbacks</a> <small>Matlab's undocumented uisplittool and uitogglesplittool are powerful toolbar controls - this article explains how to customize their behavior...</small></li><li><a
href='http://undocumentedmatlab.com/blog/udd-and-java/' rel='bookmark' title='UDD and Java'>UDD and Java</a> <small>UDD provides built-in convenience methods to facilitate the integration of Matlab UDD objects with Java code - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/feed/</wfw:commentRss> <slash:comments>27</slash:comments> </item> <item><title>Inactive Control Tooltips &amp; Event Chaining</title><link>http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/</link> <comments>http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/#comments</comments> <pubDate>Wed, 24 Feb 2010 22:34:46 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[Listener]]></category> <category><![CDATA[Matt Whitaker]]></category> <category><![CDATA[uicontrol]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1153</guid> <description><![CDATA[Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/gui-integrated-browser-control/' rel='bookmark' title='GUI integrated browser control'>GUI integrated browser control</a> <small>A fully-capable browser component is included in Matlab and can easily be incorporated in regular Matlab GUI applications. This article shows how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Once again, I welcome guest blogger Matt Whitaker, who continues his series of articles.</i></p><p>In my last post I explored some <a
target="_blank" href="http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/">tips on tooltips</a>. One of these tips involved displaying tooltips on disabled uicontrols. I explained that displaying tooltips on inactive controls is problematic since Matlab appears to intercept mouse events to these inactive controls, so even setting the tooltip on the underlying Java object will not work: The java object appears not to receive the mouse-hover event and therefore does not &#8220;know&#8221; that it&#8217;s time to display the tooltip.</p><p>When Yair and I deliberated this issue, he pointed me to his <a
target="_blank" href="http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/#comment-1173">comment on a previous article</a> showing an undocumented Java technique (Java also has some&#8230;) for forcing a tooltip to appear using the <b>ActionMap</b> of the uicontrol&#8217;s underlying Java object to get at a <i>postTip</i> action. We discussed using a <b>WindowButtonMotionFcn</b> callback to see if the mouse was above the inactive control, then triggering the forced tooltip display. Yair then went on to remind me and I quote: <i>&#8220;you&#8217;ll need to chain existing <b>WindowButtonMotionFcn</b> callbacks and take into account ModeManagers that override them.&#8221;</i></p><p>Frankly, having written code previously that handles callback chaining, I would rather poke myself in the eye with a fork!</p><p>The Image Processing Toolbox has the nice pair of <b><i>iptaddcallback</i></b> and <b><i>iptremovecallback</i></b> functions that largely handle these issues. But  for general Matlab, there seemed to be no alternative until I remembered that events trigger callbacks. I decided to use a listener for the <i>WindowButtonMotion</i> event to detect the mouse motion. <a
target="_blank" href="http://undocumentedmatlab.com/blog/continuous-slider-callback/#Event_Listener">Event listeners were briefly explained two weeks ago</a> and deserve a dedicated future article. The advantage of using an event listener is that we don&#8217;t disturb any existing <b>WindowButtonMotionFcn</b> callback. We still need to be somewhat careful that our listeners don&#8217;t do conflicting things, but it&#8217;s a lot easier than trying to manage everything through the single <b>WindowButtonMotionFcn</b>.</p><p>A demonstration of this appears below with some comments following (note that this code uses the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><b><i>FindJObj</i></b> utility</a>):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> inactiveBtnToolTip
  <span style="color: #228B22;">%Illustrates how to make a tooltip appear on an inactive control</span>
  h = <span style="color: #0000FF;">figure</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'WindowButtonMotionFcn'</span>,@windowMotion,<span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">400</span>,<span style="color: #33f;">400</span>,<span style="color: #33f;">200</span>,<span style="color: #33f;">200</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
  col = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>h,<span style="color:#A020F0;">'color'</span><span style="color: #080;">&#41;</span>;
  lbl = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'text'</span>, <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">160</span>,<span style="color: #33f;">120</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color: #F0F;">...</span>
                  <span style="color:#A020F0;">'Background'</span>,col, <span style="color:#A020F0;">'HorizontalAlignment'</span>,<span style="color:#A020F0;">'left'</span><span style="color: #080;">&#41;</span>;
  btn = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Parent'</span>,h, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Button'</span>, <span style="color: #F0F;">...</span>
                  <span style="color:#A020F0;">'Enable'</span>,<span style="color:#A020F0;">'inactive'</span>, <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">40</span>,<span style="color: #33f;">60</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'check'</span>, <span style="color:#A020F0;">'Parent'</span>,h, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'Enable button tooltip'</span>, <span style="color: #F0F;">...</span>
            <span style="color:#A020F0;">'Callback'</span>,@chkTooltipEnable, <span style="color:#A020F0;">'Value'</span>,<span style="color: #33f;">1</span>, <span style="color: #F0F;">...</span>
            <span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">80</span>,<span style="color: #33f;">180</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'Background'</span>,col<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">drawnow</span>;
&nbsp;
  <span style="color: #228B22;">%create the tooltip and postTip action</span>
  jBtn = findjobj<span style="color: #080;">&#40;</span>btn<span style="color: #080;">&#41;</span>;
  import java.<span style="">awt</span>.<span style="">event</span>.<span style="">ActionEvent</span>;
  javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setToolTipText'</span>,jBtn,<span style="color:#A020F0;">'This button is inactive'</span><span style="color: #080;">&#41;</span>;
  actionMap = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'getActionMap'</span>,jBtn<span style="color: #080;">&#41;</span>;
  action = javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'get'</span>,actionMap,<span style="color:#A020F0;">'postTip'</span><span style="color: #080;">&#41;</span>;
  actionEvent = ActionEvent<span style="color: #080;">&#40;</span>jBtn, ActionEvent.<span style="">ACTION_PERFORMED</span>, <span style="color:#A020F0;">'postTip'</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">%get the extents plus 2 pixels of the control to compare to the mouse position</span>
  btnPos = getpixelposition<span style="color: #080;">&#40;</span>btn<span style="color: #080;">&#41;</span>+<span style="color: #080;">&#91;</span>-<span style="color: #33f;">2</span>,-<span style="color: #33f;">2</span>,<span style="color: #33f;">4</span>,<span style="color: #33f;">4</span><span style="color: #080;">&#93;</span>; <span style="color: #228B22;">%give a little band around the control</span>
  left = btnPos<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
  right = <span style="color: #0000FF;">sum</span><span style="color: #080;">&#40;</span>btnPos<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">3</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
  btm = btnPos<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;
  top =  <span style="color: #0000FF;">sum</span><span style="color: #080;">&#40;</span>btnPos<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #33f;">2</span>,<span style="color: #33f;">4</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% add a listener on mouse movement events</span>
  tm = javax.<span style="">swing</span>.<span style="">ToolTipManager</span>.<span style="">sharedInstance</span>; <span style="color: #228B22;">%tooltip manager</span>
  pointListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>h,<span style="color:#A020F0;">'WindowButtonMotionEvent'</span>,@figMouseMove<span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">%inControl is a flag to prevent multiple triggers of the postTip action</span>
  <span style="color: #228B22;">%while mouse remains in the button</span>
  inControl = <span style="color: #0000FF;">false</span>;
&nbsp;
  <span style="color: #0000FF;">function</span> figMouseMove<span style="color: #080;">&#40;</span>src,evtData<span style="color: #080;">&#41;</span> <span style="color: #228B22;">%#ok</span>
    <span style="color: #228B22;">%get the current point</span>
    cPoint = evtData.<span style="">CurrentPoint</span>;
&nbsp;
    <span style="color: #0000FF;">if</span> cPoint<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> &gt;= left <span style="color: #F0F;">&amp;&amp;</span> cPoint<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> &lt;= right <span style="color: #F0F;">&amp;&amp;...</span>
       <span style="">cPoint</span><span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> &gt;= btm  <span style="color: #F0F;">&amp;&amp;</span> cPoint<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> &lt;= top
&nbsp;
      <span style="color: #0000FF;">if</span> ~inControl <span style="color: #228B22;">%we just entered</span>
        inControl = <span style="color: #0000FF;">true</span>;
        action.<span style="">actionPerformed</span><span style="color: #080;">&#40;</span>actionEvent<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%show the tooltip</span>
      <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
    <span style="color: #0000FF;">else</span>
      <span style="color: #0000FF;">if</span> inControl <span style="color: #228B22;">%we just existed</span>
        inControl = <span style="color: #0000FF;">false</span>;
        <span style="color: #228B22;">%toggle to make it disappear when leaving button</span>
        javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,tm,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
        javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'setEnabled'</span>,tm,<span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%gpMouseMove</span>
&nbsp;
  <span style="color: #0000FF;">function</span> windowMotion<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
    <span style="color: #228B22;">%illustrate that we can still do a regular window button motion callback</span>
    <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>lbl,<span style="color:#A020F0;">'String'</span>,<span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Mouse position: %d, %d'</span>,<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>h,<span style="color:#A020F0;">'CurrentPoint'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">drawnow</span>;
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%windowMotion</span>
&nbsp;
  <span style="color: #0000FF;">function</span> chkTooltipEnable<span style="color: #080;">&#40;</span>src,<span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
    <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>src,<span style="color:#A020F0;">'Value'</span><span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>pointListener,<span style="color:#A020F0;">'Enable'</span>,<span style="color:#A020F0;">'on'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">else</span>
      <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>pointListener,<span style="color:#A020F0;">'Enable'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%chkTooltipEnable</span>
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%inactiveBtnToolTip</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 218px"><img
alt="Tooltip on an inactive button" src="http://undocumentedmatlab.com/images/tooltip4.png" title="Tooltip on an inactive button" width="208" height="255" /><p
class="wp-caption-text">Tooltip on an inactive button</p></div></center></p><p>Comments on the code:</p><ol><li>The code illustrates that we can successfully add an additional listener to listen for mouse motion events while still carrying out the original <b>WindowButtonMotionFcn</b> callback. This makes chaining callbacks much easier.</li><li>The <b><i>handle.listener</i></b> object has an <b>Enable</b> property that we can use to temporarily turn the listener on and off. This can be seen in the <i>chkTooltipEnable()</i> callback for the check box in the code above. If we wanted to permanently remove the listener we would simply use <i><b>delete</b>(pointListener)</i>. Note that <b><i>addlistener</i></b> adds a hidden property to the object being listened to, so that the listener is tied to the object&#8217;s lifecycle. If you create a listener directly using <b><i>handle.listener</i></b> you are responsible for it&#8217;s disposition. Unfortunately, <b><i>addlistener</i></b> fails for HG handles on pre-R2009 Matlab releases, so we use <b><i>handle.listener</i></b> directly.</li><li>The code illustrates a good practice when tracking rapidly firing events like mouse movement of handling reentry into the callback while it is still processing a previous callback. Here we use a flag called inControl to prevent the <i>postTip</i> action being continuously fired while the mouse remains in the control.</li><li>I was unable to determine if there is any corresponding action for the <i>postTip</i> to dismiss tips so I resorted to using the ToolTipManager to toggle its own <b>Enable</b> property to cleanly hide the tooltip as the mouse leaves the control.</li></ol><p>Each Matlab callback has an associated event with it. Some of the ones that might be immediately useful at the figure-level are <b>WindowButtonDown</b>, <b>WindowButtonUp</b>, <b>WindowKeyPress</b>, and <b>WindowKeyRelease</b>. They can all be accessed through <b><i>handle.listener</i></b> or <b><i>addlistener</i></b> as in the code above.</p><p>Unfortunately, events do not always have names that directly correspond to the callback names. In order to see the list of available events for a particular Matlab object, use the following code, which relies on another undocumented function &#8211; <b><i>classhandle</i></b>. Here we list the events for <b><i>gcf</i></b>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>classhandle<span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'Events'</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'Name'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
    <span style="color:#A020F0;">'SerializeEvent'</span>
    <span style="color:#A020F0;">'FigureUpdateEvent'</span>
    <span style="color:#A020F0;">'ResizeEvent'</span>
    <span style="color:#A020F0;">'WindowKeyReleaseEvent'</span>
    <span style="color:#A020F0;">'WindowKeyPressEvent'</span>
    <span style="color:#A020F0;">'WindowButtonUpEvent'</span>
    <span style="color:#A020F0;">'WindowButtonDownEvent'</span>
    <span style="color:#A020F0;">'WindowButtonMotionEvent'</span>
    <span style="color:#A020F0;">'WindowPostChangeEvent'</span>
    <span style="color:#A020F0;">'WindowPreChangeEvent'</span></pre></div></div><p>Note that I have made extensive use of the <b><i>javaMethodEDT</b></i> function to execute Java methods that affect swing components on Swing&#8217;s Event Dispatch Thread. I plan to write about this and related functions in my next article.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/gui-integrated-browser-control/' rel='bookmark' title='GUI integrated browser control'>GUI integrated browser control</a> <small>A fully-capable browser component is included in Matlab and can easily be incorporated in regular Matlab GUI applications. This article shows how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Continuous slider callback</title><link>http://undocumentedmatlab.com/blog/continuous-slider-callback/</link> <comments>http://undocumentedmatlab.com/blog/continuous-slider-callback/#comments</comments> <pubDate>Mon, 08 Feb 2010 09:13:39 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[Listener]]></category> <category><![CDATA[schema.prop]]></category> <category><![CDATA[uicontrol]]></category> <category><![CDATA[Undocumented feature]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1052</guid> <description><![CDATA[Matlab slider uicontrols do not enable a continuous-motion callback by default. This article explains how this can be achieved using undocumented features.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy/' rel='bookmark' title='Controlling callback re-entrancy'>Controlling callback re-entrancy</a> <small>Callback reentrancy is a major problem for frequently-fired events. Luckily, it can easily be solved....</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-listbox-mouse-actions/' rel='bookmark' title='Setting listbox mouse actions'>Setting listbox mouse actions</a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Every few months, a CSSM forum reader asks how to set up a continuously-invoked slider callback: Matlab&#8217;s <a
target="_blank" rel="nofollow" href="http://blinkdagger.com/matlab/matlab-gui-tutorial-slider/">slider uicontrol</a> invokes the user callback only when the mouse button is released, and not continuously while the slider&#8217;s thumb is dragged. This functionality was again referred-to <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/272224">yesterday</a>, and I decided it merits a dedicated post.</p><p>There are three distinct simple ways to achieve continuous callbacks:</p><h3 id="Java_Callbacks">Using Java callbacks</h3><p>As explained in an earlier <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">article</a>, Matlab uicontrols are basically Java Swing objects that possess a large number of useful callbacks. Matlab sliders&#8217; underlying Java objects, which are really not <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/slider.html">JSliders</a> but <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html#operation">JScrollBars</a>, have an <b>AdjustmentValueChangedCallback</b> property that is useful for our purposes and is accessible using the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><b><i>FindJObj utility</i></b></a>. Simply download <b><i>FindJObj</i></b> from the <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14317">File Exchange</a>, and then:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hSlider = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'style'</span>,<span style="color:#A020F0;">'slider'</span>, <span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span>;
jScrollBar = findjobj<span style="color: #080;">&#40;</span>hSlider<span style="color: #080;">&#41;</span>;
jScrollBar.<span style="">AdjustmentValueChangedCallback</span> = @myCbFcn;
<span style="color: #228B22;">% or: set(jScrollBar,'AdjustmentValueChangedCallback',@myCbFcn)</span></pre></div></div><p>Where <i>myCbFcn</i> is the Matlab callback function that will be invoked continuously when the arrow buttons are depressed or the slider&#8217;s thumb is dragged.</p><h3 id="Event_Listener">Using an event listener</h3><p>An alternative to the Java route is to use Matlab&#8217;s undocumented <b><i>handle.listener</i></b> function to listen to the slider&#8217;s <b>Action</b> event, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hSlider,<span style="color:#A020F0;">'ActionEvent'</span>,@myCbFcn<span style="color: #080;">&#41;</span>;</pre></div></div><p>This alternative is used by Matlab&#8217;s own <b><i>imscrollpanel</i></b> function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">if</span> isJavaFigure
   <span style="color: #228B22;">% Must use these ActionEvents to get continuous events fired as slider</span>
   <span style="color: #228B22;">% thumb is dragged. Regular callbacks on sliders give only one event</span>
   <span style="color: #228B22;">% when the thumb is released.</span>
   hSliderHorListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hSliderHor,<span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'ActionEvent'</span>,@scrollHorizontal<span style="color: #080;">&#41;</span>;
   hSliderVerListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hSliderVer,<span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'ActionEvent'</span>,@scrollVertical<span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">setappdata</span><span style="color: #080;">&#40;</span>hScrollpanel,<span style="color:#A020F0;">'sliderListeners'</span>,<span style="color: #F0F;">...</span>
      <span style="color: #080;">&#91;</span>hSliderHorListener hSliderVerListener<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">else</span>
   <span style="color: #228B22;">% Unfortunately, the event route is only available with Java Figures,</span>
   <span style="color: #228B22;">% so platforms without Java Figure support get discrete events only</span>
   <span style="color: #228B22;">% when the mouse is released from dragging the slider thumb.</span>
   <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hSliderHor,<span style="color:#A020F0;">'callback'</span>,@scrollHorizontal<span style="color: #080;">&#41;</span>
   <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hSliderVer,<span style="color:#A020F0;">'callback'</span>,@scrollVertical<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">end</span></pre></div></div><h3 id="Property_Listener">Using a property listener</h3><p>The <b><i>handle.listener</i></b> function can also be used to listen to property value changes. In our case, set a post-set listener, that gets triggered immediately following <b>Value</b> property updates, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hhSlider = handle<span style="color: #080;">&#40;</span>hSlider<span style="color: #080;">&#41;</span>;
hProp = <span style="color: #0000FF;">findprop</span><span style="color: #080;">&#40;</span>hhSlider,<span style="color:#A020F0;">'Value'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% a schema.prop object</span>
hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>hhSlider,hProp,<span style="color:#A020F0;">'PropertyPostSet'</span>,@myCbFcn<span style="color: #080;">&#41;</span>;</pre></div></div><p>In addition to &#8216;PropertyPostSet&#8217;, we could also listen on &#8216;PropertyPreSet&#8217;, which is triggered immediately <u>before</u> the property is modified. There are also corresponding &#8216;*Get&#8217; options. In relatively old Matlab releases (I believe R2007b and earlier, but I&#8217;m not certain), the option names were simply &#8216;PostSet&#8217;, &#8216;PreSet&#8217; etc., without the &#8216;Property&#8217; prefix.</p><p>Do you know of any other way to achieve continuous callbacks? If so, I would be delighted to hear in the comments section below.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy/' rel='bookmark' title='Controlling callback re-entrancy'>Controlling callback re-entrancy</a> <small>Callback reentrancy is a major problem for frequently-fired events. Luckily, it can easily be solved....</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-listbox-mouse-actions/' rel='bookmark' title='Setting listbox mouse actions'>Setting listbox mouse actions</a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/continuous-slider-callback/feed/</wfw:commentRss> <slash:comments>30</slash:comments> </item> <item><title>setPrompt &#8211; Setting the Matlab Desktop prompt</title><link>http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/</link> <comments>http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/#comments</comments> <pubDate>Mon, 25 Jan 2010 15:30:54 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Desktop]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[Listener]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=965</guid> <description><![CDATA[The Matlab Desktop's Command-Window prompt can easily be modified using some undocumented features<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/setting-the-matlab-desktop-layout-programmatically/' rel='bookmark' title='Setting the Matlab desktop layout programmatically'>Setting the Matlab desktop layout programmatically</a> <small>The Matlab desktop enables saving and switching layouts using the main menu. This post shows how to do so programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-desktop-tab-completions/' rel='bookmark' title='Setting desktop tab completions'>Setting desktop tab completions</a> <small>The Matlab desktop's Command-Window tab-completion can be customized for user-defined functions...</small></li><li><a
href='http://undocumentedmatlab.com/blog/editormacro-v2-setting-command-window-key-bindings/' rel='bookmark' title='EditorMacro v2 &#8211; setting Command Window key-bindings'>EditorMacro v2 &#8211; setting Command Window key-bindings</a> <small>The EditorMacro utility was extended to support built-in Matlab Editor and Command-Window actions and key-bindings. This post describes the changes and the implementation details....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-status-bar-text/' rel='bookmark' title='Setting status-bar text'>Setting status-bar text</a> <small>The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A few days ago, a reader emailed me with a challenge to modify the standard matlab Command-Window prompt from &#8220;&gt;&gt; &#8221; to some other string, preferably a dynamic prompt with the current timestamp. At first thought this cannot be done: The Command-Window prompts are hard-coded and to the best of my knowledge cannot be modified via <a
target="_blank" href="http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/">properties</a> or <a
target="_blank" href="http://undocumentedmatlab.com/blog/changing-system-preferences-programmatically/">system preferences</a>.</p><p>So the prompt can (probably) not be modified in advance, but what if it could be modified after being displayed? It is true that my <a
target="_blank" href="http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/"><b<i>cprintf</i></b> utility</a> modifies the Command-Window contents in order to display formatted text in a variety of font colors. But this case is different since <b<i>cprintf</i></b> runs once synchronously (user-invoked), whereas the prompt appears asynchronously multiple times.</p><p>There are two methods of handling multiple asynchronous events in Matlab: setting a callback on the object, and setting a PostSet <b><i>handle.listener</i></b> (or <b><i>schema.listener</i></b>) on the relevant object property. The first of these methods is a well-known Matlab practice, although we shall see that it uses an undocumented callback and functionality; the PostSet method is entirely undocumented and not well-known and shall be described in some later article. I decided to use the callback method to set the prompt &#8211; interested readers can try the PostSet method.</p><h3>Setting the Command Window&#8217;s callback</h3><p>The solution involved finding the Command-Window reference handle, and setting one of its <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">many callbacks</a>, in our case <b>CaretUpdateCallback</b>. This callback is fired whenever the desktop text is modified, which is an event we trap to replace the displayed prompt:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Get the reference handle to the Command Window text area</span>
jDesktop = com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLDesktop</span>.<span style="">getInstance</span>;
<span style="color: #0000FF;">try</span>
  cmdWin = jDesktop.<span style="">getClient</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Command Window'</span><span style="color: #080;">&#41;</span>;
  jTextArea = cmdWin.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>.<span style="">getViewport</span>.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">catch</span>
  commandwindow;
  jTextArea = jDesktop.<span style="">getMainFrame</span>.<span style="">getFocusOwner</span>;
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Instrument the text area's callback</span>
<span style="color: #0000FF;">if</span> <span style="color: #0000FF;">nargin</span> <span style="color: #F0F;">&amp;&amp;</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span> <span style="color: #F0F;">&amp;&amp;</span> ~<span style="color: #0000FF;">strcmp</span><span style="color: #080;">&#40;</span>newPrompt,<span style="color:#A020F0;">'&gt;&gt; '</span><span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'CaretUpdateCallback'</span>,<span style="color: #080;">&#123;</span>@setPromptFcn,newPrompt<span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">else</span>
  <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'CaretUpdateCallback'</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>Now that we have the Command-Window object callback set, we need to set the logic of prompt replacement &#8211; this is done in the internal Matlab function <i>setPromptFcn</i>. Here is its core code:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Does the displayed text end with the default prompt?</span>
<span style="color: #228B22;">% Note: catch a possible trailing newline</span>
<span style="color: #0000FF;">try</span> jTextArea = jTextArea.<span style="">java</span>;  <span style="color: #0000FF;">catch</span>,  <span style="color: #0000FF;">end</span>  <span style="color: #228B22;">%#ok</span>
cwText = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'Text'</span><span style="color: #080;">&#41;</span>;
pos = strfind<span style="color: #080;">&#40;</span>cwText<span style="color: #080;">&#40;</span><span style="color: #0000FF;">max</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,end-<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span><span style="color: #F0F;">:</span><span style="color: #0000FF;">end</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'&gt;&gt; '</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>pos<span style="color: #080;">&#41;</span>
  <span style="color: #228B22;">% Short prompts need to be space-padded</span>
  newLen = jTextArea.<span style="">getCaretPosition</span>;
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span>&lt;<span style="color: #33f;">3</span>
    newPrompt<span style="color: #080;">&#40;</span><span style="color: #0000FF;">end</span>+<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span> = <span style="color:#A020F0;">' '</span>;
  <span style="color: #0000FF;">elseif</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span>&gt;<span style="color: #33f;">3</span>
    fprintfStr = newPrompt<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #F0F;">:</span>end-<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">fprintf</span><span style="color: #080;">&#40;</span>fprintfStr<span style="color: #080;">&#41;</span>;
    newLen = newLen + <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>fprintfStr<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% The Command-Window text should be modified on the EDT</span>
  <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'replaceRange(Ljava.lang.String;II)'</span>,<span style="color: #F0F;">...</span>
            <span style="">newPrompt</span><span style="color: #080;">&#40;</span>end-<span style="color: #33f;">2</span><span style="color: #F0F;">:</span><span style="color: #0000FF;">end</span><span style="color: #080;">&#41;</span>, newLen-<span style="color: #33f;">3</span>, newLen<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'repaint()'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>In this code snippet, note that we space-pad prompt string that are shorter than 3 characters: this is done to prevent an internal-Matlab mixup when displaying additional text &#8211; Matlab &#8220;knows&#8221; the Command-Window&#8217;s text position and it gets mixed up if it turns out to be shorter than expected.</p><p>Also note that I use the semi-documented <b><i>awtinvoke</i></b> function to replace the default prompt (and an automatically-appended space) on the Event-Dispatch Thread (more on this in a future article). Since Matlab R2008a, I could use the more convenient <b><i>javaMethodEDT</i></b> function, but I wanted my code to work on all prior Matlab 7 versions, where <b><i>javaMethodEDT</i></b> was not yet available.</p><h3>Preventing callback re-entry</h3><p>The callback snippet above would enter an endless loop if not changed: whenever the prompt is modified the callback would have been re-fired, the prompt re-modified and so on endlessly. There are many methods of preventing callback re-entry &#8211; here&#8217;s the one I chose:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> setPromptFcn<span style="color: #080;">&#40;</span>jTextArea,eventData,newPrompt<span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #228B22;">% Prevent overlapping reentry due to prompt replacement</span>
  <span style="color: #0000FF;">persistent</span> inProgress
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>inProgress<span style="color: #080;">&#41;</span>
    inProgress = <span style="color: #33f;">1</span>;  <span style="color: #228B22;">%#ok unused</span>
  <span style="color: #0000FF;">else</span>
    <span style="color: #0000FF;">return</span>;
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #0000FF;">try</span>
    <span style="color: #228B22;">% *** Prompt modification code goes here ***</span>
&nbsp;
    <span style="color: #228B22;">% force prompt-change callback to fizzle-out...</span>
    <span style="color: #0000FF;">pause</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0.02</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Never mind - ignore errors...</span>
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% Enable new callbacks now that the prompt has been modified</span>
  inProgress = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>;
&nbsp;
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% setPromptFcn</span></pre></div></div><h3>Handling multiple prompt types</h3><p>I now wanted my function to handle both static prompt strings (like: &#8216;[Yair] &#8216;) and dynamic prompts (like: &#8216;[25-Jan-2010 01:00:51] &#8216;). This is done by accepting string-evaluable strings/functions:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Try to evaluate the new prompt as a function</span>
<span style="color: #0000FF;">try</span>
  origNewPrompt = newPrompt;
  newPrompt = <span style="color: #0000FF;">feval</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">catch</span>
  <span style="color: #0000FF;">try</span>
    newPrompt = <span style="color: #0000FF;">eval</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Never mind - probably a string...</span>
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">ischar</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span> <span style="color: #F0F;">&amp;&amp;</span> <span style="color: #0000FF;">ischar</span><span style="color: #080;">&#40;</span>origNewPrompt<span style="color: #080;">&#41;</span>
  newPrompt = origNewPrompt;
<span style="color: #0000FF;">end</span></pre></div></div><h3>File Exchange submission</h3><p>I then added some edge-case error handling and wrapped everything in a single utility called <b><i>setPrompt</i></b> that is now <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/26471">available on the File Exchange</a>.</p><p>In the future, if I find time, energy and interest, maybe I&#8217;ll combine <b><i>cprintf</i></b>&#8216;s font-styling capabilities, to enable setting colored prompts.</p><h3>Setting a continuously-updated timestamp prompt</h3><p>Using the code above, we can now display a dynamic timestamp prompt, as follows:<br
/><center><div
class="wp-caption aligncenter" style="width: 438px"><img
alt="setPrompt usage examples" src="http://UndocumentedMatlab.com/images/setPrompt.png" title="setPrompt usage examples" width="428" height="224" /><p
class="wp-caption-text"><b><i>setPrompt</i></b> usage examples</p></div></center></p><p>However, the displayed timestamp is somewhat problematic in the sense that it indicates the time of prompt <u>creation</u> rather than the time that the associated Command-Window command was <u>executed</u>. In the screenshot above, [25-Jan-2010 01:29:42] is the time that the <b><i>234</i></b> command was executed, not the time that the <b><i>setPrompt</i></b> command was executed. This is somewhat misleading. It would be better if the last (current) timestamp was continuously updated and would therefore always display the latest command&#8217;s execution time. This can be done using a Matlab <b><i>timer</i></b> as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% This is entered in the main function before setting the prompt:</span>
stopPromptTimers;
<span style="color: #0000FF;">if</span> <span style="color: #0000FF;">nargin</span> <span style="color: #F0F;">&amp;&amp;</span> <span style="color: #0000FF;">strcmpi</span><span style="color: #080;">&#40;</span>newPrompt,<span style="color:#A020F0;">'timestamp'</span><span style="color: #080;">&#41;</span>
  <span style="color: #228B22;">% Update initial prompt &amp; prepare a timer to continuously update it</span>
  newPrompt = @<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color:#A020F0;">'['</span>,<span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">now</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'] '</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
  start<span style="color: #080;">&#40;</span>timer<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Tag'</span>,<span style="color:#A020F0;">'setPromptTimer'</span>, <span style="color:#A020F0;">'Name'</span>,<span style="color:#A020F0;">'setPromptTimer'</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'ExecutionMode'</span>,<span style="color:#A020F0;">'fixedDelay'</span>, <span style="color:#A020F0;">'ObjectVisibility'</span>,<span style="color:#A020F0;">'off'</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'Period'</span>,<span style="color: #33f;">0.99</span>, <span style="color:#A020F0;">'StartDelay'</span>,<span style="color: #33f;">0.5</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'TimerFcn'</span>,<span style="color: #080;">&#123;</span>@setPromptTimerFcn,jTextArea<span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Stop &amp; delete any existing prompt timer(s)</span>
<span style="color: #0000FF;">function</span> stopPromptTimers
  <span style="color: #0000FF;">try</span>
    timers = timerfindall<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'tag'</span>,<span style="color:#A020F0;">'setPromptTimer'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>timers<span style="color: #080;">&#41;</span>
      stop<span style="color: #080;">&#40;</span>timers<span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">delete</span><span style="color: #080;">&#40;</span>timers<span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Never mind...</span>
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% stopPromptTimers</span>
&nbsp;
<span style="color: #228B22;">% Internal timer callback function</span>
<span style="color: #0000FF;">function</span> setPromptTimerFcn<span style="color: #080;">&#40;</span>timerObj,eventData,jTextArea<span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">try</span>
    <span style="color: #0000FF;">try</span> jTextArea = jTextArea.<span style="">java</span>;  <span style="color: #0000FF;">catch</span>,  <span style="color: #0000FF;">end</span>  <span style="color: #228B22;">%#ok</span>
    pos = <span style="color: #0000FF;">getappdata</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'setPromptPos'</span><span style="color: #080;">&#41;</span>;
    newPrompt = <span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">now</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'replaceRange(Ljava.lang.String;II)'</span>,<span style="color: #F0F;">...</span>
              <span style="">newPrompt</span>, pos, pos+<span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'repaint()'</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Never mind...</span>
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% setPromptTimerFcn</span></pre></div></div><p>Can you come up with some innovative prompts? If so, please share them in a comment below.</p><p><u>Update 2010-Jan-26</u>: The code in this article was updated since it was first published yesterday.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/setting-the-matlab-desktop-layout-programmatically/' rel='bookmark' title='Setting the Matlab desktop layout programmatically'>Setting the Matlab desktop layout programmatically</a> <small>The Matlab desktop enables saving and switching layouts using the main menu. This post shows how to do so programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-desktop-tab-completions/' rel='bookmark' title='Setting desktop tab completions'>Setting desktop tab completions</a> <small>The Matlab desktop's Command-Window tab-completion can be customized for user-defined functions...</small></li><li><a
href='http://undocumentedmatlab.com/blog/editormacro-v2-setting-command-window-key-bindings/' rel='bookmark' title='EditorMacro v2 &#8211; setting Command Window key-bindings'>EditorMacro v2 &#8211; setting Command Window key-bindings</a> <small>The EditorMacro utility was extended to support built-in Matlab Editor and Command-Window actions and key-bindings. This post describes the changes and the implementation details....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-status-bar-text/' rel='bookmark' title='Setting status-bar text'>Setting status-bar text</a> <small>The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/feed/</wfw:commentRss> <slash:comments>9</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           category
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-05-18 21:36:19 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/category/listeners/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      2.587s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1337402177; expires=Sun, 19-May-2013 04:36:17 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Sat, 19 May 2012 04:36:19 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Sat, 19 May 2012 05:36:19 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               3aba1559ea3c1953abe4ce24b49657ce
Content-Encoding:   gzip
-->
