<?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; High risk of breaking in future versions</title> <atom:link href="http://undocumentedmatlab.com/blog/category/presumed-future-risk/high-risk-of-breaking-in-future-versions/feed/" rel="self" type="application/rss+xml" /><link>http://undocumentedmatlab.com</link> <description>Charting Matlab's unsupported hidden underbelly</description> <lastBuildDate>Wed, 08 Feb 2012 18:40:25 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.1</generator> <item><title>Matlab mex in-place editing</title><link>http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing/</link> <comments>http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing/#comments</comments> <pubDate>Wed, 08 Feb 2012 17:00:25 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Memory]]></category> <category><![CDATA[Mex]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Performance]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2699</guid> <description><![CDATA[Editing Matlab arrays in-place can be an important technique for optimizing calculations. This article shows how to do it using Mex.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/undocumented-profiler-options/' rel='bookmark' title='Undocumented profiler options'>Undocumented profiler options</a> <small>The Matlab profiler has some undocumented options that facilitate debugging memory bottlenecks and JIT (Just-In-Time Java compilation) problems....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/' rel='bookmark' title='Matlab-Java memory leaks, performance'>Matlab-Java memory leaks, performance</a> <small>Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/jmi-java-to-matlab-interface/' rel='bookmark' title='JMI &#8211; Java-to-Matlab Interface'>JMI &#8211; Java-to-Matlab Interface</a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>I would like to welcome Matlab Mex power-user <a
target="_blank" rel="nofollow" href="http://absurdlycertain.blogspot.com/">Peter Li</a> to a first in a short series of articles about undocumented aspects of Mex programing</i></p><p>Editing Matlab arrays in-place can be an important technique for optimizing calculations, especially when handling data that use large blocks of memory.  The Matlab language itself has some <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/loren/2007/03/22/in-place-operations-on-data/">limited support for in-place editing</a>, but when we are really concerned with speed we often turn to writing C/C++ extensions using the Mex interface.  Unfortunately, editing arrays in-place from Mex extensions is not officially supported in Matlab, and doing it incorrectly can cause data inconsistencies or can even cause Matlab to crash.  In this article, I will introduce the problem and show a simple solution that exhibit the basic implementation details of Matlab&#8217;s internal copy-on-write mechanism.</p><h3 id="Motivation">Why edit in-place?</h3><p>To demonstrate the techniques in this article, I use the <i>fast_median</i> function, which is part of <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/29453-nthelement">my nth_element package</a> on Matlab&#8217;s File Exchange.  You can download the package and play with the code if you want.  The examples are fairly self-explanatory, so if you do not want to try the code you should be okay just following along.</p><p>Let us try a few function calls to see how editing in-place can save time and memory:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; A = <span style="color: #0000FF;">rand</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100000000</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">tic</span>; <span style="color: #0000FF;">median</span><span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>    
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">4.122654</span> seconds.
&nbsp;
&gt;&gt; <span style="color: #0000FF;">tic</span>; fast_median<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">1.646448</span> seconds.
&nbsp;
&gt;&gt; <span style="color: #0000FF;">tic</span>; fast_median_ip<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.927898</span> seconds.</pre></div></div><p>If you try running this, be careful not to make A too large; tune the example according to the memory available on your system. In terms of the execution time for the different functions, your mileage may vary depending on factors such as: your system, what Matlab version you are running, and whether your test data is arranged in a single vector or a multicolumn array.</p><p>This example illustrates a few general points: first, <i>fast_median</i> is significantly faster than Matlab&#8217;s <i><b>native</b></i> median function. This is because <i>fast_median</i> uses a more efficient algorithm; see the nth_element page for more details.  Besides being a shameless plug, this demonstrates why we might want to write a Mex function in the first place: rewriting the median function in pure Matlab would be slow, but using C++ we can significantly improve on the status quo.</p><p>The second point is that the in-place version, <i>fast_median_ip</i>, yields an additional speed improvement.  What is the difference?  Let us look behind the scenes; here are the CPU and memory traces from my system monitor after running the above:</p><p><center><div
class="wp-caption alignleft" style="width: 387px"><img
alt="Memory and CPU usage for median() vs. fast_median_ip()" src="http://UndocumentedMatlab.com/images/median_vs_fast_median_ip.png" title="Memory and CPU usage for median() vs. fast_median_ip()" width="377" height="425"/><p
class="wp-caption-text">Memory and CPU usage for <i><b>median</b></i> vs. <i>fast_median_ip</i></p></div></center></p><p>You can see four spikes in CPU use, and some associated changes in memory allocation:</p><p>The first spike in CPU is when we created the test data vector; memory use also steps up at that time.</p><p>The second CPU spike is the largest; that is Matlab&#8217;s median function.  You can see that over that period memory use stepped up again, and then stepped back down; the median function makes a copy of the entire input data, and then throws its copy away when it is finished; this is expensive in terms of time and resources.</p><p>The <i>fast_median</i> function is the next CPU spike; it has a similar step up and down in memory use, but it is much faster.</p><p>Finally, in the case of <i>fast_median_ip</i> we see something different; there is a spike in CPU use, but memory use stays flat; the in-place version is faster and more memory efficient because it does not make a copy of the input data.</p><div
class="" style="width: 100%; overflow: auto;"></div><p>There is another important difference with the in-place version; it modifies its input array.  This can be demonstrated simply:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; A = randi<span style="color: #080;">&#40;</span><span style="color: #33f;">100</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">10</span> <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&gt;&gt; A'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">39</span>    <span style="color: #33f;">42</span>    <span style="color: #33f;">98</span>    <span style="color: #33f;">25</span>    <span style="color: #33f;">64</span>    <span style="color: #33f;">75</span>     <span style="color: #33f;">6</span>    <span style="color: #33f;">56</span>    <span style="color: #33f;">71</span>    <span style="color: #33f;">89</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">median</span><span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">60</span>
&nbsp;
&gt;&gt; fast_median<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">60</span>
&gt;&gt; A'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">39</span>    <span style="color: #33f;">42</span>    <span style="color: #33f;">98</span>    <span style="color: #33f;">25</span>    <span style="color: #33f;">64</span>    <span style="color: #33f;">75</span>     <span style="color: #33f;">6</span>    <span style="color: #33f;">56</span>    <span style="color: #33f;">71</span>    <span style="color: #33f;">89</span>
&nbsp;
&gt;&gt; fast_median_ip<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">60</span>
&gt;&gt; A'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">39</span>     <span style="color: #33f;">6</span>    <span style="color: #33f;">25</span>    <span style="color: #33f;">42</span>    <span style="color: #33f;">56</span>    <span style="color: #33f;">64</span>    <span style="color: #33f;">75</span>    <span style="color: #33f;">71</span>    <span style="color: #33f;">98</span>    <span style="color: #33f;">89</span></pre></div></div><p>As you can see, all three methods get the same answer, but <i><b>median</b></i> and <i>fast_median</i> do not modify A in the workspace, whereas after running <i>fast_median_ip</i>, input array A has changed.  This is how the in-place method is able to run without using new memory; it operates on the existing array rather than making a copy.</p><h3 id="Pitfalls">Pitfalls with in-place editing</h3><p>Modifying a function&#8217;s input is common in many languages, but in Matlab there are only a few special conditions under which this is officially sanctioned.  This is not necessarily a bad thing; many people feel that modifying input data is bad programming practice and makes code harder to maintain.  But as we have shown, it can be an important capability to have if speed and memory use are critical to an application.</p><p>Given that in-place editing is not officially supported in Matlab Mex extensions, what do we have to do to make it work?  Let us look at the normal, input-copying <i>fast_median</i> function as a starting point:</p><div
class="wp_syntax"><div
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> mexFunction<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> nlhs, mxArray <span style="color: #000040;">*</span>plhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> nrhs, <span style="color: #0000ff;">const</span> mxArray <span style="color: #000040;">*</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
   mxArray <span style="color: #000040;">*</span>incopy <span style="color: #000080;">=</span> mxDuplicateArray<span style="color: #008000;">&#40;</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   plhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> run_fast_median<span style="color: #008000;">&#40;</span>incopy<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>This is a pretty simple function (I have taken out a few lines of boiler plate input checking to keep things clean).  It relies on helper function <i>run_fast_median</i> to do the actual calculation, so the only real logic here is copying the input array <code>prhs[0]</code>.  Importantly, <i>run_fast_median</i> edits its inputs in-place, so the call to <i>mxDuplicateArray</i> ensures that the Mex function is overall well behaved, i.e. that it does not change its inputs.</p><p>Who wants to be well behaved though?  Can we save time and memory just by taking out the input duplication step?  Let us try it:</p><div
class="wp_syntax"><div
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> mexFunction<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> nlhs, mxArray <span style="color: #000040;">*</span>plhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> nrhs, <span style="color: #0000ff;">const</span> mxArray <span style="color: #000040;">*</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
   plhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> run_fast_median<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const_cast</span><span style="color: #000080;">&lt;</span>mxarray <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">// &lt;/mxarray&gt;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>Very bad behavior; note that we cast the original <code>const mxArray*</code> input to a <code>mxArray*</code> so that the compiler will let us mess with it; naughty.</p><p>But does this accomplish edit in-place for <i>fast_median</i>?  Be sure to save any work you have open and then try it:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; mex fast_median_tweaked.<span style="">cpp</span>
&gt;&gt; A = randi<span style="color: #080;">&#40;</span><span style="color: #33f;">100</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span> <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&gt;&gt; fast_median_tweaked<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">43</span></pre></div></div><p>Hmm, it looks like this worked fine.  But in fact there are subtle problems:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; A = randi<span style="color: #080;">&#40;</span><span style="color: #33f;">100</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span> <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&gt;&gt; A'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">65</span>    <span style="color: #33f;">92</span>    <span style="color: #33f;">14</span>    <span style="color: #33f;">26</span>    <span style="color: #33f;">41</span>     <span style="color: #33f;">2</span>    <span style="color: #33f;">45</span>    <span style="color: #33f;">85</span>    <span style="color: #33f;">53</span>     <span style="color: #33f;">2</span>
&gt;&gt; B = A;
&gt;&gt; B'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">65</span>    <span style="color: #33f;">92</span>    <span style="color: #33f;">14</span>    <span style="color: #33f;">26</span>    <span style="color: #33f;">41</span>     <span style="color: #33f;">2</span>    <span style="color: #33f;">45</span>    <span style="color: #33f;">85</span>    <span style="color: #33f;">53</span>     <span style="color: #33f;">2</span>
&nbsp;
&gt;&gt; fast_median_tweaked<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">43</span>
&gt;&gt; A'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">2</span>     <span style="color: #33f;">2</span>    <span style="color: #33f;">41</span>    <span style="color: #33f;">26</span>    <span style="color: #33f;">14</span>    <span style="color: #33f;">45</span>    <span style="color: #33f;">65</span>    <span style="color: #33f;">85</span>    <span style="color: #33f;">53</span>    <span style="color: #33f;">92</span>
&gt;&gt; B'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">2</span>     <span style="color: #33f;">2</span>    <span style="color: #33f;">41</span>    <span style="color: #33f;">26</span>    <span style="color: #33f;">14</span>    <span style="color: #33f;">45</span>    <span style="color: #33f;">65</span>    <span style="color: #33f;">85</span>    <span style="color: #33f;">53</span>    <span style="color: #33f;">92</span></pre></div></div><p>Uhoh, spooky; we expected that running <i>fast_median_tweaked</i> would change input A, but somehow it has also changed B, even though B is supposed to be an independent copy.  Not good.  In fact, under some conditions this kind of uncontrolled editing in-place can crash the entire Matlab environment with a segfault.  What is going on?</p><h3 id="COW">Matlab&#8217;s copy-on-write mechanism</h3><p>The answer is that our simple attempt to edit in-place conflicts with Matlab&#8217;s internal copy-on-write mechanism.  Copy-on-write is an optimization built into Matlab to help avoid expensive copying of variables in memory (actually similar to what we are trying to accomplish with edit in-place).  We can see copy-on-write in action with some simple tests:</p><div
class="wp-caption alignright" style="width: 403px"><img
alt="Matlab's Copy-on-Write memory and CPU usage" src="http://UndocumentedMatlab.com/images/copy-on-write.png" title="Matlab's Copy-on-Write memory and CPU usage" width="393" height="466"/><p
class="wp-caption-text">Matlab's Copy-on-Write memory and CPU usage</p></div><div><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Test #1: update, then copy</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; A = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100000000</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.588937</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000008</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; B = A; <span style="color: #0000FF;">toc</span>   
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000020</span> seconds.
&nbsp;
<span style="color: #228B22;">% Test #2: copy, then update</span>
&gt;&gt; <span style="color: #0000FF;">clear</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; A = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100000000</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.588937</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; B = A; <span style="color: #0000FF;">toc</span>   
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000020</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.678160</span> seconds.</pre></div></div></div><p>In the first set of operations, time and memory are used to create A, but updating A and &#8220;copying&#8221; A into B take no memory and essentially no time.  This may come as a surprise since supposedly we have made an independent copy of A in B; why does creating B take no time or memory when A is clearly a large, expensive block?</p><p>The second set of operations makes things more clear.  In this case, we again create A and then copy it to B; again this operation is fast and cheap.  But assigning into A at this point takes time and consumes a new block of memory, even though we are only assigning into a single index of A.  This is copy-on-write: Matlab tries to save you from copying large blocks of memory unless you need to.  So when you first assign B to equal A, nothing is copied; the variable B is simply set to point to the same memory location already used by A.  Only after you try to change A (or B), does Matlab decide that you really need to have two copies of the large array.</p><p>There are some additional tricks Matlab does with copy-on-write.  Here is another example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">clear</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span> = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100000000</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.573240</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">2</span><span style="color: #080;">&#125;</span> = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100000000</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.560369</span> seconds.
&nbsp;
&gt;&gt; <span style="color: #0000FF;">tic</span>; B = A; <span style="color: #0000FF;">toc</span>                     
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000016</span> seconds.
&nbsp;
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>               
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.690690</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">2</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.695758</span> seconds.
&nbsp;
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000011</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">2</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000004</span> seconds.</pre></div></div><p>This shows that for the purposes of copy-on-write, different elements of cell array A are treated independently.  When we assign B equal to A, nothing is copied.  Then when we change any part of A{1}, that whole element must be copied over.  When we subsequently change A{2}, that whole element must also be copied over; it was not copied earlier.  At this point, A and B are truly independent of each other, as both elements have experienced copy-on-write, so further assignments into either A or B are fast and require no additional memory.</p><p>Try playing with some struct arrays and you will find that copy-on-write also works independently for the elements of structs.</p><h3 id="mxUnshareArray">Reconciling edit in-place with copy-on-write: mxUnshareArray</h3><p>Now it is clear why we cannot simply edit arrays in-place from Mex functions; not only is it naughty, it fundamentally conflicts with copy-on-write.  Naively changing an array in-place can inadvertently change other variables that are still waiting for a copy-on-write, as we saw above when <i>fast_median_tweaked</i> inadvertently changed B in the workspace. This is, in the best case, an unmaintainable mess.  Under more aggressive in-place editing, it can cause Matlab to crash with a segfault.</p><p>Luckily, there is a simple solution, although it requires calling internal, undocumented Matlab functions.</p><p>Essentially what we need is a Mex function that can be run on a Matlab array that will do the following:</p><ol><li>Check whether the current array is sharing data with any other arrays that are waiting for copy-on-write.</li><li>If the array is shared, it must be unshared; the underlying memory must be copied and all the relevant pointers need to be fixed so that the array we want to work on is no longer accessible by anyone else.</li><li>If the array is not currently shared, simply proceed; the whole point is to avoid copying memory if we do not need to, so that we can benefit from the efficiencies of edit in-place.</li></ol><p>If you think about it, this is exactly the operation that Matlab needs to run internally when it is deciding whether an assignment operation requires a copy-on-write.  So it should come as no surprise that such a Mex function already exists in the form of a Matlab internal called <i>mxUnshareArray</i>.  Here is how you use it:</p><div
class="wp_syntax"><div
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">extern</span> <span style="color: #FF0000;">&quot;C&quot;</span> <span style="color: #0000ff;">bool</span> mxUnshareArray<span style="color: #008000;">&#40;</span>mxArray <span style="color: #000040;">*</span>array_ptr, <span style="color: #0000ff;">bool</span> noDeepCopy<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> mexFunction<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> nlhs, mxArray <span style="color: #000040;">*</span>plhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> nrhs, <span style="color: #0000ff;">const</span> mxArray <span style="color: #000040;">*</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
   mxUnshareArray<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const_cast</span><span style="color: #000080;">&lt;</span>mxarray <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">//&lt;/mxarray&gt;</span>
   plhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> run_fast_median<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const_cast</span><span style="color: #000080;">&lt;</span>mxarray <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">//&lt;/mxarray&gt;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>This is the method actually used by <i>fast_median_ip</i> to efficiently edit in-place without risking conflicts with copy-on-write.  Of course, if the array turns out to need to be unshared, then you do not get the benefit of edit in-place because the memory ends up getting copied.  But at least things are safe and you get the in-place benefit as long as the input array is not being shared.</p><h3 id="Extra">Further topics</h3><p>The method shown here should allow you to edit normal Matlab numerical or character arrays in-place from Mex functions safely.  For a Mex function in C rather than C++, omit the &#8220;C&#8221; in the <code>extern</code> declaration and of course you will have to use C-style casting rather than <code>const_cast</code>.  If you need to edit cell or struct arrays in-place, or especially if you need to edit subsections of shared cell or struct arrays safely and efficiently while leaving the rest of the data shared, then you will need a few more tricks.  A good place to get started is <a
target="_blank" rel="nofollow" href="http://www.mk.tu-berlin.de/Members/Benjamin/mex_sharedArrays">this article by Benjamin Schubert</a>.</p><p>Unfortunately, over the last few years Mathworks seems to have decided to make it more difficult for users to access these kinds of internal methods to make our code more efficient.  So be aware of the risk that in some future version of Matlab this method will no longer work in its current form.</p><p>Ultimately much of what is known about <i>mxUnshareArray</i> as well as the internal implementation details of how Matlab keeps track of which arrays are shared goes back to the work of Peter Boettcher, particularly his <a
target="_blank" rel="nofollow" href="http://groups.google.com/group/comp.soft-sys.matlab/browse_thread/thread/c241d8821fb90275/">headerdump.c utility</a>.  Unfortunately, it appears that HeaderDump fails with Matlab releases >=R2010a, as Mathworks have changed some of the internal memory formats &#8211; perhaps some smart reader can pick up the work and adapt HeaderDump to the new memory format.</p><p>In a future article, I hope to discuss headerdump.c and its relevance for copy-on-write and edit in-place, and some other related tools for the latest Matlab releases that do not support HeaderDump.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/undocumented-profiler-options/' rel='bookmark' title='Undocumented profiler options'>Undocumented profiler options</a> <small>The Matlab profiler has some undocumented options that facilitate debugging memory bottlenecks and JIT (Just-In-Time Java compilation) problems....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/' rel='bookmark' title='Matlab-Java memory leaks, performance'>Matlab-Java memory leaks, performance</a> <small>Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/jmi-java-to-matlab-interface/' rel='bookmark' title='JMI &#8211; Java-to-Matlab Interface'>JMI &#8211; Java-to-Matlab Interface</a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Types of undocumented Matlab aspects</title><link>http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/</link> <comments>http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/#comments</comments> <pubDate>Thu, 24 Nov 2011 18:00:36 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented feature]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JMI]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[Undocumented property]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2534</guid> <description><![CDATA[This article lists the different types of undocumented/unsupported/hidden aspects in Matlab<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/reasons-for-undocumented-matlab-aspects/' rel='bookmark' title='Reasons for undocumented Matlab aspects'>Reasons for undocumented Matlab aspects</a> <small>There are many reasons for the numerous undocumented aspects in Matlab - this article explains them....</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-cursorbar-object/' rel='bookmark' title='Undocumented cursorbar object'>Undocumented cursorbar object</a> <small>Matlab's internal undocumented graphics.cursorbar object can be used to present dynamic data-tip cross-hairs...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uiundo-matlab-undocumented-undo-redo-manager/' rel='bookmark' title='uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager'>uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager</a> <small>The built-in uiundo function provides easy yet undocumented access to Matlab's powerful undo/redo functionality. This article explains its usage....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Why are there so many undocumented aspects in Matlab?</p><p>This is a great question, recently <a
target="_blank" href="http://undocumentedmatlab.com/blog/guide-customization/#comment-61578">asked</a> by a reader of this blog, so I wanted to expand on it in next week&#8217;s article. Before specifying the different reasons, let&#8217;s map the nature of undocumented aspects that we find in Matlab.</p><p>The term <i>undocumented/unsupported</i> (as opposed to <i>mis-documentated</i> or <i>deprecated</i>) actually refers to quite a large number of different types.<br
/> In the following list, the hyperlinks on the list-item titles lead to a list of corresponding articles on this website:</p><ul><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/undocumented-function/">Undocumented functions</a></b><br
/> Matlab functions which appears nowhere in the documentation, are usually built-in functions (do not have an m-file) and can only be inferred from online CSSM posts or usage within one of the Matlab m-functions installed with Matlab (the latter being the usual case). None of these functions is officially supported by MathWorks. <a
target="_blank" href="http://undocumentedmatlab.com/blog/category/mex/">MEX</a> is an important source for such functions.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/semi-documented-function/">Semi-documented functions</a></b><br
/> Matlab functionality which exists in Matlab m-functions installed with Matlab, but have their main comment separated from the H1 comment line, thereby hiding it from normal view (via Matlab&#8217;s <i><b>help</b></i> function). The H1 comment line itself is simply a standard warning that this function is not officially supported and may change in some future version. To see the actual help comment, simply edit the function (using Matlab&#8217;s <i><b>edit</b></i> function or any text editor) and place a comment sign (%) at the empty line between the H1 comment and the actual help section. The entire help section will then onward be visible via the <i><b>help</b></i> function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">        <span style="color: #0000FF;">function</span> <span style="color: #080;">&#91;</span>tree, container<span style="color: #080;">&#93;</span> = uitree<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
        <span style="color: #228B22;">% WARNING: This feature is not supported in MATLAB</span>
        <span style="color: #228B22;">% and the API and functionality may change in a future release.</span>
<span style="color: #0000FF;">fix</span> =&gt;  <span style="color: #228B22;">%</span>
        <span style="color: #228B22;">% UITREE creates a uitree component with hierarchical data in a figure window.</span>
        <span style="color: #228B22;">%   UITREE creates an empty uitree object with default property values in</span>
        <span style="color: #228B22;">%   a figure window.</span>
        <span style="color: #228B22;">%...</span></pre></div></div><p>These functions are not documented in the full documentation (via Matlab&#8217;s <i><b>doc</b></i> function, or online). The odd thing is that some of these functions may appear in the category help output (for example, <i><b>help</b>(&#8216;uitools&#8217;)</i>), and in some cases may even have a fully-visible help section (e.g., <i><b>help</b>(&#8216;setptr&#8217;)</i>), but do not have any online help documentation (<i><b>docsearch</b>(&#8216;setptr&#8217;)</i> fails, and <i><b>doc</b>(&#8216;setptr&#8217;)</i> simply displays the readable help text).</p><p>All these functions are officially unsupported by MathWorks, even when having a readable help section. The rule of thumb appears to be that a Matlab function is supported only if it has online documentation. Note, however, that in some rare cases a documentation discrepancy may be due to a MathWorks documentation error, not to unsupportability&#8230;</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/undocumented-function/">Helper functions</a></b><br
/> Many fully-supported Matlab functions use helper functions that have a specific use in the main (documented) function(s).  Often, these helper functions are tightly-coupled to their documented parents and are useless as stand-alone functions. But quite a few of them have quite useful stand-alone use, as I&#8217;ve already shown in some past articles.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/undocumented-feature/">Undocumented features</a> and <a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/undocumented-property/">properties</a></b><br
/> Features of otherwise-documented Matlab functions, which appear nowhere in the official documentation. You guessed it – these are also not supported by MathWorks&#8230; Like undocumented functions, you can only infer such features by the occasional CSSM post or a reference somewhere in Matlab&#8217;s m-code.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/semi-documented-feature/">Semi-documented features</a></b><br
/> Features of otherwise-documented Matlab functions, which are documented in a separate section beneath the main help section, and nowhere else (not in the full doc not the online documentation). If you did not know in advance that these features existed, you could only learn of them by manually looking at Matlab&#8217;s m-files (which is what I do in most cases&#8230;).</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/undocumented-property/">Undocumented properties</a></b><br
/> Many Matlab objects have internal properties, which can be retrieved (via Matlab&#8217;s <i><b>get</b></i> function) and/or set (via the <i><b>set</b></i> function) programmatically. All these properties are fully documented. Many objects also possess hidden properties, some of which are very interesting and useful, but which are undocumented and (oh yes) unsupported. Like undocumented features, they can only be inferred from CSSM or existing code. In a recent <a
target="_blank" href="http://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/">article</a> I described my <i><b>getundoc</b></i> utility, which lists these undocumented properties of specified objects.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/internal-component/">Internal Matlab classes</a></b><br
/> Matlab uses a vast array of specialized Java classes to handle everything from algorithms to GUI. These classes are (of course) undocumented/unsupported. They can often be accessed directly from the Matlab Command Window or user m-files. GUI classes can be inferred by inspecting the figure frame&#8217;s Java components, and non-GUI classes can often be inferred from references in Matlab&#8217;s m-files.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/JMI">Matlab-Java integration</a></b><br
/> Matlab&#8217;s GUI interface, as well as the Java-to-Matlab interface (JMI) is fully undocumented and unsupported. In addition to JMI, there are other mechanisms to run Matlab code from within Java (namely JMI, COM and DDE) &#8211; these are all unsupported and by-and-large undocumented.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/?s=UDD">Matlab&#8217;s UDD mechanism</a></b><br
/> UDD (Unified Data Definition?) is used extensively in Matlab as the internal object-oriented mechanism for describing object properties and functionalities. We can use UDD for a wide variety of uses. UDD was described in a series of articles here in early 2011.</li></ul><p>Next week I will list the reasons that cause MathWorks to decide whether a particular feature or property should be documented or not.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/reasons-for-undocumented-matlab-aspects/' rel='bookmark' title='Reasons for undocumented Matlab aspects'>Reasons for undocumented Matlab aspects</a> <small>There are many reasons for the numerous undocumented aspects in Matlab - this article explains them....</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-cursorbar-object/' rel='bookmark' title='Undocumented cursorbar object'>Undocumented cursorbar object</a> <small>Matlab's internal undocumented graphics.cursorbar object can be used to present dynamic data-tip cross-hairs...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uiundo-matlab-undocumented-undo-redo-manager/' rel='bookmark' title='uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager'>uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager</a> <small>The built-in uiundo function provides easy yet undocumented access to Matlab's powerful undo/redo functionality. This article explains its usage....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Uitable sorting</title><link>http://undocumentedmatlab.com/blog/uitable-sorting/</link> <comments>http://undocumentedmatlab.com/blog/uitable-sorting/#comments</comments> <pubDate>Tue, 26 Jul 2011 18:00:01 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JIDE]]></category> <category><![CDATA[uitable]]></category> <category><![CDATA[uitools]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2391</guid> <description><![CDATA[Matlab's uitables can be sortable using simple undocumented features<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitable-customization-report/' rel='bookmark' title='Uitable customization report'>Uitable customization report</a> <small>In last week&#8217;s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives/' rel='bookmark' title='Tab panels &#8211; uitab and relatives'>Tab panels &#8211; uitab and relatives</a> <small>This article describes several undocumented Matlab functions that support tab-panels...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/ref/uitable.html"><i><b>uitable</b></i></a> is probably the most complex basic GUI controls available in Matlab. It displays data in a table within a figure, with settable properties as with any other Matlab Handle-Graphics (HG) control. After many years in which the <i><b>uitable</b></i> was available but <a
target="_blank" href="http://undocumentedmatlab.com/blog/legend-semi-documented-feature/#Semi-documented">semi-documented</a> and not officially supported in Matlab, it finally became fully documented and supported in R2008a (aka Matlab 7.6). At that time its internal implementation has changed from a MathWorks-developed Java table to a <a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/JIDE/">JIDE</a>-based Java table (another JIDE-derived table was described <a
target="_blank" href="http://undocumentedmatlab.com/blog/jide-property-grids/">here</a> last year). Since R2008a, both versions of <i><b>uitable</b></i> are available &#8211; the old version is available by adding the &#8216;v0&#8242; input arg.</p><p>Matlab&#8217;s <i><b>uitable</b></i> exposes only a very limited subset of functionalities and properties to the user. Numerous other functionalities are available by accessing the underlying Java table and hidden Matlab properties. Today I will describe a very <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/answers/1880-uitable-can-the-headers-be-made-clickable">common need</a> in GUI tables, that for some unknown reason is missing in Matlab&#8217;s <i><b>uitable</b></i>: Sorting table data columns.</p><p>Last week I <a
target="_blank" href="http://undocumentedmatlab.com/blog/running-vb-code-in-matlab/">explained</a> how we can modify table headers of an ActiveX table control to display sorting icons. In that case, sorting was built-in the control, and the question was just how to display the sorting arrow icon. Unfortunately, Matlab&#8217;s <i><b>uitable</b></i> does not have sorting built-in, although it&#8217;s quite easy to add it, as I shall now show.</p><h3 id="Old">Old uitable sorting</h3><p>The old <i><b>uitable</b></i> is the default control used until R2007b, or that can be selected with the &#8216;v0&#8242; input arg since R2008a. It was based on an internal MathWorks extension of the standard Java Swing <a
target="_blank" rel="nofollow" href="http://download.oracle.com/javase/tutorial/uiswing/components/table.html">JTable</a> &#8211; a class called <code>com.mathworks.widgets.spreadsheet.SpreadsheetTable</code>.</p><p>Users will normally try to sort columns by clicking the header. This has been a deficiency of JTable for ages. To solve this for the old (pre-R2008a) <i><b>uitable</b></i>, download one of several available JTable sorter classes, or my TableSorter class (available <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14225-java-based-data-table">here</a>).  Add the TableSorter.jar file to your static java classpath (via <code>edit('classpath.txt')</code>) or your dynamic classpath (<code>javaaddpath('TableSorter.jar')</code>).</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display the uitable and get its underlying Java object handle</span>
<span style="color: #080;">&#91;</span>mtable,hcontainer<span style="color: #080;">&#93;</span> = uitable<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color: #0000FF;">gcf</span>, <span style="color: #0000FF;">magic</span><span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>, <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'A'</span>, <span style="color:#A020F0;">'B'</span>, <span style="color:#A020F0;">'C'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;   <span style="color: #228B22;">% discard the 'v0' in R2007b and earlier</span>
jtable = mtable.<span style="">getTable</span>;   <span style="color: #228B22;">% or: get(mtable,'table');</span>
&nbsp;
<span style="color: #228B22;">% We want to use sorter, not data model...</span>
<span style="color: #228B22;">% Unfortunately, UitablePeer expects DefaultTableModel not TableSorter so we need a modified UitablePeer class</span>
<span style="color: #228B22;">% But UitablePeer is a Matlab class, so use a modified TableSorter &amp; attach it to the Model</span>
<span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">which</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'TableSorter'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
   <span style="color: #228B22;">% Add TableSorter as TableModel listener</span>
   sorter = TableSorter<span style="color: #080;">&#40;</span>jtable.<span style="">getModel</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
   jtable.<span style="">setModel</span><span style="color: #080;">&#40;</span>sorter<span style="color: #080;">&#41;</span>;
   sorter.<span style="">setTableHeader</span><span style="color: #080;">&#40;</span>jtable.<span style="">getTableHeader</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
   <span style="color: #228B22;">% Set the header tooltip (with sorting instructions)</span>
   jtable.<span style="">getTableHeader</span>.<span style="">setToolTipText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'&lt;html&gt;&amp;nbsp;&lt;b&gt;Click&lt;/b&gt; to sort up; &lt;b&gt;Shift-click&lt;/b&gt; to sort down&lt;br /&gt;&amp;nbsp;...&lt;/html&gt;'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">else</span>
   <span style="color: #228B22;">% Set the header tooltip (no sorting instructions...)</span>
   jtable.<span style="">getTableHeader</span>.<span style="">setToolTipText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'&lt;html&gt;&amp;nbsp;&lt;b&gt;Click&lt;/b&gt; to select entire column&lt;br /&gt;&amp;nbsp;&lt;b&gt;Ctrl-click&lt;/b&gt; (or &lt;b&gt;Shift-click&lt;/b&gt;) to select multiple columns&amp;nbsp;&lt;/html&gt;'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 607px"><img
alt="Sorted uitable - old version" src="http://www.mathworks.com/matlabcentral/fx_files/14225/1/table.png" title="Sorted uitable - old version" width="597" /><p
class="wp-caption-text">Sorted <i><b>uitable</b></i> - old version</p></div></center></p><h3 id="New">New uitable sorting</h3><p>The new <i><b>uitable</b></i> is based on JIDE&#8217;s <code>com.jidesoft.grid.SortableTable</code> and so has built-in sorting support – all you need to do is to turn it on. First get the underlying Java object using my <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/">FindJObj utility</a>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display the uitable and get its underlying Java object handle</span>
mtable = uitable<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>, <span style="color:#A020F0;">'Data'</span>,<span style="color: #0000FF;">magic</span><span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'ColumnName'</span>,<span style="color: #080;">&#123;</span><span style="color:#A020F0;">'A'</span>, <span style="color:#A020F0;">'B'</span>, <span style="color:#A020F0;">'C'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
jscrollpane = findjobj<span style="color: #080;">&#40;</span>mtable<span style="color: #080;">&#41;</span>;
jtable = jscrollpane.<span style="">getViewport</span>.<span style="">getView</span>;
&nbsp;
<span style="color: #228B22;">% Now turn the JIDE sorting on</span>
jtable.<span style="">setSortable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;		<span style="color: #228B22;">% or: set(jtable,'Sortable','on');</span>
jtable.<span style="">setAutoResort</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
jtable.<span style="">setMultiColumnSortable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
jtable.<span style="">setPreserveSelectionsAfterSorting</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Note: the Matlab <code>mtable</code> handle has a hidden <b>Sortable</b> property, but it has no effect – use the Java property mentioned above instead. I assume that the hidden <b>Sortable</b> property was meant to implement the sorting behavior in R2008a, but MathWorks never got around to actually implement it, and so it remains this way to this day.</p><h3 id="Report">A more detailed report</h3><p>I have prepared a 30-page report about using and customizing Matlab&#8217;s <i><b>uitable</b></i>, which greatly expands on the above. This report is available for a small fee <a
target="_blank" rel="nofollow" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&#038;business=ymasoftware@gmail.com&#038;currency_code=USD&#038;amount=35&#038;return=&#038;item_name=Matlab-uitable-report">here</a> (please allow up to 48 hours for email delivery). The report includes the following:</p><ul><li>comparison of the old vs. the new <i><b>uitable</b></i> implementations</li><li>description of the <i><b>uitable</b></i> properties and callbacks</li><li>alternatives to <i><b>uitable</b></i> using a variety of technologies</li><li>updating a specific cell&#8217;s value</li><li>setting background and foreground colors for a cell or column</li><li>using dedicated cell renderer and editor components</li><li>HTML processing</li><li>setting dynamic cell-specific tooltip</li><li>setting dynamic cell-specific drop-down selection options</li><li>using a color-selection drop-down for cells</li><li>customizing scrollbars</li><li>customizing column widths and resizing</li><li>customizing selection behavior</li><li>data sorting (expansion of today&#8217;s article)</li><li>data filtering (similar to Excel&#8217;s data filtering control)</li><li>merging table cells</li><li>programmatically adding/removing rows</li><li>numerous links to online resources</li><li>overview of the JIDE grids package, which contains numerous extremely useful GUI controls and components</li></ul><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitable-customization-report/' rel='bookmark' title='Uitable customization report'>Uitable customization report</a> <small>In last week&#8217;s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives/' rel='bookmark' title='Tab panels &#8211; uitab and relatives'>Tab panels &#8211; uitab and relatives</a> <small>This article describes several undocumented Matlab functions that support tab-panels...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/uitable-sorting/feed/</wfw:commentRss> <slash:comments>16</slash:comments> </item> <item><title>Docking figures in compiled applications</title><link>http://undocumentedmatlab.com/blog/docking-figures-in-compiled-applications/</link> <comments>http://undocumentedmatlab.com/blog/docking-figures-in-compiled-applications/#comments</comments> <pubDate>Wed, 15 Jun 2011 18:00:56 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Figure window]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Compiler]]></category> <category><![CDATA[Docking]]></category> <category><![CDATA[Figure]]></category> <category><![CDATA[JavaFrame]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2341</guid> <description><![CDATA[Figures in compiled applications cannot officially be docked since R2008a, but this can be done using a simple undocumented trick.<pre> </pre>Related posts:<ol><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/findjobj-find-underlying-java-object/' rel='bookmark' title='FindJObj &#8211; find a Matlab component&#8217;s underlying Java object'>FindJObj &#8211; find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li><li><a
href='http://undocumentedmatlab.com/blog/blurred-matlab-figure-window/' rel='bookmark' title='Blurred Matlab figure window'>Blurred Matlab figure window</a> <small>Matlab figure windows can be blurred using a semi-transparent overlaid window - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/jmi-wrapper-local-matlabcontrol-part-2/' rel='bookmark' title='JMI wrapper &#8211; local MatlabControl part 2'>JMI wrapper &#8211; local MatlabControl part 2</a> <small>An example using matlabcontrol for calling Matlab from within a Java class is explained and discussed...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Up until Matlab release R2008a, the Matlab compiler enabled compiled Matlab applications to have dockable figure windows, which docked into a &#8220;Figures&#8221; container. <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-A6XFET/">Starting with R2008a</a>, the compiler removed the figure&#8217;s docking capability and figures can no longer be docked.</p><p>Well, at least not officially <img
src='http://undocumentedmatlab.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p><p>The following trick restores the docking controls to figures in R2008a-compiled applications, enabling figure docking. Simply add one or both of the following alternatives in your application, after the figure has been created:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Alternative #1 - uses pure Matlab</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hFig, <span style="color:#A020F0;">'DockControls'</span>, <span style="color:#A020F0;">'on'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Alternative #2 - uses the underlying Java frame</span>
jFrame = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>hFig<span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'JavaFrame'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">try</span>
   <span style="color: #228B22;">% This works up to R2011a</span>
   jFrame.<span style="">fFigureClient</span>.<span style="">setClientDockable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">catch</span>
   <span style="color: #228B22;">% This works from R2008b and up</span>
   jFrame.<span style="">fHG1Client</span>.<span style="">setClientDockable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>where <code>hFig</code> is the figure handle. This will have no effect for the regular interpreted (non-compiled) run of the application, where these controls are &#8216;on&#8217; by default. But in the compiled application, although it may erroneously report that the controls are &#8216;on&#8217;, they are in fact &#8216;off&#8217;, so turning them &#8216;on&#8217; fixes the problem.</p><p><center><div
class="wp-caption aligncenter" style="width: 478px"><img
alt="Matlab figure docking control" src="http://UndocumentedMatlab.com/images/dock4.png" title="Matlab figure docking control" width="468" /><p
class="wp-caption-text">Matlab figure docking control</p></div></center></p><p><u>Note</u>: the two variants in alternative #2 above are actually identical, it is simply that the relevant field name has changed: Up to R2008a, only the <code>fFigureClient</code> existed; in R2008b, the <code>fHG1Client</code> field was added, which was simply an alias for <code>fFigureClient</code>, holding the same reference handle, so either of these fields could be used (a corresponding fHG2Client was also added &#8211; more on HG1 and HG2 <a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/hg2/">here</a>). In R2011b (at least the pre-release), the <code>fFigureClient</code> alias field was dropped and only <code>fHG1Client</code> remained. While the field name has changed, the underlying docking functionality appears to have remained stable over all these releases. For the record, in answer to a <a
href="http://undocumentedmatlab.com/blog/docking-figures-in-compiled-applications/#comment-48632">user question below</a>, these fields can be listed using the built in <i><b>fieldnames</b></i> function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% R2008b - R2011a:</span>
&gt;&gt; <span style="color: #0000FF;">fieldnames</span><span style="color: #080;">&#40;</span>jFrame<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
    <span style="color:#A020F0;">'fFigureClient'</span>
    <span style="color:#A020F0;">'fHG2Client'</span>
    <span style="color:#A020F0;">'fHG1Client'</span>
    <span style="color:#A020F0;">'fUseHG2'</span>
    <span style="color:#A020F0;">'UICONTROLBACKGROUND_OS'</span>
    <span style="color:#A020F0;">'UICONTROLBACKGROUND_COMPATIBLE'</span></pre></div></div><p>I was reminded of this trick by <a
target="_blank" href="http://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives/#comment-47197">Aurélien&#8217;s recent comment</a>, where he mentions MathWorks so-called workaround for this problem, which (IMHO) is really not a work-around at all: MathWorks advises to modify our application to use &#8211; would you believe this &#8211; tabbed panels to &#8220;dock&#8221; the separate figures contents onto separate panels. Not to mention the fact that this so-called &#8220;solution&#8221; relies on undocumented and unsupported Matlab functionality (that of <a
target="_blank" href="http://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives/">tabbed-panels</a>) and requires major rework of existing applications, it also results in far inferior look-and-feel than simple docking as G-d intended&#8230;</p><p>Since I have demonstrated above that the docking functionality actually exists in compiled apps just as in the interpreted m-file apps, I do not understand why MathWorks took such great pains to prevent this important functionality in the compiler. There must be some important reason for this, but I cannot think of any. Perhaps if there is enough public demand, MathWorks will agree to return the docking functionality.</p><p>Unfortunately, I recently discovered that in the most recent compiler, that ships with R2011a, alternative #1 above (which uses pure Matlab) no longer works. Sometime between R2008a and R2011a MathWorks discovered my first back-door and closed it. Such a pity&#8230;</p><p>Luckily, alternative #2 (which uses the underlying Java frame object) seems to still work, even on R2011a.</p><p>I still haven&#8217;t tested this on R2011b&#8217;s compiler (whose pre-release has become available for download yesterday), but hopefully the trick above will continue to work on R2011b and on subsequent releases &#8211; please <a
href="http://undocumentedmatlab.com/blog/docking-figures-in-compiled-applications/#respond">tell me</a> if you find out otherwise.</p><p><pre> </pre>Related posts:<ol><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/findjobj-find-underlying-java-object/' rel='bookmark' title='FindJObj &#8211; find a Matlab component&#8217;s underlying Java object'>FindJObj &#8211; find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li><li><a
href='http://undocumentedmatlab.com/blog/blurred-matlab-figure-window/' rel='bookmark' title='Blurred Matlab figure window'>Blurred Matlab figure window</a> <small>Matlab figure windows can be blurred using a semi-transparent overlaid window - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/jmi-wrapper-local-matlabcontrol-part-2/' rel='bookmark' title='JMI wrapper &#8211; local MatlabControl part 2'>JMI wrapper &#8211; local MatlabControl part 2</a> <small>An example using matlabcontrol for calling Matlab from within a Java class is explained and discussed...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/docking-figures-in-compiled-applications/feed/</wfw:commentRss> <slash:comments>15</slash:comments> </item> <item><title>Plot-type selection components</title><link>http://undocumentedmatlab.com/blog/plot-type-selection-components/</link> <comments>http://undocumentedmatlab.com/blog/plot-type-selection-components/#comments</comments> <pubDate>Thu, 28 Apr 2011 14:48:23 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Internal component]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2270</guid> <description><![CDATA[Several built-in components enable programmatic plot-type selection in Matlab GUI - this article explains how<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/color-selection-components/' rel='bookmark' title='Color selection components'>Color selection components</a> <small>Matlab has several internal color-selection components that can easily be integrated in Matlab GUI...</small></li><li><a
href='http://undocumentedmatlab.com/blog/date-selection-components/' rel='bookmark' title='Date selection components'>Date selection components</a> <small>The JIDE package, pre-bundled in Matlab, contains several GUI controls for selecting dates - this article explains how they can be used...</small></li><li><a
href='http://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties/' rel='bookmark' title='Borderless button used for plot properties'>Borderless button used for plot properties</a> <small>A borderless button can be used to add unobtrusive functionality to plot axes...</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-status-bar-components/' rel='bookmark' title='Setting status-bar components'>Setting status-bar components</a> <small>Matlab status-bars are Java containers in which we can add GUI controls such as progress-bars, not just simple text labels...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Last week, Scott Hirsh, Matlab&#8217;s lead product manager, <a
rel="nofollow" href="http://blogs.mathworks.com/desktop/2011/04/18/redesigned-plot-catalog-in-matlab-r2011a/" target="_blank">posted an article</a> about Matlab&#8217;s redesigned plot catalog. In that post, Scott mentioned the internal deliberations about whether the plot catalog was needed at all. He explained how the final decision was to keep and enhance the catalog, rather than remove it, based on user feedback that the catalog is in fact useful, its main benefit being the fact that it is large and easy to use.</p><p>I posted a short <a
rel="nofollow" href="http://blogs.mathworks.com/desktop/2011/04/18/redesigned-plot-catalog-in-matlab-r2011a/#comment-7698" target="_blank">comment</a> mentioning my experience of using the catalog programmatically as another use-case that might be taken into consideration. Today, I would like to expand that short comment and explain how built-in plot-selection components can be used in our Matlab GUI programs.</p><h3 id="components">Built-in plot selection components</h3><p><code>PlotTypeCombo</code> is a plot-function selector is that is one of my personal favorite controls for embedding in a Matlab application:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Present the PlotCombo control</span>
import com.<span style="">mathworks</span>.<span style="">mlwidgets</span>.<span style="">graphics</span>.*
jPlotCombo = PlotTypeCombo;
pos = <span style="color: #080;">&#91;</span><span style="color: #33f;">100</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">170</span>,<span style="color: #33f;">50</span><span style="color: #080;">&#93;</span>;
<span style="color: #080;">&#91;</span>jhPlotCombo,hPanel<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span>jPlotCombo,pos,<span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jhPlotCombo,<span style="color:#A020F0;">'ActionPerformedCallback'</span>,@myCBFcn<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Callback function to process PlotCombo selections</span>
<span style="color: #0000FF;">function</span> myCBFcn<span style="color: #080;">&#40;</span>jObject,jEventData<span style="color: #080;">&#41;</span>
   newPlotFunc = jObject.<span style="">getSelectedItem</span>.<span style="">getName</span>.<span style="color: #0000FF;">char</span>;
   <span style="color: #228B22;">%Now do something useful with the selected function</span>
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% myCBFcn</span></pre></div></div><p><code>PlotCatalog</code> is a dialog window that presents the plot functions catalog:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">com.<span style="">mathworks</span>.<span style="">mlwidgets</span>.<span style="">graphics</span>.<span style="">PlotCatalog</span>.<span style="">getInstance</span>.<span style="">show</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 660px"><img
title="PlotTypeCombo" src="http://UndocumentedMatlab.com/images/PlotTypeCombo.png" alt="PlotTypeCombo" width="130" /> &nbsp;&nbsp; <img
title="PlotCatalog" src="http://UndocumentedMatlab.com/images/PlotCatalog.png" alt="PlotCatalog" width="440" /><p
class="wp-caption-text">PlotTypeCombo and PlotCatalog</p></div></center></p><h3 id="Usage">Usage example</h3><p>For one application that I developed for a client, I implemented a dynamic report page that enables users to select the plotting function using a <code>PlotTypeCombo</code>. When users select a non-default function (e.g. <i><b>stairs</b></i> or <i><b>loglog</b></i>), that function is automatically added to the combo-box for possible future reuse.  I implemented this as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Add the specified plotFunc to a PlotTypeCombo control</span>
<span style="color: #0000FF;">function</span> updatePlotCombo<span style="color: #080;">&#40;</span>plotCombo, plotFunc<span style="color: #080;">&#41;</span>
&nbsp;
   <span style="color: #228B22;">% Convert plotFunc (a Matlab string) into a Java PlotSignature</span>
   import com.<span style="">mathworks</span>.<span style="">mlwidgets</span>.<span style="">graphics</span>.*
   plotSig = PlotMetadata.<span style="">getPlotSignature</span><span style="color: #080;">&#40;</span>plotFunc<span style="color: #080;">&#41;</span>;
&nbsp;
   <span style="color: #228B22;">% Get the list of all existing plot types in the combo box</span>
   existingPlotTypes = <span style="color: #080;">&#123;</span><span style="color: #080;">&#125;</span>;
   <span style="color: #0000FF;">for</span> plotIdx = <span style="color: #33f;">0</span> <span style="color: #F0F;">:</span> plotCombo.<span style="">ItemCount</span>-<span style="color: #33f;">1</span>
      nextItem = plotCombo.<span style="">getItemAt</span><span style="color: #080;">&#40;</span>plotIdx<span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">if</span> isjava<span style="color: #080;">&#40;</span>nextItem<span style="color: #080;">&#41;</span>
         nextItem = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>nextItem.<span style="">getName</span><span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">end</span>
      existingPlotTypes = <span style="color: #080;">&#91;</span>existingPlotTypes, nextItem<span style="color: #080;">&#93;</span>;
   <span style="color: #0000FF;">end</span>
&nbsp;
   <span style="color: #228B22;">% If the new plotType is NOT already in the list</span>
   <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">strmatch</span><span style="color: #080;">&#40;</span>plotType,existingPlotTypes,<span style="color:#A020F0;">'exact'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
      <span style="color: #228B22;">% Add the new plotType to the list just prior to the end,</span>
      <span style="color: #228B22;">% so that &quot;More plots...&quot; will always be last</span>
      plotCombo.<span style="">insertItemAt</span><span style="color: #080;">&#40;</span>plotSig,plotCombo.<span style="">ItemCount</span>-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">end</span>
&nbsp;
   <span style="color: #228B22;">% Set the currently-selected item to be the requested plotType</span>
   <span style="color: #228B22;">% Note: temporarily disable callbacks to prevent involuntary action</span>
   plotCombo.<span style="">ActionPerformedCallback</span> = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>;
   plotCombo.<span style="">setSelectedItem</span><span style="color: #080;">&#40;</span>plotSig<span style="color: #080;">&#41;</span>;
   plotCombo.<span style="">ActionPerformedCallback</span> = @myCBFcn;  <span style="color: #228B22;">% selection callback</span>
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% updatePlotCombo</span></pre></div></div><p>Usage of this function would then be as simple as invoking the <i>updatePlotCombo()</i> function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">updatePlotCombo<span style="color: #080;">&#40;</span>jhPlotCombo,<span style="color:#A020F0;">'stairs'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 660px"><img
alt="Dynamic plot selection example" src="http://UndocumentedMatlab.com/images/IDS_DefsReports.png" title="Dynamic plot selection example" width="650" /><p
class="wp-caption-text">Dynamic plot selection example</p></div></center></p><h3 id="Signatures">Plot signatures</h3><p>The astute reader will have noticed from the <i>updatePlotCombo()</i> function above, that the <code>PlotTypeCombo</code> items are plot-signature objects. Different plot functions expect a different set of input arguments (a.k.a. <i>signature</i>). This information is kept and can be queried from the <code>PlotMetadata</code> class:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; com.<span style="">mathworks</span>.<span style="">mlwidgets</span>.<span style="">graphics</span>.<span style="">PlotMetadata</span>.<span style="">listAllSignatures</span>
<span style="color: #0000FF;">ans</span> =
<span style="color: #080;">&#91;</span><span style="color: #0000FF;">area</span>, <span style="color: #0000FF;">bar</span>, <span style="color: #0000FF;">bar</span> <span style="color: #080;">&#40;</span>stacked<span style="color: #080;">&#41;</span>, <span style="color: #0000FF;">barh</span> <span style="color: #080;">&#40;</span>stacked<span style="color: #080;">&#41;</span>, <span style="color: #0000FF;">barh</span>, <span style="color: #0000FF;">comet</span>, <span style="color: #0000FF;">compass</span>, <span style="color: #0000FF;">errorbar</span>, 
 <span style="color: #0000FF;">feather</span>, <span style="color: #0000FF;">loglog</span>, <span style="color: #0000FF;">plot</span>, <span style="color: #0000FF;">plot</span> N series, <span style="color: #0000FF;">plot</span> N series against T, <span style="color: #0000FF;">plot3</span>, <span style="color: #0000FF;">plotmatrix</span>, <span style="color: #0000FF;">plotyy</span>, 
 <span style="color: #0000FF;">polar</span>, <span style="color: #0000FF;">quiver</span>, <span style="color: #0000FF;">quiver3</span>, <span style="color: #0000FF;">ribbon</span>, <span style="color: #0000FF;">scatter</span>, <span style="color: #0000FF;">scatter3</span>, <span style="color: #0000FF;">semilogx</span>, <span style="color: #0000FF;">semilogy</span>, 
 <span style="color: #0000FF;">stairs</span>, <span style="color: #0000FF;">stem</span>, <span style="color: #0000FF;">stem3</span>, <span style="color: #0000FF;">null</span>, <span style="color: #0000FF;">contour</span>, contour3, <span style="color: #0000FF;">contourf</span>, <span style="color: #0000FF;">image</span>, 
 imagesc, <span style="color: #0000FF;">mesh</span>, <span style="color: #0000FF;">meshc</span>, meshz, <span style="color: #0000FF;">pcolor</span>, <span style="color: #0000FF;">plot</span> against first column, <span style="color: #0000FF;">surf</span>, <span style="color: #0000FF;">surfc</span>, 
 <span style="color: #0000FF;">surfl</span>, <span style="color: #0000FF;">waterfall</span>, <span style="color: #0000FF;">null</span>, <span style="color: #0000FF;">contour</span>, contour3, <span style="color: #0000FF;">contourf</span>, <span style="color: #0000FF;">image</span>, imagesc, 
 <span style="color: #0000FF;">mesh</span>, <span style="color: #0000FF;">meshc</span>, meshz, <span style="color: #0000FF;">pcolor</span>, <span style="color: #0000FF;">plot</span> against first column, <span style="color: #0000FF;">surf</span>, <span style="color: #0000FF;">surfc</span>, <span style="color: #0000FF;">surfl</span>, <span style="color: #0000FF;">waterfall</span><span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; plotFunc = <span style="color:#A020F0;">'surf'</span>;  <span style="color: #228B22;">% for example</span>
&gt;&gt; plotSig = PlotMetadata.<span style="">getPlotSignature</span><span style="color: #080;">&#40;</span>plotFunc<span style="color: #080;">&#41;</span>;
&gt;&gt; args = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>plotSig.<span style="">getArgs</span><span style="color: #080;">&#41;</span>';
args = 
    <span style="color: #080;">&#91;</span>1x1 com.<span style="">mathworks</span>.<span style="">mlwidgets</span>.<span style="">graphics</span>.<span style="">PlotArgDescriptor</span><span style="color: #080;">&#93;</span>    
    <span style="color: #080;">&#91;</span>1x1 com.<span style="">mathworks</span>.<span style="">mlwidgets</span>.<span style="">graphics</span>.<span style="">PlotArgDescriptor</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>1x1 com.<span style="">mathworks</span>.<span style="">mlwidgets</span>.<span style="">graphics</span>.<span style="">PlotArgDescriptor</span><span style="color: #080;">&#93;</span>    
    <span style="color: #080;">&#91;</span>1x1 com.<span style="">mathworks</span>.<span style="">mlwidgets</span>.<span style="">graphics</span>.<span style="">PlotArgDescriptor</span><span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; argsStruct = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>;  <span style="color: #228B22;">% initialize</span>
&gt;&gt; <span style="color: #0000FF;">for</span> argsIdx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>args<span style="color: #080;">&#41;</span>
      argsStruct<span style="color: #080;">&#40;</span>argsIdx<span style="color: #080;">&#41;</span>.<span style="color: #0000FF;">axis</span> = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>args<span style="color: #080;">&#123;</span>argsIdx<span style="color: #080;">&#125;</span>.<span style="">getAxis</span><span style="color: #080;">&#41;</span>;
      argsStruct<span style="color: #080;">&#40;</span>argsIdx<span style="color: #080;">&#41;</span>.<span style="">name</span> = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>args<span style="color: #080;">&#123;</span>argsIdx<span style="color: #080;">&#125;</span>.<span style="">getName</span><span style="color: #080;">&#41;</span>;
      argsStruct<span style="color: #080;">&#40;</span>argsIdx<span style="color: #080;">&#41;</span>.<span style="">label</span> = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>args<span style="color: #080;">&#123;</span>argsIdx<span style="color: #080;">&#125;</span>.<span style="">getLabel</span><span style="color: #080;">&#41;</span>;
      argsStruct<span style="color: #080;">&#40;</span>argsIdx<span style="color: #080;">&#41;</span>.<span style="">dims</span> = args<span style="color: #080;">&#123;</span>argsIdx<span style="color: #080;">&#125;</span>.<span style="">getNumDimensions</span>;
      argsStruct<span style="color: #080;">&#40;</span>argsIdx<span style="color: #080;">&#41;</span>.<span style="">reqObj</span> = args<span style="color: #080;">&#123;</span>argsIdx<span style="color: #080;">&#125;</span>.<span style="">getRequired</span>;
      req = argsStruct<span style="color: #080;">&#40;</span>argsIdx<span style="color: #080;">&#41;</span>.<span style="">reqObj</span>;
      argsStruct<span style="color: #080;">&#40;</span>argsIdx<span style="color: #080;">&#41;</span>.<span style="">requiredFlag</span> = req.<span style="">equals</span><span style="color: #080;">&#40;</span>req.<span style="">REQUIRED</span><span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">end</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span>argsStruct<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>			<span style="color: #228B22;">% Info about the first input argument</span>
            <span style="color: #0000FF;">axis</span><span style="color: #F0F;">:</span> <span style="color:#A020F0;">'X'</span>
            name<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'X'</span>
           label<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'X Data Source'</span>
            dims<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>2x1 <span style="color: #0000FF;">int32</span><span style="color: #080;">&#93;</span>
          reqObj<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 com.<span style="">mathworks</span>.<span style="">mlwidgets</span>.<span style="">graphics</span>.<span style="">PlotArgDescriptor</span>$RequiredType<span style="color: #080;">&#93;</span>
    requiredFlag<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#123;</span>argsStruct.<span style="color: #0000FF;">axis</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>		<span style="color: #228B22;">% axis info of all input arguments</span>
    <span style="color:#A020F0;">'X'</span>    <span style="color:#A020F0;">'Y'</span>    <span style="color:#A020F0;">'Z'</span>    <span style="color:#A020F0;">''</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#123;</span>argsStruct.<span style="">name</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>		<span style="color: #228B22;">% name of all input arguments</span>
    <span style="color:#A020F0;">'X'</span>    <span style="color:#A020F0;">'Y'</span>    <span style="color:#A020F0;">'Z'</span>    <span style="color:#A020F0;">'C'</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#123;</span>argsStruct.<span style="">label</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>	<span style="color: #228B22;">% data label of all input arguments</span>
    <span style="color:#A020F0;">'X Data Source'</span>    <span style="color:#A020F0;">'Y Data Source'</span>    <span style="color:#A020F0;">'Z Data Source'</span>    <span style="color:#A020F0;">'Color'</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#123;</span>argsStruct.<span style="">dims</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>		<span style="color: #228B22;">% dimensionality of all input args</span>
    <span style="color: #080;">&#91;</span>2x1 <span style="color: #0000FF;">int32</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span>2x1 <span style="color: #0000FF;">int32</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">2</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">2</span><span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#123;</span>argsStruct.<span style="">requiredFlag</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>	<span style="color: #228B22;">% is any input arg mandatory?</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span><span style="color: #080;">&#93;</span></pre></div></div><p>This signature data can be queried for each separate plot type. In fact, I use this mechanism above so that whenever the user changes the plot-type in the <code>PlotTypeCombo</code>, the usage label text beneath the combo-box is updated with the specific plot&#8217;s signature information:</p><p><center><div
class="wp-caption aligncenter" style="width: 410px"><img
alt="Dynamic plot signature data" src="http://UndocumentedMatlab.com/images/IDS_DefsReportsDetails.png" title="Dynamic plot signature data" width="400" /><p
class="wp-caption-text">Dynamic plot signature data</p></div></center></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/color-selection-components/' rel='bookmark' title='Color selection components'>Color selection components</a> <small>Matlab has several internal color-selection components that can easily be integrated in Matlab GUI...</small></li><li><a
href='http://undocumentedmatlab.com/blog/date-selection-components/' rel='bookmark' title='Date selection components'>Date selection components</a> <small>The JIDE package, pre-bundled in Matlab, contains several GUI controls for selecting dates - this article explains how they can be used...</small></li><li><a
href='http://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties/' rel='bookmark' title='Borderless button used for plot properties'>Borderless button used for plot properties</a> <small>A borderless button can be used to add unobtrusive functionality to plot axes...</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-status-bar-components/' rel='bookmark' title='Setting status-bar components'>Setting status-bar components</a> <small>Matlab status-bars are Java containers in which we can add GUI controls such as progress-bars, not just simple text labels...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/plot-type-selection-components/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/findjobj-find-underlying-java-object/' rel='bookmark' title='FindJObj &#8211; find a Matlab component&#8217;s underlying Java object'>FindJObj &#8211; find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li><li><a
href='http://undocumentedmatlab.com/blog/fixing-a-java-focus-problem/' rel='bookmark' title='Fixing a Java focus problem'>Fixing a Java focus problem</a> <small>Java components added to Matlab GUIs do not participate in the standard focus cycle - this article explains how to fix this problem....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Once again I welcome Donn Shull, who concludes his series on Matlab&#8217;s undocumented UDD mechanism with a discussion of the UDD-Java relationship.</i></p><h3 id="Introduction">Introduction to the UDD-Java relationship</h3><p>Over the course of this series we have mentioned connections between UDD and Java. In <a
target="_blank" href="http://undocumentedmatlab.com/blog/udd-events-and-listeners/">UDD Events and Listeners</a> we described how in Matlab each Java object can have a UDD companion. In <a
target="_blank" href="http://undocumentedmatlab.com/blog/hierarchical-systems-with-udd/">Hierarchical Systems with UDD</a> we briefly noted that a UDD hierarchy may be passed to Java. This suggests that there is a two way relationship between between UDD and Java.</p><p>In this article we will use some undocumented built-in methods such as <b><i>java</i></b> and <b><i>classhandle</i></b> to explore the UDD-Java relationship. We have used built-in methods for UDD objects before. We have also mentioned the importance of studying code from The MathWorks. When you come across something that looks like it may be a UDD method you can check with the <b><i>which</i></b> command:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">which</span> java -<span style="color: #0000FF;">all</span>
C<span style="color: #F0F;">:</span>\MATLAB\R2010b\toolbox\matlab\general\java.<span style="">m</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% javahandle.com.mathworks.hg.peer.Echo method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% ui.figure method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% hg2utils.HGHandle method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% JavaVisible method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% hg.figure method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% hg.GObject method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% schema.class method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% handle.handle method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% schema.method method</span>
C<span style="color: #F0F;">:</span>\MATLAB\R2010b\toolbox\rptgen\rptgen\@sgmltag\java.<span style="">m</span>  <span style="color: #228B22;">% sgmltag method</span></pre></div></div><p>If you find <code>schema.class</code> in the comments for built-in methods, then the method is a general UDD method.</p><h3 id="javahandle">UDD javahandle companions for Java object</h3><p>Whenever a Java class is instantiated in Matlab, it is possible to <a
target="_blank" href="http://undocumentedmatlab.com/blog/udd-events-and-listeners/#Java">create a companion UDD object</a>. The created companion can be in either the <code>javahandle</code> or the <code>javahandle_withcallbacks</code> package. The primary reason for creating the companion object is to avoid <a
rel="nofollow" target="_blank" href="http://mathforum.org/kb/message.jspa?messageID=5950839">memory leaks</a> when attaching a Matlab callback to a callback property. It makes sense in general to use the <code>javahandle_withcallbacks</code> package.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #228B22;">% creage a java instance and the companion UDD object</span>
&gt;&gt; javaFrame = javax.<span style="">swing</span>.<span style="">JFrame</span>;
&nbsp;
&gt;&gt; <span style="color: #228B22;">% dot notation</span>
&gt;&gt; javaFrameUDD = javaFrame.<span style="">handle</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;
&nbsp;
&gt;&gt; <span style="color: #228B22;">% or Matlab notation</span>
&gt;&gt; javaFrameUDD = handle<span style="color: #080;">&#40;</span>javaFrame,<span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>We can use the built-in <b><i>classhandle</i></b> method to inspect our UDD companion object. This can be used, for example, to obtain a list of the events that the Java class generates:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #228B22;">% use classhandle to list a java classes events</span>
&gt;&gt; jch = javaFrame.<span style="">handle</span>.<span style="">classhandle</span>;
&nbsp;
&gt;&gt; <span style="color: #0000FF;">for</span> index = <span style="color: #33f;">1</span><span style="color: #F0F;">:</span>numel<span style="color: #080;">&#40;</span>jch.<span style="">Events</span><span style="color: #080;">&#41;</span>, <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span>jch.<span style="">Events</span><span style="color: #080;">&#40;</span>index<span style="color: #080;">&#41;</span>.<span style="">Name</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">end</span>
MouseWheelMoved
MouseClicked
MouseEntered
MouseExited
MousePressed
MouseReleased
WindowGainedFocus
WindowLostFocus
WindowActivated
WindowClosed
WindowClosing
WindowDeactivated
WindowDeiconified
WindowIconified
WindowOpened
ComponentHidden
ComponentMoved
ComponentResized
ComponentShown
MouseDragged
MouseMoved
ComponentAdded
ComponentRemoved
AncestorMoved
AncestorResized
FocusGained
FocusLost
WindowStateChanged
HierarchyChanged
CaretPositionChanged
InputMethodTextChanged
PropertyChange
KeyPressed
KeyReleased
KeyTyped
&nbsp;
&gt;&gt; <span style="color: #228B22;">% Use one of the object's callbacks</span>
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>javaFrameUDD,<span style="color:#A020F0;">'WindowGainedFocusCallback'</span>,@myCallbackFcn<span style="color: #080;">&#41;</span>;</pre></div></div><p>If we do not wish to use callback properties, then we can create our UDD companion in the <code>javahandle</code> package and use <b><i>handle.listener</i></b> to respond to events.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">javaFrame = javax.<span style="">swing</span>.<span style="">JFrame</span>;
javaFrameUDD = javaFrame.<span style="">handle</span>;
lis = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>javaFrameUDD,<span style="color:#A020F0;">'WindowGainedFocus'</span>,@myCallbackFcn<span style="color: #080;">&#41;</span>;</pre></div></div><h3 id="Passing">Passing UDD objects to Java code</h3><p>You can pass any UDD object to your Java classes in Matlab. Matlab will create a Java bean adapter for the UDD object. The bean adapter created is a subclass of <code>com.MathWorks.jmi.bean.UDDObject</code>. <code>UDDObject</code> implements the Java interfaces <code>com.MathWorks.jmi.bean.DynamicProperties</code>, <code>com.MathWorks.jmi.bean.MTObject</code>, <code>com.MathWorks.jmi.bean.TreeObject</code>, and <code>com.mathworks.services.Browseable</code>.</p><p>The generated bean adapter will have the methods of the parent class the methods of the UDD class, as well as <i>set</i> and <i>get</i> methods for the class properties. To understand how this works, let&#8217;s start with our <code>simple.object</code> and use the <b><i>java</i></b> method to inspect the bean adapter:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myObj = simple.<span style="">object</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'myObj'</span>, <span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;
&nbsp;
&gt;&gt; <span style="color: #228B22;">% using dot notation with the java method </span>
&gt;&gt; myObj.<span style="">java</span>.<span style="">getClass</span>
<span style="color: #0000FF;">ans</span> =
<span style="color: #0000FF;">class</span> objectBeanAdapter0
&nbsp;
&gt;&gt; myObj.<span style="">java</span>.<span style="color: #0000FF;">methods</span>
&nbsp;
<span style="color: #0000FF;">Methods</span> <span style="color: #0000FF;">for</span> <span style="color: #0000FF;">class</span> objectBeanAdapter0<span style="color: #F0F;">:</span>
&nbsp;
acquireReference                    createNullMatlabObjectListener      lastDown                            
addBelow                            createNullPropertyChangeListener    left                                
addBrowseableListener               <span style="color: #0000FF;">dialog</span>                              notify                              
addFirstBelow                       <span style="color: #0000FF;">disp</span>                                notifyAll                           
addLeft                             dispose                             objectBeanAdapter0                  
addMatlabObjectListener             equals                              releaseReference                    
addObjectPropertyChangeListener     findProperty                        removeBrowseableListener            
addRight                            firstDown                           removeMatlabObjectListener          
browseableCanHaveChildren           getChildAt                          removeObjectPropertyChangeListener  
browseableChild                     getChildCount                       right                               
browseableChildCount                getClass                            setDirtyFlag                        
browseableChildFetchCount           getClassName                        setDynamicPropertyValue             
browseableChildren                  getDynamicProperties                setName                             
browseableDataObject                getDynamicPropertyValue             setPropertyValue                    
browseableDisplayObject             getIndex                            setThreadSafetyCheckLevel           
browseableHasChildren               getIndexOfChild                     setValue                            
browseableNChildren                 getName                             toString                            
browseableNextNSiblings             getNewInstance                      up                                  
browseableNextSibling               getPropertyValue                    updateCache                         
browseableParent                    getValue                            updateChildCount                    
browseablePrevNSiblings             hashCode                            updateIndex                         
browseablePrevSibling               isDirty                             wait                                
checkThreadSafety                   isLeaf                              
clearDirtyFlag                      isObservable                        
compareTo                           isValid</pre></div></div><p>The parent class has added a large number of methods to the bean adapter for our original class. By looking at the list we can see our <i>dialog</i> and <i>disp</i> methods. There are also <i>getName</i>, <i>setName</i>, <i>getValue</i>, and <i>setValue</i> methods for our classes properties. The rest of the methods were inherited from the base <code>UDDObject</code> superclass. We can use any superclass method directly with the bean adapter object. For example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myObj.<span style="">java</span>.<span style="">getPropertyValue</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Name'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
myObj</pre></div></div><h3 id="Interface">Java interface class</h3><p>To be able to use our UDD object in user-written Java code, we need a Java interface class for it. While we could manually write an interface file, UDD provides a very handy convenience method to automatically create the interface file. For this, we use the <b><i>classhandle</i></b> method again. The <code>schema.class</code> object obtained using <b><i>classhandle</i></b> has a method called <i>createJavaInterface</i> that takes two string arguments: the Java interface classname, and the folder in which to place the interface file. The steps to create and use this interface file are:</p><ol><li>Create and test your UDD class</li><li>Create a Java interface file using <code>schema.class</code>&#8216;s <i>CreateJavaInterface</i></li><li>Modify your UDD class definition file (schema.m) to reference the Java interface file</li><li>Create the Java code that uses your class</li></ol><p>For example, to create a Java interface file for the simple object we created above, use the following commands in Matlab:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">classH = classhandle<span style="color: #080;">&#40;</span>myObj<span style="color: #080;">&#41;</span>;
classH.<span style="">createJavaInterface</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'simpleObjectInterface'</span>,<span style="color: #0000FF;">pwd</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>This will create the following simpleObjectInterface.java file in the current working directory:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> simpleObjectInterface 
       <span style="color: #000000; font-weight: bold;">extends</span> com.<span style="color: #006633;">mathworks</span>.<span style="color: #006633;">jmi</span>.<span style="color: #006633;">bean</span>.<span style="color: #006633;">TreeObject</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/* Properties */</span>
    <span style="color: #000000; font-weight: bold;">public</span> java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> getValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setValue<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/* Methods */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> dialog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> disp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>The interface file contains <i>set</i> and <i>get</i> accessor methods for our UDD object properties, and Java prototypes for the UDD methods (in our case, <i>dialog</i> and <i>disp</i>).</p><p>The next step is to modify our class definition file (schema.m) to reference the Java interface file we have created. This modification provides the information that Matlab needs to create the bean adapter that implements the Java interface:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">simpleClass = schema.<span style="color: #0000FF;">class</span><span style="color: #080;">&#40;</span>simplePackage, <span style="color:#A020F0;">'object'</span><span style="color: #080;">&#41;</span>;
simpleClass.<span style="">JavaInterfaces</span> = <span style="color: #080;">&#123;</span> <span style="color:#A020F0;">'simpleObjectInterface'</span> <span style="color: #080;">&#125;</span>;</pre></div></div><p>We can verify that the generated bean adapter implements the interface using Java Reflection techniques. As always when we have made changes to the class definition file, we need to use the <b><i>clear classes</i></b> command, and then recreate our objects:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myObj = simple.<span style="">object</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'myObj'</span>, <span style="color: #0000FF;">pi</span><span style="color: #080;">&#41;</span>
myObj =
  Name<span style="color: #F0F;">:</span> myObj
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">3.141593</span>
&nbsp;
&gt;&gt; myObjBean = java<span style="color: #080;">&#40;</span>myObj<span style="color: #080;">&#41;</span> 
myObjBean =
  Name<span style="color: #F0F;">:</span> myObj
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">3.141593</span>
&nbsp;
&gt;&gt; interfaces = myObjBean.<span style="">getClass</span>.<span style="">getInterfaces</span> 
interfaces =
java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #F0F;">:</span>
    <span style="color: #080;">&#91;</span>java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span><span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; interfaces<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> 
<span style="color: #0000FF;">ans</span> =
interface simpleObjectInterface</pre></div></div><h3 id="Usage">Using UDD in Java</h3><p>Let&#8217;s create a simple Java class that illustrates passing a UDD object to Java. Here we will just have two methods: The first gets the Value property from a class instance and doubles it; the second launches the class instance dialog:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> accessUDDClass 
<span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">double</span> localValue<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> accessUDDClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doubleValue<span style="color: #009900;">&#40;</span>simpleObjectInterface UDDObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    localValue <span style="color: #339933;">=</span> UDDObj.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    UDDObj.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">*</span>localValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> launchDialog<span style="color: #009900;">&#40;</span>simpleObjectInterface UDDObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    UDDObj.<span style="color: #006633;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>If we have set up the Java compiler and environment variables correctly, we can compile our interface and Java class files from inside Matlab using the system command (alternately, we can compile using any external Java compiler or IDE):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; system<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javac accessUDDClass.java simpleObjectInterface.java'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
     <span style="color: #33f;">0</span></pre></div></div><p>Now test our simple Java class with the UDD object created earlier:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; javaObj = accessUDDClass
javaObj =
accessUDDClass@eb9b73
&nbsp;
&gt;&gt; javaObj.<span style="">doubleValue</span><span style="color: #080;">&#40;</span>myObj<span style="color: #080;">&#41;</span>  <span style="color: #228B22;">% pi =&gt; 2*pi</span>
&nbsp;
&gt;&gt; myObj
myObj =
  Name<span style="color: #F0F;">:</span> myObj
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">6.283185</span></pre></div></div><p>This concludes the UDD series. I would like to thank Yair for his help in preparing and presenting this information.</p><h3 id="Editor">Editor&#8217;s note</h3><p><i>I would like to thank Donn for his enourmously detailed work on UDD, and for preparing it in easy-to-follow articles. I can personally attest to the huge time investment it has taken him. I trully believe he deserves a warm &#8220;thank you&#8221; from the Matlab community. Please visit <a
target="_blank" rel="nofollow" href="http://aetoolbox.com/default.aspx">Donn&#8217;s website</a>, or add a short <a
href="http://UndocumentedMatlab.com/blog/udd-and-java/#respond">comment</a> below.</p><p>In the following weeks, I return to the regular stuff that made this website famous: solving day-to-day Matlab problems using simple undocumented built-in Matlab gems.<br/>- Yair</i></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/' rel='bookmark' title='Matlab callbacks for Java events'>Matlab callbacks for Java events</a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/jmi-java-to-matlab-interface/' rel='bookmark' title='JMI &#8211; Java-to-Matlab Interface'>JMI &#8211; Java-to-Matlab Interface</a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/' rel='bookmark' title='FindJObj &#8211; find a Matlab component&#8217;s underlying Java object'>FindJObj &#8211; find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li><li><a
href='http://undocumentedmatlab.com/blog/fixing-a-java-focus-problem/' rel='bookmark' title='Fixing a Java focus problem'>Fixing a Java focus problem</a> <small>Java components added to Matlab GUIs do not participate in the standard focus cycle - this article explains how to fix this problem....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/udd-and-java/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>JGraph and BDE</title><link>http://undocumentedmatlab.com/blog/jgraph-and-bde/</link> <comments>http://undocumentedmatlab.com/blog/jgraph-and-bde/#comments</comments> <pubDate>Wed, 26 Jan 2011 18:00:18 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Internal component]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2063</guid> <description><![CDATA[This article shows how to display graph-theory diagrams in Matlab using several different Java libraries<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/syntax-highlighted-labels-panels/' rel='bookmark' title='Syntax highlighted labels &amp; panels'>Syntax highlighted labels &#038; panels</a> <small>Syntax-highlighted labels and edit-boxes can easily be displayed in Matlab GUI - this article explains how. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-type-selection-components/' rel='bookmark' title='Plot-type selection components'>Plot-type selection components</a> <small>Several built-in components enable programmatic plot-type selection in Matlab GUI - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-sorting/' rel='bookmark' title='Uitable sorting'>Uitable sorting</a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li><li><a
href='http://undocumentedmatlab.com/blog/advanced-jide-property-grids/' rel='bookmark' title='Advanced JIDE Property Grids'>Advanced JIDE Property Grids</a> <small>JIDE property grids can use complex cell renderer and editor components and can signal property change events asynchronously to Matlab callbacks...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Last week, when I presented <a
target="_blank" href="http://undocumentedmatlab.com/blog/jfreechart-graphs-and-gauges/">JFreeChart</a>, I intended to follow it with other articles dedicated to JGraph, JFreeReport and other useful open-source packages that can be used from within Matlab. However, blog follower Scott Koch was quick to <a
target="_blank" href="http://undocumentedmatlab.com/blog/jfreechart-graphs-and-gauges/#comment-30052">post a comment</a> about JGraph and even took the time to add a short code snippet illustrating JGraph&#8217;s use. So, I will honor Scott by re-posting his example here.</p><h3 id="JGraph">JGraph</h3><p><a
target="_blank" rel="nofollow" href="http://www.jgraph.com/jgraph.html">JGraph</a> is an open-source Java library that helps visualize graph-theory objects and connectors. Note that in today&#8217;s article we discuss graphs in the sense of connected object trees, not graphs in the sense of x-vs.-y data charts.</p><p>To use JGraph, <a
target="_blank" rel="nofollow" href="http://www.jgraph.com/jgraphdownload.html">download</a> the JGraphx zip file,  then extract the zip file. You&#8217;ll get a jgraphx/ subfolder containing separate sub-folders with docs, examples, source-code and a lib/ sub-folder with a jgraphx.jar file. To use JGraph in Matlab, load jgraphx.jar into Matlab&#8217;s Java classpath (there is no need to use the static classpath in this case, although you can of course do so if you like):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">javaaddpath<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'jgraphx/lib/jgraphx.jar'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>We can now run Scott Koch&#8217;s example of using JGraph in Matlab:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Make the graph object</span>
graph = com.<span style="">mxgraph</span>.<span style="color: #0000FF;">view</span>.<span style="">mxGraph</span>;
&nbsp;
<span style="color: #228B22;">% Get the parent cell</span>
parent = graph.<span style="">getDefaultParent</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Group update</span>
graph.<span style="">getModel</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>.<span style="">beginUpdate</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Add some child cells</span>
v1 = graph.<span style="">insertVertex</span><span style="color: #080;">&#40;</span>parent, <span style="color:#A020F0;">''</span>, <span style="color:#A020F0;">'Hello'</span>, <span style="color: #33f;">240</span>, <span style="color: #33f;">150</span>, <span style="color: #33f;">80</span>, <span style="color: #33f;">30</span><span style="color: #080;">&#41;</span>;
v2 = graph.<span style="">insertVertex</span><span style="color: #080;">&#40;</span>parent, <span style="color:#A020F0;">''</span>, <span style="color:#A020F0;">'World'</span>, <span style="color: #33f;">20</span>,   <span style="color: #33f;">20</span>, <span style="color: #33f;">80</span>, <span style="color: #33f;">30</span><span style="color: #080;">&#41;</span>;
graph.<span style="">insertEdge</span><span style="color: #080;">&#40;</span>parent, <span style="color:#A020F0;">''</span>, <span style="color:#A020F0;">'Edge'</span>, v1, v2<span style="color: #080;">&#41;</span>;
graph.<span style="">getModel</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>.<span style="">endUpdate</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Get scrollpane</span>
graphComponent = com.<span style="">mxgraph</span>.<span style="">swing</span>.<span style="">mxGraphComponent</span><span style="color: #080;">&#40;</span>graph<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Make a figure and stick the component on it</span>
f = <span style="color: #0000FF;">figure</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'pixels'</span><span style="color: #080;">&#41;</span>;
pos = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>f,<span style="color:#A020F0;">'position'</span><span style="color: #080;">&#41;</span>; 
mypanel = javax.<span style="">swing</span>.<span style="">JPanel</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">BorderLayout</span><span style="color: #080;">&#41;</span>;
mypanel.<span style="">add</span><span style="color: #080;">&#40;</span>graphComponent<span style="color: #080;">&#41;</span>;
<span style="color: #080;">&#91;</span>obj, hcontainer<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span>mypanel, <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,pos<span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #F0F;">:</span><span style="color: #33f;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>, f<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 443px"><img
alt="JGraph example within a Matlab figure (the graph is fully interactive)" src="http://UndocumentedMatlab.com/images/JGraph_Matlab_example.png" title="JGraph example within a Matlab figure (the graph is fully interactive)" width="433" height="275" /><p
class="wp-caption-text">JGraph example within a Matlab figure (the graph is fully interactive)</p></div></center></p><p>And some more complex JGraph examples, taken from the &#8220;official&#8221; samples gallery:</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><a
target="_blank" href="http://UndocumentedMatlab.com/images/JGraph_JDOgenie.png"><img
alt="JGraph sample (click for details)" src="http://UndocumentedMatlab.com/images/JGraph_JDOgenie.png" title="JGraph sample (click for details)" width="450" height="343" /></a><br/><a
target="_blank" href="http://UndocumentedMatlab.com/images/JGraph_yawleditor.png"><img
alt="JGraph sample (click for details)" src="http://UndocumentedMatlab.com/images/JGraph_yawleditor.png" title="JGraph sample (click for details)" width="450" height="308" /></a><p
class="wp-caption-text">JGraph samples (click for details)</p></div></center></p><h3 id="JGraphT">JGraphT</h3><p><a
target="_blank" rel="nofollow" href="http://jgrapht.sourceforge.net/">JGraphT</a> is another open-source Java-based library. JGraphT is a non-GUI library that provides underlying graph-theory objects and algorithms. These can be used in conjunction with the JGraph visualization library.</p><p>The Matlab File Exchange has a <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/27074-maximal-independent-sets-using-jgrapht">submission that wraps JGraphT within Matlab</a>. Apparently, this Matlab utility is a wrapper for the non-GUI JGraphT, but I see no reason not to use JGraph&#8217;s GUI visualization capabilities, since JGraphT has an extremely simple JGraph adapter.</p><h3 id="BDE">BDE</h3><p>BDE (Block Diagram Editor?) is an internal Matlab Java class library that has been included in Matlab releases for many years, to this day. Judging from the size of this library file (%matlabroot%/java/jar/bde.jar), MathWorks continues to develop BDE, which grows from one release to another.</p><p>BDE can be used to present and edit block diagrams. The diagrams themselves are stored in XML files in a BDE/ subfolder beneath the user&#8217;s <i><b>prefdir</b></i> folder. However, I do not know how these diagrams can be used. Here&#8217;s a simple usage example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">bde = com.<span style="">mathworks</span>.<span style="">bde</span>.<span style="">clients</span>.<span style="">BDEDesktop</span>;  <span style="color: #228B22;">% fails in R2009b+</span>
diagram = com.<span style="">mathworks</span>.<span style="">bde</span>.<span style="">diagram</span>.<span style="">Diagram</span>;
client = com.<span style="">mathworks</span>.<span style="">bde</span>.<span style="">clients</span>.<span style="">DiagramViewDTClient</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'YMA'</span>,diagram<span style="color: #080;">&#41;</span>;
bde.<span style="">addClient</span><span style="color: #080;">&#40;</span>client,<span style="color:#A020F0;">'YMA'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 460px"><a
target="_blank" href="http://UndocumentedMatlab.com/images/BDE.png"><img
alt="BDE sample diagram (click for details)" src="http://UndocumentedMatlab.com/images/BDE.png" title="BDE sample diagram (click for details)" width="450" height="309" /></a><p
class="wp-caption-text">BDE sample diagram (click for details)</p></div></center></p><p>Note: In Matlab release R2009b, BDE apparently had a major redesign, and the above code no longer works.</p><p>I would be happy if anyone could enlighten me regarding the possible usage of BDE, or how it can be ran in one of the latest releases. Please leave a comment <a
href="http://undocumentedmatlab.com/blog/jgraph-and-bde/#respond">below</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/syntax-highlighted-labels-panels/' rel='bookmark' title='Syntax highlighted labels &amp; panels'>Syntax highlighted labels &#038; panels</a> <small>Syntax-highlighted labels and edit-boxes can easily be displayed in Matlab GUI - this article explains how. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-type-selection-components/' rel='bookmark' title='Plot-type selection components'>Plot-type selection components</a> <small>Several built-in components enable programmatic plot-type selection in Matlab GUI - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-sorting/' rel='bookmark' title='Uitable sorting'>Uitable sorting</a> <small>Matlab's uitables can be sortable using simple undocumented features...</small></li><li><a
href='http://undocumentedmatlab.com/blog/advanced-jide-property-grids/' rel='bookmark' title='Advanced JIDE Property Grids'>Advanced JIDE Property Grids</a> <small>JIDE property grids can use complex cell renderer and editor components and can signal property change events asynchronously to Matlab callbacks...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/jgraph-and-bde/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> <item><title>Matlab callbacks for Java events</title><link>http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/</link> <comments>http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/#comments</comments> <pubDate>Tue, 30 Nov 2010 22:09:35 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[Listener]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1987</guid> <description><![CDATA[Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/udd-events-and-listeners/' rel='bookmark' title='UDD Events and Listeners'>UDD Events and Listeners</a> <small>UDD event listeners can be used to listen to property value changes and other important events of Matlab objects...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool-callbacks/' rel='bookmark' title='uisplittool &amp; uitogglesplittool callbacks'>uisplittool &#038; uitogglesplittool callbacks</a> <small>Matlab's undocumented uisplittool and uitogglesplittool are powerful toolbar controls - this article explains how to customize their behavior...</small></li><li><a
href='http://undocumentedmatlab.com/blog/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><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></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/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><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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/feed/</wfw:commentRss> <slash:comments>26</slash:comments> </item> <item><title>Customizing Workspace context-menu</title><link>http://undocumentedmatlab.com/blog/customizing-workspace-context-menu/</link> <comments>http://undocumentedmatlab.com/blog/customizing-workspace-context-menu/#comments</comments> <pubDate>Wed, 03 Nov 2010 18:00:17 +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[Undocumented feature]]></category> <category><![CDATA[Internal component]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1943</guid> <description><![CDATA[Matlab's Workspace table context-menu can be configured with user-defined actions - this article explains how.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/adding-context-menu-to-uitree/' rel='bookmark' title='Adding a context-menu to a uitree'>Adding a context-menu to a uitree</a> <small>uitree is an undocumented Matlab function, which does not easily enable setting a context-menu. Here's how to do it....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/' rel='bookmark' title='Customizing Matlab&#8217;s Workspace table'>Customizing Matlab&#8217;s Workspace table</a> <small>The Matlab Desktop's Workspace pane table can be customized, as described here...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-help-popup-contents/' rel='bookmark' title='Customizing help popup contents'>Customizing help popup contents</a> <small>The built-in HelpPopup, available since Matlab R2007b, has a back-door that enables displaying arbitrary text, HTML and URL web-pages....</small></li><li><a
href='http://undocumentedmatlab.com/blog/context-sensitive-help/' rel='bookmark' title='Context-Sensitive Help'>Context-Sensitive Help</a> <small>Matlab has a hidden/unsupported built-in mechanism for easy implementation of context-sensitive help...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>My first article of 2010 described <a
target="_blank" href="http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/">customizing Matlab&#8217;s Workspace table</a>, in particular the values presented in the Bytes column.</p><p>Last week, a reader of that article <a
target="_blank" href="http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/#comment-21170">posted a comment</a> asking how to customize the context (right-click) menu with some user-defined actions. The user has found that modifying the menu via the regular table handles gets reset automatically.</p><p>Today&#8217;s article will describe an easy and effective way to add user-defined actions to the Workspace table, for specific object types.</p><p>The trick is not to modify the context-menu directly. As the reader above has noticed, this menu is automatically recreated on the fly, so no change will be persistent. Instead, we modify Matlab&#8217;s internal registry of class-specific context-menus. This is done in several prefspanel.m files in the Matlab codebase (For example: \toolbox\matlab\audiovideo\prefspanel.m and \toolbox\signal\signal\prefspanel.m).</p><p>The mechanism relies on the following Java method, which is unsupported and undocumented, yet has existed in the present form for the past several releases:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">classes = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'double'</span>, <span style="color:#A020F0;">'java.lang.Object'</span><span style="color: #080;">&#125;</span>;
menuName = <span style="color:#A020F0;">'My context-menu'</span>;
menuItems = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'Inspect'</span>, <span style="color:#A020F0;">'Properties'</span>, <span style="color:#A020F0;">'-'</span>, <span style="color:#A020F0;">'class name'</span><span style="color: #080;">&#125;</span>;
menuActions = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'inspect($1)'</span>, <span style="color:#A020F0;">'get($1)'</span>, <span style="color:#A020F0;">''</span>, <span style="color:#A020F0;">'class($1)'</span><span style="color: #080;">&#125;</span>;
com.<span style="">mathworks</span>.<span style="">mlwidgets</span>.<span style="color: #0000FF;">workspace</span>.<span style="">MatlabCustomClassRegistry</span>.<span style="">registerClassCallbacks</span><span style="color: #080;">&#40;</span>classes,menuName,menuItems,menuActions<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 366px"><img
alt="Customizable Workspace table context-menu" src="http://UndocumentedMatlab.com/images/Workspace_ContextMenu.png" title="Customizable Workspace table context-menu" width="356" height="329" /><p
class="wp-caption-text">Customizable Workspace table context-menu</p></div></center></p><p>Once you have found your particular context-menu configuration useful, place the short customization code in your <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/loren/2009/03/03/whats-in-your-startupm/">startup.m file</a> so that the change becomes permanent in all future Matlab sessions.</p><p>A few other supporting static methods are available in the com.mathworks.mlwidgets.workspace.MatlabCustomClassRegistry class: <i>getClassCallbacksInformation (className)</i>, <i>registerSimilarClassCallbacks(newClassNames,definedClassName)</i> and <i>unregisterClassCallbacks(definedClassName)</i>. For example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">com.<span style="">mathworks</span>.<span style="">mlwidgets</span>.<span style="color: #0000FF;">workspace</span>.<span style="">MatlabCustomClassRegistry</span>.<span style="">getClassCallbacksInformation</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'double'</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">ans</span> =
java.<span style="">lang</span>.<span style="">Object</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #F0F;">:</span>
    <span style="color:#A020F0;">'My context-menu'</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">4</span> element array<span style="color: #080;">&#93;</span>        <span style="color: #228B22;">% = menuItems</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">4</span> element array<span style="color: #080;">&#93;</span>        <span style="color: #228B22;">% = menuActions</span></pre></div></div><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/adding-context-menu-to-uitree/' rel='bookmark' title='Adding a context-menu to a uitree'>Adding a context-menu to a uitree</a> <small>uitree is an undocumented Matlab function, which does not easily enable setting a context-menu. Here's how to do it....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/' rel='bookmark' title='Customizing Matlab&#8217;s Workspace table'>Customizing Matlab&#8217;s Workspace table</a> <small>The Matlab Desktop's Workspace pane table can be customized, as described here...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-help-popup-contents/' rel='bookmark' title='Customizing help popup contents'>Customizing help popup contents</a> <small>The built-in HelpPopup, available since Matlab R2007b, has a back-door that enables displaying arbitrary text, HTML and URL web-pages....</small></li><li><a
href='http://undocumentedmatlab.com/blog/context-sensitive-help/' rel='bookmark' title='Context-Sensitive Help'>Context-Sensitive Help</a> <small>Matlab has a hidden/unsupported built-in mechanism for easy implementation of context-sensitive help...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/customizing-workspace-context-menu/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Undocumented cursorbar object</title><link>http://undocumentedmatlab.com/blog/undocumented-cursorbar-object/</link> <comments>http://undocumentedmatlab.com/blog/undocumented-cursorbar-object/#comments</comments> <pubDate>Wed, 29 Sep 2010 18:00:12 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[schema.class]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1903</guid> <description><![CDATA[Matlab's internal undocumented graphics.cursorbar object can be used to present dynamic data-tip cross-hairs<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/' rel='bookmark' title='getundoc &#8211; get undocumented object properties'>getundoc &#8211; get undocumented object properties</a> <small>getundoc is a very simple utility that displays the hidden (undocumented) properties of a specified handle object....</small></li><li><a
href='http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/' rel='bookmark' title='FindJObj &#8211; find a Matlab component&#8217;s underlying Java object'>FindJObj &#8211; find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li><li><a
href='http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/' rel='bookmark' title='Types of undocumented Matlab aspects'>Types of undocumented Matlab aspects</a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uiundo-matlab-undocumented-undo-redo-manager/' rel='bookmark' title='uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager'>uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager</a> <small>The built-in uiundo function provides easy yet undocumented access to Matlab's powerful undo/redo functionality. This article explains its usage....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Every now and then, I stumble on a Matlab feature that looks interesting and potentially useful and has existed for many previous Matlab releases, yet remains undocumented and unsupported. Today I present one such object, Matlab&#8217;s <i><b>graphics.cursorbar</b></i>.</p><p>The <i><b>graphics.cursorbar</b></i> object answers a very basic need that is often encountered in graph exploration: displaying data values together with horizontal/vertical cross-hairs, similarly to <i><b>ginput</b></i>.</p><p>While Matlab has provided the data-cursor mode for a long time, the cross-hair feature is still missing as of R2010b. Many CSSM newsgroup readers have asked about this missing feature. Some recent examples: <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/278992">here</a>, <a
target="_blank" rel="nofollow" href="http://www.mathworks.cn/matlabcentral/newsreader/view_thread/289787">here</a>, and <a
target="_blank" rel="nofollow" href="http://www.mathworks.cn/matlabcentral/newsreader/view_thread/288484">here</a>.</p><h3 id="DataMatrix">DataMatrix</h3><p>To answer this need I have created the <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/16181-datamatrix"><i><b>DataMatrix</b></i> utility</a>, which is available on the Matlab File Exchange:</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="DataMatrix: customizable data tooltip &#038; cross-hairs" src="http://UndocumentedMatlab.com/images/datamatrix.png" title="DataMatrix: customizable data tooltip &#038; cross-hairs" width="450" height="398" /><p
class="wp-caption-text">DataMatrix: customizable data tooltip &#038; cross-hairs</p></div></center></p><p><i><b>DataMatrix</b></i> displays matlab&#8217;s standard data-cursor tooltip together with dotted cross-hairs, both of which move with the mouse pointer. <i><b>DataMatrix</b></i> is actually based mostly on fully-documented stuff: the undocumented aspects are secondary to the main program flow and mostly just ensure old releases compatibility and correct behavior in the presence of some figure modes.</p><h3 id="Cursorbar">graphics.cursorbar</h3><p>When I created <i><b>DataMatrix</b></i> in 2007, I had no idea that <i><b>graphics.cursorbar</b></i> already existed. <i><b>graphics.cursorbar</b></i> is an internal Matlab object, that is undocumented and unsupported. It has several advantages over <i><b>DataMatrix</b></i>, being more customizable in the cross-hairs and data marker, although not enabling to customize the tooltip text nor to present both cross-hairs (only horizontal/vertical, not both). It is one of the earliest examples of Matlab&#8217;s old class system (<i><b>schema</b></i>-based).</p><p>We initialize a <i><b>graphics.cursorbar</b></i> object by supplying an axes (not very useful) or plot-line handle (more useful). The cursorbar handle can be customized using properties such as <b>BottomMarker, TopMarker, CursorLineColor, CursorLineStyle, CursorLineWidth, TargetMarkerSize, TargetMarkerStyle, ShowText, Orientation, Position</b> (Position is a hidden property) etc., as well as the regular HG properties (<b>UserData, Visibility, Parent</b> etc.).</p><p>Once the cursor-bar is created, it can be dragged and moved via the mouse cursor, as the following code snippet and animated gif show:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">t=<span style="color: #33f;">0</span><span style="color: #F0F;">:</span>.01<span style="color: #F0F;">:</span><span style="color: #33f;">7</span>; hp=<span style="color: #0000FF;">plot</span><span style="color: #080;">&#40;</span>t,<span style="color: #0000FF;">sin</span><span style="color: #080;">&#40;</span>t<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
hCursorbar = graphics.<span style="">cursorbar</span><span style="color: #080;">&#40;</span>hp<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">drawnow</span>
hCursorbar.<span style="">CursorLineColor</span> = <span style="color: #080;">&#91;</span>.9,.3,.6<span style="color: #080;">&#93;</span>; <span style="color: #228B22;">% default=[0,0,0]='k'</span>
hCursorbar.<span style="">CursorLineStyle</span> = <span style="color:#A020F0;">'-.'</span>;       <span style="color: #228B22;">% default='-'</span>
hCursorbar.<span style="">CursorLineWidth</span> = <span style="color: #33f;">2.5</span>;        <span style="color: #228B22;">% default=1</span>
hCursorbar.<span style="">Orientation</span> = <span style="color:#A020F0;">'vertical'</span>;     <span style="color: #228B22;">% =default</span>
hCursorbar.<span style="">TargetMarkerSize</span> = <span style="color: #33f;">12</span>;        <span style="color: #228B22;">% default=8</span>
hCursorbar.<span style="">TargetMarkerStyle</span> = <span style="color:#A020F0;">'o'</span>;      <span style="color: #228B22;">% default='s' (square)</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 310px"><img
alt="Matlab's internal cursorbar object" src="http://UndocumentedMatlab.com/images/colorbar_animated.gif" title="Matlab's internal cursorbar object" width="300" height="225" /><p
class="wp-caption-text">Matlab's internal cursorbar object</p></div></center></p><p>Comments within the internal code (%matlabroot%\toolbox\matlab\graphics\@graphics\@cursorbar\*.m) suggest that <i><b>graphics.cursorbar</b></i> has been around since 2003 at least, although I have not checked such old releases. The presented tooltip resembles one of the old data tips, not one of the modern ones. In addition, as far as I can tell, <i><b>graphics.cursorbar</b></i> is not used within the Matlab code corpus. For these reasons it would not surprise me at all to find that MathWorks will remove this feature in some near future release. Until then &#8211; have fun using it!</p><p>Can you find a good use for <i><b>graphics.cursorbar</b></i>? if so, please let us know by posting a comment <a
href="http://undocumentedmatlab.com/blog/undocumented-cursorbar-object/#respond">below</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/' rel='bookmark' title='getundoc &#8211; get undocumented object properties'>getundoc &#8211; get undocumented object properties</a> <small>getundoc is a very simple utility that displays the hidden (undocumented) properties of a specified handle object....</small></li><li><a
href='http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/' rel='bookmark' title='FindJObj &#8211; find a Matlab component&#8217;s underlying Java object'>FindJObj &#8211; find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li><li><a
href='http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/' rel='bookmark' title='Types of undocumented Matlab aspects'>Types of undocumented Matlab aspects</a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uiundo-matlab-undocumented-undo-redo-manager/' rel='bookmark' title='uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager'>uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager</a> <small>The built-in uiundo function provides easy yet undocumented access to Matlab's powerful undo/redo functionality. This article explains its usage....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/undocumented-cursorbar-object/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           category
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-02-09 06:51:12 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/category/presumed-future-risk/high-risk-of-breaking-in-future-versions/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      8.113s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1328795470; expires=Fri, 08-Feb-2013 13:51:10 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Thu, 09 Feb 2012 13:51:12 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Thu, 09 Feb 2012 14:51:12 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               d5096d99ce41d95bb72aebab146b150f
Content-Encoding:   gzip
-->
