<?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>Thu, 17 May 2012 12:01:26 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.1</generator> <item><title>Matlab&#8217;s internal memory representation</title><link>http://undocumentedmatlab.com/blog/matlabs-internal-memory-representation/</link> <comments>http://undocumentedmatlab.com/blog/matlabs-internal-memory-representation/#comments</comments> <pubDate>Thu, 15 Mar 2012 18:11:23 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Memory]]></category> <category><![CDATA[Mex]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Peter Li]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2798</guid> <description><![CDATA[Matlab's internal memory structure is explored and discussed.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/profiling-matlab-memory-usage/' rel='bookmark' title='Profiling Matlab memory usage'>Profiling Matlab memory usage</a> <small>mtic and mtoc were a couple of undocumented features that enabled users of past Matlab releases to easily profile memory usage. ...</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/matlab-mex-in-place-editing/' rel='bookmark' title='Matlab mex in-place editing'>Matlab mex in-place editing</a> <small>Editing Matlab arrays in-place can be an important technique for optimizing calculations. This article shows how to do it using Mex. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/udd-properties/' rel='bookmark' title='UDD Properties'>UDD Properties</a> <small>UDD provides a very convenient way to add customizable properties to existing Matlab object handles...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Once again I&#8217;d like to welcome guest blogger <a
target="_blank" rel="nofollow" href="http://absurdlycertain.blogspot.com/">Peter Li</a>. Peter wrote about <a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing/">Matlab Mex in-place editing</a> last month. Today, Peter pokes around in Matlab&#8217;s internal memory representation to the greater good and glory of Matlab Mex programming.</i></p><p><b><i>Disclaimer: The information in this article is provided for informational purposes only.  Be aware that poking into Matlab&#8217;s internals is not condoned or supported by MathWorks, and is not recommended for any regular usage.  Poking into memory has the potential to crash your computer so save your data!  Moreover, be advised (as the text below will show) that the information is highly prone to change without any advance notice in future Matlab releases, which could lead to very adverse effects on any program that relies on it. On the scale of undocumented Matlab topics, this practically breaks the scale, so be EXTREMELY careful when using this.</i></b></p><p>A few weeks ago I <a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing/">discussed</a> Matlab&#8217;s copy-on-write mechanism as part of my discussion of editing Matlab arrays in-place.  Today I want to explore some behind-the-scenes details of how the copy-on-write mechanism is implemented.  In the process we will learn a little about Matlab&#8217;s internal array representation.  I will also introduce some simple tools you can use to explore more of Matlab&#8217;s internals.  I will only cover basic information, so there are plenty more details left to be filled in by others who are interested.</p><h3 id="Copy-on-write">Brief review of copy-on-write and mxArray</h3><p>Copy-on-write is Matlab&#8217;s mechanism for avoiding unnecessary duplication of data in memory.  To implement this, Matlab needs to keep track internally of which sets of variables are copies of each other.  As described in <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/matlab_external/f21585.html">MathWorks&#8217;s article</a>, &#8220;<i>the Matlab language works with a single object type: the Matlab array. All Matlab variables (including scalars, vectors, matrices, strings, cell arrays, structures, and objects) are stored as Matlab arrays. In C/C++, the Matlab array is declared to be of type <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/apiref/mxarray.html"><code>mxArray</code></a></i>&#8220;. This means that <code>mxArray</code> defines how Matlab lays out all the information about an array (its Matlab data type, its size, its data, etc.) in memory.  So understanding Matlab&#8217;s internal array representation basically boils down to understanding <code>mxArray</code>.</p><p>Unfortunately, MathWorks also tells us that &#8220;<i><code>mxArray</code> is a C language <a
target='_blank' rel='nofollow' href='http://en.wikipedia.org/wiki/Opaque_pointer'>opaque type</a></i>&#8220;. This means that MathWorks does not expose the organization of <code>mxArray</code> to users (i.e. Matlab or Mex programmers).  Instead, MathWorks defines <code>mxArray</code> internally, and allows users to interact with it only through an API, a set of functions that know how to handle <code>mxArray</code> in their back end.  So, for example, a Mex programmer does not get the dimensions of an <code>mxArray</code> by directly accessing the relevant field in memory.  Instead, the Mex programmer only has a pointer to the <code>mxArray</code>, and passes this pointer into an API function that knows where in memory to find the requested information and then passes the result back to the programmer.</p><p>This is generally a good thing: the API provides an abstraction layer between the programmer and the memory structures so that if MathWorks needs to change the back end organization (to add a new feature for example), we programmers do not need to modify our code; instead MathWorks just updates the API to reflect the new internal organization.  On the other hand, being able to look into the internal structure of <code>mxArray</code> on occasion can help us understand how Matlab works, and can help us write more efficient code if we are careful as in the example of editing arrays in-place.</p><p>So how do we get a glimpse inside <code>mxArray</code>?  The first step is simply to find the region of memory where the <code>mxArray</code> lives: its beginning and end.  Finding where in memory the <code>mxArray</code> begins is pretty easy: it is given by its pointer value.  Here is a simple Mex function that takes a Matlab array as input and prints its memory address:</p><div
class="wp_syntax"><div
class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* printaddr.cpp */</span>
<span style="color: #339933;">#include &quot;mex.h&quot;</span>
<span style="color: #993333;">void</span> mexFunction<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> nlhs<span style="color: #339933;">,</span> mxArray <span style="color: #339933;">*</span>plhs<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> nrhs<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> mxArray <span style="color: #339933;">*</span>prhs<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>nrhs <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> mexErrMsgTxt<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;One input required.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%p<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> prhs<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>This function is nice as it prints the address in a standard hexadecimal format.  The same information can also be received directly in Matlab (i.e., without needing <i><b>printaddr</b></i>), using the undocumented <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/15485#34519"><i><b>format debug</b></i> command</a> (here&#8217;s <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/15988">another reference</a>):</p> </pre><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">format</span> debug
&nbsp;
&gt;&gt; A = <span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">10</span>
A =
Structure address = 7fc3b8869ae0
m = <span style="color: #33f;">1</span>
n = <span style="color: #33f;">10</span>
pr = 7fc44922c890
<span style="color: #0000FF;">pi</span> = <span style="color: #33f;">0</span>
     <span style="color: #33f;">1</span>     <span style="color: #33f;">2</span>     <span style="color: #33f;">3</span>     <span style="color: #33f;">4</span>     <span style="color: #33f;">5</span>     <span style="color: #33f;">6</span>     <span style="color: #33f;">7</span>     <span style="color: #33f;">8</span>     <span style="color: #33f;">9</span>    <span style="color: #33f;">10</span>
&nbsp;
&gt;&gt; printaddr<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
7fc3b8869ae0</pre></div></div><p>To play with this further from within Matlab however, it's nice to have the address returned to us as a 64-bit unsigned integer; here's a Mex function that does that:</p><div
class="wp_syntax"><div
class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* getaddr.cpp */</span>
<span style="color: #339933;">#include &quot;mex.h&quot;</span>
<span style="color: #993333;">void</span> mexFunction<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> nlhs<span style="color: #339933;">,</span> mxArray <span style="color: #339933;">*</span>plhs<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> nrhs<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> mxArray <span style="color: #339933;">*</span>prhs<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>nrhs <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> mexErrMsgTxt<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;One input required.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   plhs<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> mxCreateNumericMatrix<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> mxUINT64_CLASS<span style="color: #339933;">,</span> mxREAL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>out <span style="color: #339933;">=</span> static_cast<span style="color: #339933;">&lt;</span>unsigned <span style="color: #993333;">long</span> <span style="color: #339933;">*&gt;</span><span style="color: #009900;">&#40;</span>mxGetData<span style="color: #009900;">&#40;</span>plhs<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   out<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span> prhs<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>Here's <i><b>getaddr</b></i> in action:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; getaddr<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
           <span style="color: #33f;">139870853618400</span>
&nbsp;
<span style="color: #228B22;">% And using pure Matlab:</span>
&gt;&gt; <span style="color: #0000FF;">hex2dec</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'7f36388b5ae0'</span><span style="color: #080;">&#41;</span>  <span style="color: #228B22;">% output of printaddr or format debug</span>
<span style="color: #0000FF;">ans</span> =
           <span style="color: #33f;">139870853618400</span></pre></div></div><p>So now we know where to find our array in memory.  With this information we can already learn a lot.  To make our exploration a little cleaner though, it would be nice to know where the array ends in memory too, in other words we would like to know the size of the <code>mxArray</code>.</p><h3 id="Structure">Finding the structure of mxArray</h3><p>The first thing to understand is that the amount of memory taken by an <code>mxArray</code> does not have anything to do with the dimensions of the array in Matlab.  So a 1x1 Matlab array and a 100x100 Matlab array have the same size <code>mxArray</code> representation in memory.  As you will know if you have experience programming in Mex, this is simply because the Matlab array's data contents are not stored directly within <code>mxArray</code>.  Instead, <code>mxArray</code> only stores a pointer to another memory location where the actual data reside.  This is fine; the internal information we want to poke into is all still in <code>mxArray</code>, and it is easy to get the pointer to the array's data contents using the API functions <i>mxGetData</i> or <i>mxGetPr</i>.</p><p>So we are still left with trying to figure out the size of <code>mxArray</code>.  There are a couple paths forward.  First I want to talk about a historical tool that used to make a lot of this internal information easily available.  This was a function called <i>headerdump</i>, by Peter Boetcher (described <a
target="_blank" rel="nofollow" href="http://www.mit.edu/~pwb/matlab/">here</a> and <a
target="_blank" rel="nofollow" href="http://groups.google.com/group/comp.soft-sys.matlab/browse_thread/thread/c241d8821fb90275">here</a>). <i>headerdump</i> was created for exactly the goal we are currently working towards: to understand Matlab's copy-on-write mechanism.  Unfortunately, as Matlab has evolved, newer versions have incrementally broken this useful tool.  So our goal here is to create a replacement.  Still, we can learn a lot from the earlier work.</p><p>One of the things that helped people figure out Matlab's internals in the past is that in older versions of Matlab <code>mxArray</code> is not a completely opaque type.  Even in recent versions up through at least R2010a, if you look into $MATLAB/extern/include/matrix.h you can find a definition of <code>mxArray_tag</code> that looks something like this:</p><div
class="wp_syntax"><div
class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* R2010a */</span>
<span style="color: #993333;">struct</span> mxArray_tag <span style="color: #009900;">&#123;</span>
   <span style="color: #993333;">void</span>  <span style="color: #339933;">*</span>reserved<span style="color: #339933;">;</span>
   <span style="color: #993333;">int</span>    reserved1<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
   <span style="color: #993333;">void</span>  <span style="color: #339933;">*</span>reserved2<span style="color: #339933;">;</span>
   size_t  number_of_dims<span style="color: #339933;">;</span>
   <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> reserved3<span style="color: #339933;">;</span>
   <span style="color: #993333;">struct</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>  flag0  <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>  flag1  <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>  flag2  <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>  flag3  <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>  flag4  <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>  flag5  <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>  flag6  <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>  flag7  <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>  flag7a <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>  flag8  <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>  flag9  <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>  flag10 <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>  flag11 <span style="color: #339933;">:</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span>
       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>  flag12 <span style="color: #339933;">:</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span>
       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>  flag13 <span style="color: #339933;">:</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>   flags<span style="color: #339933;">;</span>
   size_t reserved4<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
   <span style="color: #993333;">union</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #993333;">struct</span> <span style="color: #009900;">&#123;</span>
           <span style="color: #993333;">void</span>  <span style="color: #339933;">*</span>pdata<span style="color: #339933;">;</span>
           <span style="color: #993333;">void</span>  <span style="color: #339933;">*</span>pimag_data<span style="color: #339933;">;</span>
           <span style="color: #993333;">void</span>  <span style="color: #339933;">*</span>reserved5<span style="color: #339933;">;</span>
           size_t reserved6<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>   number_array<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>   data<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div><p>This is what you could call murky or obfuscated, but not completely opaque.  The fields mostly have unhelpful names like "reserved", but on the other hand we at least have a sense for what fields there are and their layout.</p><p>A more informative (yet unofficial) definition was <a
target="_blank" rel="nofollow" href="http://groups.google.com/group/comp.soft-sys.matlab/browse_thread/thread/b8dbd91953c494fb">provided</a> by James Tursa and Peter Boetcher:</p><div
class="wp_syntax"><div
class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;mex.h&quot;</span>
<span style="color: #808080; font-style: italic;">/* Definition of structure mxArray_tag for debugging purposes. Might not be fully correct 
 * for Matlab 2006b or 2007a, but the important things are. Thanks to Peter Boettcher.
 */</span>
<span style="color: #993333;">struct</span> mxArray_tag <span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>name<span style="color: #339933;">;</span>
  mxClassID class_id<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> vartype<span style="color: #339933;">;</span>
  mxArray    <span style="color: #339933;">*</span>crosslink<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span>      number_of_dims<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span>      refcount<span style="color: #339933;">;</span>
  <span style="color: #993333;">struct</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>    scalar_flag <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>    flag1 <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>    flag2 <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>    flag3 <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>    flag4 <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>    flag5 <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>    flag6 <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>    flag7 <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>    private_data_flag <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>    flag8 <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>    flag9 <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>    flag10 <span style="color: #339933;">:</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>    flag11 <span style="color: #339933;">:</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>    flag12 <span style="color: #339933;">:</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span>    flag13 <span style="color: #339933;">:</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>   flags<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span>  rowdim<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span>  coldim<span style="color: #339933;">;</span>
  <span style="color: #993333;">union</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">struct</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #993333;">double</span>  <span style="color: #339933;">*</span>pdata<span style="color: #339933;">;</span>       <span style="color: #666666; font-style: italic;">// original: void*</span>
      <span style="color: #993333;">double</span>  <span style="color: #339933;">*</span>pimag_data<span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// original: void*</span>
      <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>irptr<span style="color: #339933;">;</span>
      <span style="color: #993333;">void</span>  <span style="color: #339933;">*</span>jcptr<span style="color: #339933;">;</span>
      <span style="color: #993333;">int</span>   nelements<span style="color: #339933;">;</span>
      <span style="color: #993333;">int</span>   nfields<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>   number_array<span style="color: #339933;">;</span>
    <span style="color: #993333;">struct</span> <span style="color: #009900;">&#123;</span>
      mxArray <span style="color: #339933;">**</span>pdata<span style="color: #339933;">;</span>
      <span style="color: #993333;">char</span>  <span style="color: #339933;">*</span>field_names<span style="color: #339933;">;</span>
      <span style="color: #993333;">void</span>  <span style="color: #339933;">*</span>dummy1<span style="color: #339933;">;</span>
      <span style="color: #993333;">void</span>  <span style="color: #339933;">*</span>dummy2<span style="color: #339933;">;</span>
      <span style="color: #993333;">int</span>   dummy3<span style="color: #339933;">;</span>
      <span style="color: #993333;">int</span>   nfields<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>   struct_array<span style="color: #339933;">;</span>
    <span style="color: #993333;">struct</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>pdata<span style="color: #339933;">;</span>  <span style="color: #808080; font-style: italic;">/*mxGetInfo*/</span>
      <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>field_names<span style="color: #339933;">;</span>
      <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>name<span style="color: #339933;">;</span>
      <span style="color: #993333;">int</span> checksum<span style="color: #339933;">;</span>
      <span style="color: #993333;">int</span>  nelements<span style="color: #339933;">;</span>
      <span style="color: #993333;">int</span>  reserved<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>  object_array<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>   data<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div><p>For comparison, here is another definition from an earlier version of Matlab.</p><div
class="wp_syntax"><div
class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* R11 aka Matlab 5.0 (1999) */</span>
<span style="color: #993333;">struct</span> mxArray_tag <span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">char</span> name<span style="color: #009900;">&#91;</span>mxMAXNAM<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span>  class_id<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span>  vartype<span style="color: #339933;">;</span>
  mxArray <span style="color: #339933;">*</span>crosslink<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span>  number_of_dims<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span>  nelements_allocated<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span>  dataflags<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span>  rowdim<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span>  coldim<span style="color: #339933;">;</span>
  <span style="color: #993333;">union</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">struct</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>pdata<span style="color: #339933;">;</span>
      <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>pimag_data<span style="color: #339933;">;</span>
      <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>irptr<span style="color: #339933;">;</span>
      <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>jcptr<span style="color: #339933;">;</span>
      <span style="color: #993333;">int</span>   reserved<span style="color: #339933;">;</span>
      <span style="color: #993333;">int</span>   nfields<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>   number_array<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>   data<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div><p>I took this R11 definition from the source code to <i>headerdump</i> (specifically, from <i>mxinternals.h</i>, which also has <code>mxArray_tag</code> definitions for R12 (Matlab 6.0) and R13 (Matlab 6.5)), and you can see that it is much more informative, because many fields have been given useful names thanks to the work of Peter Boetcher and others.  Note also that the definition from this old version of Matlab is quite different from the version from R2010a.</p><p>At this point, if you are running a much earlier version of Matlab like R11 or R13, you can break off from the current article and start playing around with <i>headerdump</i> directly to try to understand Matlab's internals.  For more recent versions of Matlab, we have more work to do.  Getting back to our original goal, if we take the <code>mxArray_tag</code> definition from R2010a and run <i>sizeof</i>, we get an answer for the amount of memory taken up by an <code>mxArray</code> in R2010a: <b>104 bytes</b>.</p><h3 id="Size">Determining the size of mxArray</h3><p>It was nice to derive the size of <code>mxArray</code> from actual MathWorks code, but unfortunately this information is no longer available as of R2011a.  Somewhere between R2010a and R2011a, MathWorks stepped up their efforts to make <code>mxArray</code> completely opaque.  So we should find another way to get the size of <code>mxArray</code> for current and future Matlab versions.</p><p>One ugly trick that works is to create many new arrays quickly and see where their starting points end up in memory:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; A = <span style="color: #0000FF;">num2cell</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">100</span><span style="color: #080;">&#41;</span>';
&gt;&gt; addrs = <span style="color: #0000FF;">sort</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">cellfun</span><span style="color: #080;">&#40;</span>@getaddr, A<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>What we did here is create 100 new arrays, and then get all their memory addresses in sorted order.  Now we can take a look at how far apart these new arrays ended up in memory:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">semilogy</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">diff</span><span style="color: #080;">&#40;</span>addrs<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>The resulting plot will look different each time you run this; it is not really predictable where Matlab will put new arrays into memory.  Here is an example from my system:</p><p><center><div
class="wp-caption aligncenter" style="width: 493px"><img
alt="Plot of memory addresses" src="http://UndocumentedMatlab.com/images/mxArray_memory.png" title="Plot of memory addresses" width="483" height="381" /><p
class="wp-caption-text">Plot of memory addresses</p></div></center></p><p>Your results may look different, and you might have to increase the number of new arrays from 100 to 1000 to get the qualitative result, but the important feature of this plot is that there is a minimum distance between new arrays of about 10<sup>2</sup>.  In fact, if we just go straight for this minimum distance:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">min</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">diff</span><span style="color: #080;">&#40;</span>addrs<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
            <span style="color: #33f;">104</span></pre></div></div><p>we find that although <code>mxArray</code> has gone completely opaque from R2010a to R2011a, the full size of <code>mxArray</code> in memory has stayed the same: 104 bytes.</p><h3 id="Dump">Dumping mxArray from memory</h3><p>We now have all the information we need to start looking into Matlab's array representation.  There are many tools available that allow you to browse memory locations or dump memory contents to disk.  For our purposes though, it is nice to be able to do everything from within Matlab.  Therefore I introduce a simple tool that prints memory locations into the Matlab console:</p><div
class="wp_syntax"><div
class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* printmem.cpp */</span>
<span style="color: #339933;">#include &quot;mex.h&quot;</span>
<span style="color: #993333;">void</span> mexFunction<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> nlhs<span style="color: #339933;">,</span> mxArray <span style="color: #339933;">*</span>plhs<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> nrhs<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> mxArray <span style="color: #339933;">*</span>prhs<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>nrhs <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>mxIsUint64<span style="color: #009900;">&#40;</span>prhs<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> mxIsEmpty<span style="color: #009900;">&#40;</span>prhs<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    mexErrMsgTxt<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;First argument must be a uint64 memory address&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>addr <span style="color: #339933;">=</span> static_cast<span style="color: #339933;">&lt;</span>unsigned <span style="color: #993333;">long</span> <span style="color: #339933;">*&gt;</span><span style="color: #009900;">&#40;</span>mxGetData<span style="color: #009900;">&#40;</span>prhs<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>mem <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> addr<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>nrhs <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">2</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>mxIsDouble<span style="color: #009900;">&#40;</span>prhs<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> mxIsEmpty<span style="color: #009900;">&#40;</span>prhs<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    mexErrMsgTxt<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Second argument must be a double-type integer byte size.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>      
  <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> nbytes <span style="color: #339933;">=</span> static_cast<span style="color: #339933;">&lt;</span>unsigned int<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>mxGetScalar<span style="color: #009900;">&#40;</span>prhs<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> nbytes<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%.2x &quot;</span><span style="color: #339933;">,</span> mem<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #0000dd;">16</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>Here is how you use it in Matlab:</p> </pre><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; A = <span style="color: #33f;">0</span>;
&gt;&gt; printmem<span style="color: #080;">&#40;</span>getaddr<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>, <span style="color: #33f;">104</span><span style="color: #080;">&#41;</span>
00 00 00 00 00 00 00 00 06 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00
00 00 00 00 01 02 00 00 01 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 <span style="color: #33f;">70</span> fa <span style="color: #33f;">33</span> df 6f 7f 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00</pre></div></div><p>And there you have it: the inner guts of <code>mxArray</code> laid bare.  I have printed each byte as a two character hexadecimal value, as is standard, so there are 16 bytes printed per row.</p><h3 id="">What does it mean?</h3><p>So now we have 104 bytes of Matlab internals to dig into.  We can start playing with this with a few simple examples:</p><pre>
>> A = 0; B = 1;
>> printmem(getaddr(A), 104)
00 00 00 00 00 00 00 00 06 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00
00 00 00 00 01 02 00 00 01 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 <span style="background-color:#e6b8af;">c0 b0 27 df 6f 7f</span> 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

>> printmem(getaddr(B), 104)
00 00 00 00 00 00 00 00 06 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00
00 00 00 00 01 02 00 00 01 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 <span style="background-color:#e6b8af;">70 fa 33 df 6f 7f</span> 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
</pre><p></p><p>In this and subsequent examples, I will highlight bytes that are different or that are of interest.  What we can see from this example is that although arrays A and B have different content, almost nothing is different between their <code>mxArray</code> representations.  What is different, is the memory address stored in the highlighted bytes.  This confirms our earlier assertion that <code>mxArray</code> does not store the array contents, but only a pointer to the content location.</p><p>Now let us try to figure out some of the other fields:</p><pre>
>> A = 1:3; B = 1:10; C = (1:10)';
>> printmem(getaddr(A), 64)
00 00 00 00 00 00 00 00 06 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 <span style="background-color:#a4c2f4;">02</span> 00 00 00 00 00 00 00
00 00 00 00 00 <span style="background-color:#a4c2f4;">02</span> 00 00 <span style="background-color:#e6b8af;">01</span> 00 00 00 00 00 00 00
<span style="background-color:#e6b8af;">03</span> 00 00 00 00 00 00 00 60 80 22 df 6f 7f 00 00

>> printmem(getaddr(B), 64)
00 00 00 00 00 00 00 00 06 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 <span style="background-color:#a4c2f4;">02</span> 00 00 00 00 00 00 00
00 00 00 00 00 <span style="background-color:#a4c2f4;">02</span> 00 00 <span style="background-color:#e6b8af;">01</span> 00 00 00 00 00 00 00
<span style="background-color:#e6b8af;">0a</span> 00 00 00 00 00 00 00 80 83 29 df 6f 7f 00 00

>> printmem(getaddr(C), 64)
00 00 00 00 00 00 00 00 06 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 <span style="background-color:#a4c2f4;">02</span> 00 00 00 00 00 00 00
00 00 00 00 00 <span style="background-color:#a4c2f4;">02</span> 00 00 <span style="background-color:#e6b8af;">0a</span> 00 00 00 00 00 00 00
<span style="background-color:#e6b8af;">01</span> 00 00 00 00 00 00 00 80 83 29 df 6f 7f 00 00
</pre><p></p><p>(Note that this time I only printed the first four lines of each array as this is where the interesting differences are for this example.)</p><p>In <span
style="background-color:#e6b8af;">red</span> I highlighted the bytes in each array that give its number of rows and columns (note that hexadecimal 0a is 10 in decimal).  In <span
style="background-color:#a4c2f4;">blue</span> I highlighted areas that store the value "02", which could be the location for storing the number of dimensions.  Let us look into this more:</p><pre>
>> A = rand([3 3 3]);
>> printmem(getaddr(A), 64)
00 00 00 00 00 00 00 00 06 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 <span style="background-color:#e6b8af;">03</span> 00 00 00 00 00 00 00
00 00 00 00 00 02 00 00 <span style="background-color:#e6b8af;">30 4a 3f df 6f 7f</span> 00 00
<span style="background-color:#e6b8af;">09</span> 00 00 00 00 00 00 00 b0 d3 24 df 6f 7f 00 00
</pre><p></p><p>Two interesting results here:  The first highlighted region changed from 02 to 03, so this must be the place where <code>mxArray</code> indicates a 3-dimensional array rather than 2D.  Another important thing also changed though: we can see in the second highlighted region that there is a new memory address stored where we used to find the number of rows.  And in the third highlighted region we now have the number 09 instead of the number of columns.</p><p>Clearly, Matlab has a different way of representing a 2D matrix versus arrays of higher dimension such as 3D.  In the 2D case, <code>mxArray</code> simply holds the nrows and ncols directly, but for a higher dimension case we hold only the number of dimensions (03), the total number of elements (09), and a pointer to another memory location (0x7f6fdf3f4a30) which holds the array of sizes for each dimension.</p><h3 id="COW">The copy-on-write mechanism</h3><p>Finally, we are in a position to understand how Matlab internally implements copy-on-write:</p><pre>
>> A = 1:10;
>> printmem(getaddr(A), 64);
00 00 00 00 00 00 00 00 06 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00
00 00 00 00 00 02 00 00 01 00 00 00 00 00 00 00
0a 00 00 00 00 00 00 00 90 f3 24 df 6f 7f 00 00

>> B = A;
>> printaddr(B);
0x7f6f4c7b6810

>> printmem(getaddr(A), 64);
<span style="background-color:#e6b8af;">10 68 7b 4c 6f 7f</span> 00 00 06 00 00 00 00 00 00 00
<span style="background-color:#e6b8af;">10 68 7b 4c 6f 7f</span> 00 00 02 00 00 00 00 00 00 00
00 00 00 00 00 02 00 00 01 00 00 00 00 00 00 00
0a 00 00 00 00 00 00 00 <span style="background-color:#a4c2f4;">90 f3 24 df 6f 7f</span> 00 00
</pre><p></p><p>What we see is that by setting B = A, we change the internal representation of A itself.  Two new memory address pointers are added to the <code>mxArray</code> for A.  As it turns out, both of these point to the address for array B, which makes sense; this is how Matlab internally keeps track of arrays that are copies of each other.  Note that because byte order is <a
target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Endianness">little-endian</a>, the memory addresses from <i><b>printmem</b></i> are byte-wise, i.e. every two characters, reversed relative to the address from <i><b>printaddr</b></i>.</p><p>We can also look into array B:</p><pre>
>> printmem(getaddr(B), 64);
<span style="background-color:#e6b8af;">f0 41 7a 4c 6f 7f</span> 00 00 06 00 00 00 00 00 00 00
<span style="background-color:#e6b8af;">f0 41 7a 4c 6f 7f</span> 00 00 02 00 00 00 00 00 00 00
00 00 00 00 00 02 00 00 01 00 00 00 00 00 00 00
0a 00 00 00 00 00 00 00 <span style="background-color:#a4c2f4;">90 f3 24 df 6f 7f</span> 00 00

>> printaddr(A);
<span style="background-color:#e6b8af;">0x7f6f4c7a41f0</span>
</pre><p></p><p>As I have highlighted, there are two interesting points here.  First the red highlights show that array B has pointers back to array A.  Second the blue highlight shows that the Matlab data for array B actually just points back to the same memory as the data for array A (the values 1:10).</p><p>Finally, we would like to understand why there are two pointers added.  Let us see what happens if we add a third linked variable:</p><pre>
>> C = B;
>> printaddr(A); printaddr(B); printaddr(C);
<span style="background-color:#e6b8af;">0x7f6f4c7a41f0</span>
<span style="background-color:#a4c2f4;">0x7f6f4c7b6810</span>
<span style="background-color:#00ff00;">0x7f6f4c7b69b0</span>

>> printmem(getaddr(A), 32)
<span style="background-color:#00ff00;">b0 69 7b 4c 6f 7f</span> 00 00 06 00 00 00 00 00 00 00
<span style="background-color:#a4c2f4;">10 68 7b 4c 6f 7f</span> 00 00 02 00 00 00 00 00 00 00

>> printmem(getaddr(B), 32)
<span style="background-color:#e6b8af;">f0 41 7a 4c 6f 7f</span> 00 00 06 00 00 00 00 00 00 00
<span style="background-color:#00ff00;">b0 69 7b 4c 6f 7f</span> 00 00 02 00 00 00 00 00 00 00

>> printmem(getaddr(C), 32)
<span style="background-color:#a4c2f4;">10 68 7b 4c 6f 7f</span> 00 00 06 00 00 00 00 00 00 00
<span style="background-color:#e6b8af;">f0 41 7a 4c 6f 7f</span> 00 00 02 00 00 00 00 00 00 00
</pre><p></p><p>So it turns out that Matlab keeps track of a set of linked variables with a kind of cyclical, doubly-linked list structure; array A is linked to B in the forward direction and is also linked to C in the reverse direction by looping back around, etc.  The cyclical nature of this makes sense, as we need to be able to start from any of A, B, or C and find all the linked arrays.  But it is still not entirely clear why the list needs to be cyclical AND linked in both directions.  In fact, in earlier versions of Matlab this cyclical list was only singly-linked.</p><h3 id="Conclusions">Conclusions</h3><p>Obviously there is a lot more to <code>mxArray</code> and Matlab internals than what we have delved into here.  Still, with this basic introduction I hope to have whet your appetite for understanding more about Matlab internals, and provided some simple tools to help you explore.  I want to reiterate that in general MathWorks's approach of an opaque <code>mxArray</code> type with access abstracted through an API layer is a good policy.  The last thing you would want to do is take the information here and write a bunch of code that relies on the structure of <code>mxArray</code> to work; next time MathWorks needs to add a new feature and change <code>mxArray</code>, all your code will break.  So in general we are all better off playing within the API that MathWorks provides.  And remember: poking into memory can crash your computer, so save your data!</p><p>On the other hand, occasionally there are cases, like in-place editing, where it is useful to push the capabilities of Matlab a little beyond what MathWorks envisioned.  In these cases, having an understanding of Matlab's internals can be critical, for example in understanding how to avoid conflicting with copy-on-write.  Therefore I hope the information presented here will prove useful.  Ideally, someone will be motivated to take this starting point and repair some of the tools like <i>headerdump</i> that made Matlab's internal workings more transparent in the past.  I believe that having more of this information out in the Matlab community is good for the community as a whole.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/profiling-matlab-memory-usage/' rel='bookmark' title='Profiling Matlab memory usage'>Profiling Matlab memory usage</a> <small>mtic and mtoc were a couple of undocumented features that enabled users of past Matlab releases to easily profile memory usage. ...</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/matlab-mex-in-place-editing/' rel='bookmark' title='Matlab mex in-place editing'>Matlab mex in-place editing</a> <small>Editing Matlab arrays in-place can be an important technique for optimizing calculations. This article shows how to do it using Mex. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/udd-properties/' rel='bookmark' title='UDD Properties'>UDD Properties</a> <small>UDD provides a very convenient way to add customizable properties to existing Matlab object handles...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/matlabs-internal-memory-representation/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Profiling Matlab memory usage</title><link>http://undocumentedmatlab.com/blog/profiling-matlab-memory-usage/</link> <comments>http://undocumentedmatlab.com/blog/profiling-matlab-memory-usage/#comments</comments> <pubDate>Thu, 01 Mar 2012 00:13:04 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Memory]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2768</guid> <description><![CDATA[mtic and mtoc were a couple of undocumented features that enabled users of past Matlab releases to easily profile memory usage.<pre> </pre>Related posts:<ol><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/matlabs-internal-memory-representation/' rel='bookmark' title='Matlab&#8217;s internal memory representation'>Matlab&#8217;s internal memory representation</a> <small>Matlab's internal memory structure is explored and discussed. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing/' rel='bookmark' title='Matlab mex in-place editing'>Matlab mex in-place editing</a> <small>Editing Matlab arrays in-place can be an important technique for optimizing calculations. This article shows how to do it using Mex. ...</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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Anyone who has had experience with real-life applications knows that Memory usage can have a significant impact on the application&#8217;s usability, in aspects such as performance, interactivity, and even (on some lousy memory-management Operating Systems) crashes/hangs.</p><p>In Matlab releases of the past few years, this has been addressed by expanding the information reported by the built-in <i><b>memory</b></i> function. In addition, an undocumented feature was added to the Matlab Profiler that <a
target="_blank" href="http://undocumentedmatlab.com/blog/undocumented-profiler-options/">enables monitoring</a> memory usage.</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><br
/> <img
title="Profile report with memory &amp; JIT info" src="http://undocumentedmatlab.com/images/profile2d_450.png" alt="Profile report with memory &amp; JIT info" width="450" /><img
title="Profile report with memory &amp; JIT info" src="http://undocumentedmatlab.com/images/profile2c_450.png" alt="Profile report with memory &amp; JIT info" width="450" /></p><p><img
title="Profile report with memory &amp; JIT info" src="http://undocumentedmatlab.com/images/profile2.png" alt="Profile report with memory &amp; JIT info" width="416" /><br
/><p
class="wp-caption-text">Profile report with memory &amp; JIT info</p></div></center></p><p>In Matlab release R2008a (but not on newer releases) we could also use a nifty parameter of the undocumented <a
target="_blank" href="http://undocumentedmatlab.com/blog/undocumented-feature-function/"><i><b>feature</b></i> function</a>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; feature mtic; a=<span style="color: #0000FF;">ones</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100</span><span style="color: #080;">&#41;</span>; feature mtoc
<span style="color: #0000FF;">ans</span> = 
      TotalAllocated<span style="color: #F0F;">:</span> <span style="color: #33f;">84216</span>
          TotalFreed<span style="color: #F0F;">:</span> <span style="color: #33f;">2584</span>
    LargestAllocated<span style="color: #F0F;">:</span> <span style="color: #33f;">80000</span>
           NumAllocs<span style="color: #F0F;">:</span> <span style="color: #33f;">56</span>
            NumFrees<span style="color: #F0F;">:</span> <span style="color: #33f;">43</span>
                Peak<span style="color: #F0F;">:</span> <span style="color: #33f;">81640</span></pre></div></div><p>As can easily be seen in this example, allocating 100<sup>2</sup> doubles requires 80000 bytes of allocation, plus some 4KB others that were allocated (and 2KB freed) within the function <i><b>ones</b></i>. Running the same code line again gives a very similar result, but now there are 80000 more bytes freed when the matrix <code>a</code> is overwritten:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; feature mtic; a=<span style="color: #0000FF;">ones</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100</span><span style="color: #080;">&#41;</span>; feature mtoc
<span style="color: #0000FF;">ans</span> = 
      TotalAllocated<span style="color: #F0F;">:</span> <span style="color: #33f;">84120</span>
          TotalFreed<span style="color: #F0F;">:</span> <span style="color: #33f;">82760</span>
    LargestAllocated<span style="color: #F0F;">:</span> <span style="color: #33f;">80000</span>
           NumAllocs<span style="color: #F0F;">:</span> <span style="color: #33f;">54</span>
            NumFrees<span style="color: #F0F;">:</span> <span style="color: #33f;">49</span>
                Peak<span style="color: #F0F;">:</span> <span style="color: #33f;">81328</span></pre></div></div><p>This is pretty informative and very handy for debugging memory bottlenecks. Unfortunately, starting in R2008b, features mtic and mtoc are no longer supported <i>&#8220;under the current <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/tech-notes/1100/1106.html">memory manager</a>&#8220;</i>. Sometime around 2010 the mtic and mtoc features were completely removed. Users of R2008b and newer releases therefore need to use the internal structs returned by the <i><b>memory</b></i> function, and/or use the profiler&#8217;s memory-monitoring feature. If you ask me, using mtic/mtoc was much simpler and easier. I for one miss these features.</p><p>In a related matter, if we wish to monitor Java&#8217;s memory used within Matlab, we are in a bind, because there are no built-in tools to help us. there are several JVM switches that can be turned on in the <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-18I2C/"><i>java.opts</i></a> file: -Xrunhprof[:help]|[:option=value,...], -Xprof, -Xrunprof, -XX:+PrintClassHistogram <a
target="_blank" rel="nofollow" href="http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html">and so on</a>. There are several memory-monitoring (so-called &#8220;heap-walking&#8221;) tools: the standard JDK jconsole, jmap, jhat and jvisualvm (with its useful plugins) provide good basic coverage. MathWorks has <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-3L4JU7/">posted</a> a tutorial on using jconsole with Matlab. There are a number of other third-party tools such as <a
target="_blank" rel="nofollow" href="http://www.khelekore.org/jmp/">JMP</a> (for JVMs 1.5 and earlier) or <a
target="_blank" rel="nofollow" href="http://www.khelekore.org/jmp/tijmp/">TIJMP</a> (for JVM 1.6). Within Matlab, we can use utilities such as <a
target="_blank" rel="nofollow" href="http://www.javamex.com/classmexer/">Classmexer</a> to estimate a particular object&#8217;s size (both shallow and deep referencing), or use <code>java.lang.Runtime.getRuntime()</code>&#8216;s methods (<i>maxMemory(), freeMemory()</i> and <i>totalMemory()</i>) to monitor overall Java memory (<a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/296813#797410">sample usage</a>).</p><p>Specifically in R2011b (but in no other release), we can also use a built-in Java memory monitor. Unfortunately, this simple and yet useful memory monitor was removed in R2012a (or maybe it was just moved to another package and I haven&#8217;t found out where&#8230; <i>yet</i>&#8230;):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">com.<span style="">mathworks</span>.<span style="">xwidgets</span>.<span style="">JavaMemoryMonitor</span>.<span style="">invoke</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 230px"><img
alt="Matlab R2011b's Java memory monitor" src="http://UndocumentedMatlab.com/images/Java_Memory_Monitor.png" title="Matlab R2011b's Java memory monitor" width="159" /><p
class="wp-caption-text">Matlab R2011b's Java memory monitor</p></div></center></p><p>As I have already noted quite often, using undocumented Matlab features and functions carries the risk that they will not be supported in some future Matlab release. Today&#8217;s article is a case in point.</p><p><pre> </pre>Related posts:<ol><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/matlabs-internal-memory-representation/' rel='bookmark' title='Matlab&#8217;s internal memory representation'>Matlab&#8217;s internal memory representation</a> <small>Matlab's internal memory structure is explored and discussed. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing/' rel='bookmark' title='Matlab mex in-place editing'>Matlab mex in-place editing</a> <small>Editing Matlab arrays in-place can be an important technique for optimizing calculations. This article shows how to do it using Mex. ...</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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/profiling-matlab-memory-usage/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <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[Guest bloggers]]></category> <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> <category><![CDATA[Peter Li]]></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/matlabs-internal-memory-representation/' rel='bookmark' title='Matlab&#8217;s internal memory representation'>Matlab&#8217;s internal memory representation</a> <small>Matlab's internal memory structure is explored and discussed. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/profiling-matlab-memory-usage/' rel='bookmark' title='Profiling Matlab memory usage'>Profiling Matlab memory usage</a> <small>mtic and mtoc were a couple of undocumented features that enabled users of past Matlab releases to easily profile memory usage. ...</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></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/matlabs-internal-memory-representation/' rel='bookmark' title='Matlab&#8217;s internal memory representation'>Matlab&#8217;s internal memory representation</a> <small>Matlab's internal memory structure is explored and discussed. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/profiling-matlab-memory-usage/' rel='bookmark' title='Profiling Matlab memory usage'>Profiling Matlab memory usage</a> <small>mtic and mtoc were a couple of undocumented features that enabled users of past Matlab releases to easily profile memory usage. ...</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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> <item><title>Types of undocumented Matlab aspects</title><link>http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/</link> <comments>http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/#comments</comments> <pubDate>Thu, 24 Nov 2011 18:00:36 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented feature]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JMI]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[Undocumented property]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2534</guid> <description><![CDATA[This article lists the different types of undocumented/unsupported/hidden aspects in Matlab<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/reasons-for-undocumented-matlab-aspects/' rel='bookmark' title='Reasons for undocumented Matlab aspects'>Reasons for undocumented Matlab aspects</a> <small>There are many reasons for the numerous undocumented aspects in Matlab - this article explains them....</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-cursorbar-object/' rel='bookmark' title='Undocumented cursorbar object'>Undocumented cursorbar object</a> <small>Matlab's internal undocumented graphics.cursorbar object can be used to present dynamic data-tip cross-hairs...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uiundo-matlab-undocumented-undo-redo-manager/' rel='bookmark' title='uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager'>uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager</a> <small>The built-in uiundo function provides easy yet undocumented access to Matlab's powerful undo/redo functionality. This article explains its usage....</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-xml-functionality/' rel='bookmark' title='Undocumented XML functionality'>Undocumented XML functionality</a> <small>Matlab's built-in XML-processing functions have several undocumented features that can be used by Java-savvy users...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Why are there so many undocumented aspects in Matlab?</p><p>This is a great question, recently <a
target="_blank" href="http://undocumentedmatlab.com/blog/guide-customization/#comment-61578">asked</a> by a reader of this blog, so I wanted to expand on it in next week&#8217;s article. Before specifying the different reasons, let&#8217;s map the nature of undocumented aspects that we find in Matlab.</p><p>The term <i>undocumented/unsupported</i> (as opposed to <i>mis-documentated</i> or <i>deprecated</i>) actually refers to quite a large number of different types.<br
/> In the following list, the hyperlinks on the list-item titles lead to a list of corresponding articles on this website:</p><ul><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/undocumented-function/">Undocumented functions</a></b><br
/> Matlab functions which appears nowhere in the documentation, are usually built-in functions (do not have an m-file) and can only be inferred from online CSSM posts or usage within one of the Matlab m-functions installed with Matlab (the latter being the usual case). None of these functions is officially supported by MathWorks. <a
target="_blank" href="http://undocumentedmatlab.com/blog/category/mex/">MEX</a> is an important source for such functions.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/semi-documented-function/">Semi-documented functions</a></b><br
/> Matlab functionality which exists in Matlab m-functions installed with Matlab, but have their main comment separated from the H1 comment line, thereby hiding it from normal view (via Matlab&#8217;s <i><b>help</b></i> function). The H1 comment line itself is simply a standard warning that this function is not officially supported and may change in some future version. To see the actual help comment, simply edit the function (using Matlab&#8217;s <i><b>edit</b></i> function or any text editor) and place a comment sign (%) at the empty line between the H1 comment and the actual help section. The entire help section will then onward be visible via the <i><b>help</b></i> function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">        <span style="color: #0000FF;">function</span> <span style="color: #080;">&#91;</span>tree, container<span style="color: #080;">&#93;</span> = uitree<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
        <span style="color: #228B22;">% WARNING: This feature is not supported in MATLAB</span>
        <span style="color: #228B22;">% and the API and functionality may change in a future release.</span>
<span style="color: #0000FF;">fix</span> =&gt;  <span style="color: #228B22;">%</span>
        <span style="color: #228B22;">% UITREE creates a uitree component with hierarchical data in a figure window.</span>
        <span style="color: #228B22;">%   UITREE creates an empty uitree object with default property values in</span>
        <span style="color: #228B22;">%   a figure window.</span>
        <span style="color: #228B22;">%...</span></pre></div></div><p>These functions are not documented in the full documentation (via Matlab&#8217;s <i><b>doc</b></i> function, or online). The odd thing is that some of these functions may appear in the category help output (for example, <i><b>help</b>(&#8216;uitools&#8217;)</i>), and in some cases may even have a fully-visible help section (e.g., <i><b>help</b>(&#8216;setptr&#8217;)</i>), but do not have any online help documentation (<i><b>docsearch</b>(&#8216;setptr&#8217;)</i> fails, and <i><b>doc</b>(&#8216;setptr&#8217;)</i> simply displays the readable help text).</p><p>All these functions are officially unsupported by MathWorks, even when having a readable help section. The rule of thumb appears to be that a Matlab function is supported only if it has online documentation. Note, however, that in some rare cases a documentation discrepancy may be due to a MathWorks documentation error, not to unsupportability&#8230;</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/undocumented-function/">Helper functions</a></b><br
/> Many fully-supported Matlab functions use helper functions that have a specific use in the main (documented) function(s).  Often, these helper functions are tightly-coupled to their documented parents and are useless as stand-alone functions. But quite a few of them have quite useful stand-alone use, as I&#8217;ve already shown in some past articles.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/undocumented-feature/">Undocumented features</a> and <a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/undocumented-property/">properties</a></b><br
/> Features of otherwise-documented Matlab functions, which appear nowhere in the official documentation. You guessed it – these are also not supported by MathWorks&#8230; Like undocumented functions, you can only infer such features by the occasional CSSM post or a reference somewhere in Matlab&#8217;s m-code.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/semi-documented-feature/">Semi-documented features</a></b><br
/> Features of otherwise-documented Matlab functions, which are documented in a separate section beneath the main help section, and nowhere else (not in the full doc not the online documentation). If you did not know in advance that these features existed, you could only learn of them by manually looking at Matlab&#8217;s m-files (which is what I do in most cases&#8230;).</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/undocumented-property/">Undocumented properties</a></b><br
/> Many Matlab objects have internal properties, which can be retrieved (via Matlab&#8217;s <i><b>get</b></i> function) and/or set (via the <i><b>set</b></i> function) programmatically. All these properties are fully documented. Many objects also possess hidden properties, some of which are very interesting and useful, but which are undocumented and (oh yes) unsupported. Like undocumented features, they can only be inferred from CSSM or existing code. In a recent <a
target="_blank" href="http://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/">article</a> I described my <i><b>getundoc</b></i> utility, which lists these undocumented properties of specified objects.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/internal-component/">Internal Matlab classes</a></b><br
/> Matlab uses a vast array of specialized Java classes to handle everything from algorithms to GUI. These classes are (of course) undocumented/unsupported. They can often be accessed directly from the Matlab Command Window or user m-files. GUI classes can be inferred by inspecting the figure frame&#8217;s Java components, and non-GUI classes can often be inferred from references in Matlab&#8217;s m-files.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/JMI">Matlab-Java integration</a></b><br
/> Matlab&#8217;s GUI interface, as well as the Java-to-Matlab interface (JMI) is fully undocumented and unsupported. In addition to JMI, there are other mechanisms to run Matlab code from within Java (namely JMI, COM and DDE) &#8211; these are all unsupported and by-and-large undocumented.</p></li><li><b><a
target="_blank" href="http://undocumentedmatlab.com/?s=UDD">Matlab&#8217;s UDD mechanism</a></b><br
/> UDD (Unified Data Definition?) is used extensively in Matlab as the internal object-oriented mechanism for describing object properties and functionalities. We can use UDD for a wide variety of uses. UDD was described in a series of articles here in early 2011.</li></ul><p>Next week I will list the reasons that cause MathWorks to decide whether a particular feature or property should be documented or not.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/reasons-for-undocumented-matlab-aspects/' rel='bookmark' title='Reasons for undocumented Matlab aspects'>Reasons for undocumented Matlab aspects</a> <small>There are many reasons for the numerous undocumented aspects in Matlab - this article explains them....</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-cursorbar-object/' rel='bookmark' title='Undocumented cursorbar object'>Undocumented cursorbar object</a> <small>Matlab's internal undocumented graphics.cursorbar object can be used to present dynamic data-tip cross-hairs...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uiundo-matlab-undocumented-undo-redo-manager/' rel='bookmark' title='uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager'>uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager</a> <small>The built-in uiundo function provides easy yet undocumented access to Matlab's powerful undo/redo functionality. This article explains its usage....</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-xml-functionality/' rel='bookmark' title='Undocumented XML functionality'>Undocumented XML functionality</a> <small>Matlab's built-in XML-processing functions have several undocumented features that can be used by Java-savvy users...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Uitable sorting</title><link>http://undocumentedmatlab.com/blog/uitable-sorting/</link> <comments>http://undocumentedmatlab.com/blog/uitable-sorting/#comments</comments> <pubDate>Tue, 26 Jul 2011 18:00:01 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JIDE]]></category> <category><![CDATA[uitable]]></category> <category><![CDATA[uitools]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2391</guid> <description><![CDATA[Matlab's uitables can be sortable using simple undocumented features<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitable-customization-report/' rel='bookmark' title='Uitable customization report'>Uitable customization report</a> <small>In last week&#8217;s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives/' rel='bookmark' title='Tab panels &#8211; uitab and relatives'>Tab panels &#8211; uitab and relatives</a> <small>This article describes several undocumented Matlab functions that support tab-panels...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/ref/uitable.html"><i><b>uitable</b></i></a> is probably the most complex basic GUI controls available in Matlab. It displays data in a table within a figure, with settable properties as with any other Matlab Handle-Graphics (HG) control. After many years in which the <i><b>uitable</b></i> was available but <a
target="_blank" href="http://undocumentedmatlab.com/blog/legend-semi-documented-feature/#Semi-documented">semi-documented</a> and not officially supported in Matlab, it finally became fully documented and supported in R2008a (aka Matlab 7.6). At that time its internal implementation has changed from a MathWorks-developed Java table to a <a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/JIDE/">JIDE</a>-based Java table (another JIDE-derived table was described <a
target="_blank" href="http://undocumentedmatlab.com/blog/jide-property-grids/">here</a> last year). Since R2008a, both versions of <i><b>uitable</b></i> are available &#8211; the old version is available by adding the &#8216;v0&#8242; input arg.</p><p>Matlab&#8217;s <i><b>uitable</b></i> exposes only a very limited subset of functionalities and properties to the user. Numerous other functionalities are available by accessing the underlying Java table and hidden Matlab properties. Today I will describe a very <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/answers/1880-uitable-can-the-headers-be-made-clickable">common need</a> in GUI tables, that for some unknown reason is missing in Matlab&#8217;s <i><b>uitable</b></i>: Sorting table data columns.</p><p>Last week I <a
target="_blank" href="http://undocumentedmatlab.com/blog/running-vb-code-in-matlab/">explained</a> how we can modify table headers of an ActiveX table control to display sorting icons. In that case, sorting was built-in the control, and the question was just how to display the sorting arrow icon. Unfortunately, Matlab&#8217;s <i><b>uitable</b></i> does not have sorting built-in, although it&#8217;s quite easy to add it, as I shall now show.</p><h3 id="Old">Old uitable sorting</h3><p>The old <i><b>uitable</b></i> is the default control used until R2007b, or that can be selected with the &#8216;v0&#8242; input arg since R2008a. It was based on an internal MathWorks extension of the standard Java Swing <a
target="_blank" rel="nofollow" href="http://download.oracle.com/javase/tutorial/uiswing/components/table.html">JTable</a> &#8211; a class called <code>com.mathworks.widgets.spreadsheet.SpreadsheetTable</code>.</p><p>Users will normally try to sort columns by clicking the header. This has been a deficiency of JTable for ages. To solve this for the old (pre-R2008a) <i><b>uitable</b></i>, download one of several available JTable sorter classes, or my TableSorter class (available <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14225-java-based-data-table">here</a>).  Add the TableSorter.jar file to your static java classpath (via <code>edit('classpath.txt')</code>) or your dynamic classpath (<code>javaaddpath('TableSorter.jar')</code>).</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display the uitable and get its underlying Java object handle</span>
<span style="color: #080;">&#91;</span>mtable,hcontainer<span style="color: #080;">&#93;</span> = uitable<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color: #0000FF;">gcf</span>, <span style="color: #0000FF;">magic</span><span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>, <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'A'</span>, <span style="color:#A020F0;">'B'</span>, <span style="color:#A020F0;">'C'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;   <span style="color: #228B22;">% discard the 'v0' in R2007b and earlier</span>
jtable = mtable.<span style="">getTable</span>;   <span style="color: #228B22;">% or: get(mtable,'table');</span>
&nbsp;
<span style="color: #228B22;">% We want to use sorter, not data model...</span>
<span style="color: #228B22;">% Unfortunately, UitablePeer expects DefaultTableModel not TableSorter so we need a modified UitablePeer class</span>
<span style="color: #228B22;">% But UitablePeer is a Matlab class, so use a modified TableSorter &amp; attach it to the Model</span>
<span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">which</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'TableSorter'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
   <span style="color: #228B22;">% Add TableSorter as TableModel listener</span>
   sorter = TableSorter<span style="color: #080;">&#40;</span>jtable.<span style="">getModel</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
   jtable.<span style="">setModel</span><span style="color: #080;">&#40;</span>sorter<span style="color: #080;">&#41;</span>;
   sorter.<span style="">setTableHeader</span><span style="color: #080;">&#40;</span>jtable.<span style="">getTableHeader</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
   <span style="color: #228B22;">% Set the header tooltip (with sorting instructions)</span>
   jtable.<span style="">getTableHeader</span>.<span style="">setToolTipText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'&lt;html&gt;&amp;nbsp;&lt;b&gt;Click&lt;/b&gt; to sort up; &lt;b&gt;Shift-click&lt;/b&gt; to sort down&lt;br /&gt;&amp;nbsp;...&lt;/html&gt;'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">else</span>
   <span style="color: #228B22;">% Set the header tooltip (no sorting instructions...)</span>
   jtable.<span style="">getTableHeader</span>.<span style="">setToolTipText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'&lt;html&gt;&amp;nbsp;&lt;b&gt;Click&lt;/b&gt; to select entire column&lt;br /&gt;&amp;nbsp;&lt;b&gt;Ctrl-click&lt;/b&gt; (or &lt;b&gt;Shift-click&lt;/b&gt;) to select multiple columns&amp;nbsp;&lt;/html&gt;'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 607px"><img
alt="Sorted uitable - old version" src="http://www.mathworks.com/matlabcentral/fx_files/14225/1/table.png" title="Sorted uitable - old version" width="597" /><p
class="wp-caption-text">Sorted <i><b>uitable</b></i> - old version</p></div></center></p><h3 id="New">New uitable sorting</h3><p>The new <i><b>uitable</b></i> is based on JIDE&#8217;s <code>com.jidesoft.grid.SortableTable</code> and so has built-in sorting support – all you need to do is to turn it on. First get the underlying Java object using my <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/">FindJObj utility</a>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display the uitable and get its underlying Java object handle</span>
mtable = uitable<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>, <span style="color:#A020F0;">'Data'</span>,<span style="color: #0000FF;">magic</span><span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'ColumnName'</span>,<span style="color: #080;">&#123;</span><span style="color:#A020F0;">'A'</span>, <span style="color:#A020F0;">'B'</span>, <span style="color:#A020F0;">'C'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
jscrollpane = findjobj<span style="color: #080;">&#40;</span>mtable<span style="color: #080;">&#41;</span>;
jtable = jscrollpane.<span style="">getViewport</span>.<span style="">getView</span>;
&nbsp;
<span style="color: #228B22;">% Now turn the JIDE sorting on</span>
jtable.<span style="">setSortable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;		<span style="color: #228B22;">% or: set(jtable,'Sortable','on');</span>
jtable.<span style="">setAutoResort</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
jtable.<span style="">setMultiColumnSortable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
jtable.<span style="">setPreserveSelectionsAfterSorting</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Note: the Matlab <code>mtable</code> handle has a hidden <b>Sortable</b> property, but it has no effect – use the Java property mentioned above instead. I assume that the hidden <b>Sortable</b> property was meant to implement the sorting behavior in R2008a, but MathWorks never got around to actually implement it, and so it remains this way to this day.</p><h3 id="Report">A more detailed report</h3><p>I have prepared a 30-page report about using and customizing Matlab&#8217;s <i><b>uitable</b></i>, which greatly expands on the above. This report is available for a small fee <a
target="_blank" rel="nofollow" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&#038;business=octahedron.ltd@gmail.com&#038;currency_code=USD&#038;amount=35&#038;return=&#038;item_name=Matlab-uitable-report">here</a> (please allow up to 48 hours for email delivery). The report includes the following (more details <a
target="_blank" href="http://undocumentedmatlab.com/blog/uitable-customization-report/">here</a>):</p><ul><li>comparison of the old vs. the new <i><b>uitable</b></i> implementations</li><li>description of the <i><b>uitable</b></i> properties and callbacks</li><li>alternatives to <i><b>uitable</b></i> using a variety of technologies</li><li>updating a specific cell&#8217;s value</li><li>setting background and foreground colors for a cell or column</li><li>using dedicated cell renderer and editor components</li><li>HTML processing</li><li>setting dynamic cell-specific tooltip</li><li>setting dynamic cell-specific drop-down selection options</li><li>using a color-selection drop-down for cells</li><li>customizing scrollbars</li><li>customizing column widths and resizing</li><li>customizing selection behavior</li><li>data sorting (expansion of today&#8217;s article)</li><li>data filtering (similar to Excel&#8217;s data filtering control)</li><li>merging table cells</li><li>programmatically adding/removing rows</li><li>numerous links to online resources</li><li>overview of the JIDE grids package, which contains numerous extremely useful GUI controls and components</li></ul><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitable-customization-report/' rel='bookmark' title='Uitable customization report'>Uitable customization report</a> <small>In last week&#8217;s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives/' rel='bookmark' title='Tab panels &#8211; uitab and relatives'>Tab panels &#8211; uitab and relatives</a> <small>This article describes several undocumented Matlab functions that support tab-panels...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/uitable-sorting/feed/</wfw:commentRss> <slash:comments>16</slash:comments> </item> <item><title>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>3</slash:comments> </item> <item><title>UDD and Java</title><link>http://undocumentedmatlab.com/blog/udd-and-java/</link> <comments>http://undocumentedmatlab.com/blog/udd-and-java/#comments</comments> <pubDate>Wed, 23 Mar 2011 21:04:35 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Donn Shull]]></category> <category><![CDATA[Listener]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2213</guid> <description><![CDATA[UDD provides built-in convenience methods to facilitate the integration of Matlab UDD objects with Java code - this article explains how<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/' rel='bookmark' title='Matlab callbacks for Java events'>Matlab callbacks for Java events</a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/jmi-java-to-matlab-interface/' rel='bookmark' title='JMI &#8211; Java-to-Matlab Interface'>JMI &#8211; Java-to-Matlab Interface</a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/extending-a-java-class-with-udd/' rel='bookmark' title='Extending a Java class with UDD'>Extending a Java class with UDD</a> <small>Java classes can easily be extended in Matlab, using pure Matlab code. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/' rel='bookmark' title='FindJObj &#8211; find a Matlab component&#8217;s underlying Java object'>FindJObj &#8211; find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Once again I welcome Donn Shull, who concludes his series on Matlab&#8217;s undocumented UDD mechanism with a discussion of the UDD-Java relationship.</i></p><h3 id="Introduction">Introduction to the UDD-Java relationship</h3><p>Over the course of this series we have mentioned connections between UDD and Java. In <a
target="_blank" href="http://undocumentedmatlab.com/blog/udd-events-and-listeners/">UDD Events and Listeners</a> we described how in Matlab each Java object can have a UDD companion. In <a
target="_blank" href="http://undocumentedmatlab.com/blog/hierarchical-systems-with-udd/">Hierarchical Systems with UDD</a> we briefly noted that a UDD hierarchy may be passed to Java. This suggests that there is a two way relationship between between UDD and Java.</p><p>In this article we will use some undocumented built-in methods such as <b><i>java</i></b> and <b><i>classhandle</i></b> to explore the UDD-Java relationship. We have used built-in methods for UDD objects before. We have also mentioned the importance of studying code from The MathWorks. When you come across something that looks like it may be a UDD method you can check with the <b><i>which</i></b> command:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">which</span> java -<span style="color: #0000FF;">all</span>
C<span style="color: #F0F;">:</span>\MATLAB\R2010b\toolbox\matlab\general\java.<span style="">m</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% javahandle.com.mathworks.hg.peer.Echo method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% ui.figure method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% hg2utils.HGHandle method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% JavaVisible method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% hg.figure method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% hg.GObject method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% schema.class method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% handle.handle method</span>
java <span style="color: #0000FF;">is</span> a built-in method                               <span style="color: #228B22;">% schema.method method</span>
C<span style="color: #F0F;">:</span>\MATLAB\R2010b\toolbox\rptgen\rptgen\@sgmltag\java.<span style="">m</span>  <span style="color: #228B22;">% sgmltag method</span></pre></div></div><p>If you find <code>schema.class</code> in the comments for built-in methods, then the method is a general UDD method.</p><h3 id="javahandle">UDD javahandle companions for Java object</h3><p>Whenever a Java class is instantiated in Matlab, it is possible to <a
target="_blank" href="http://undocumentedmatlab.com/blog/udd-events-and-listeners/#Java">create a companion UDD object</a>. The created companion can be in either the <code>javahandle</code> or the <code>javahandle_withcallbacks</code> package. The primary reason for creating the companion object is to avoid <a
rel="nofollow" target="_blank" href="http://mathforum.org/kb/message.jspa?messageID=5950839">memory leaks</a> when attaching a Matlab callback to a callback property. It makes sense in general to use the <code>javahandle_withcallbacks</code> package.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #228B22;">% creage a java instance and the companion UDD object</span>
&gt;&gt; javaFrame = javax.<span style="">swing</span>.<span style="">JFrame</span>;
&nbsp;
&gt;&gt; <span style="color: #228B22;">% dot notation</span>
&gt;&gt; javaFrameUDD = javaFrame.<span style="">handle</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;
&nbsp;
&gt;&gt; <span style="color: #228B22;">% or Matlab notation</span>
&gt;&gt; javaFrameUDD = handle<span style="color: #080;">&#40;</span>javaFrame,<span style="color:#A020F0;">'CallbackProperties'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>We can use the built-in <b><i>classhandle</i></b> method to inspect our UDD companion object. This can be used, for example, to obtain a list of the events that the Java class generates:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #228B22;">% use classhandle to list a java classes events</span>
&gt;&gt; jch = javaFrame.<span style="">handle</span>.<span style="">classhandle</span>;
&nbsp;
&gt;&gt; <span style="color: #0000FF;">for</span> index = <span style="color: #33f;">1</span><span style="color: #F0F;">:</span>numel<span style="color: #080;">&#40;</span>jch.<span style="">Events</span><span style="color: #080;">&#41;</span>, <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span>jch.<span style="">Events</span><span style="color: #080;">&#40;</span>index<span style="color: #080;">&#41;</span>.<span style="">Name</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">end</span>
MouseWheelMoved
MouseClicked
MouseEntered
MouseExited
MousePressed
MouseReleased
WindowGainedFocus
WindowLostFocus
WindowActivated
WindowClosed
WindowClosing
WindowDeactivated
WindowDeiconified
WindowIconified
WindowOpened
ComponentHidden
ComponentMoved
ComponentResized
ComponentShown
MouseDragged
MouseMoved
ComponentAdded
ComponentRemoved
AncestorMoved
AncestorResized
FocusGained
FocusLost
WindowStateChanged
HierarchyChanged
CaretPositionChanged
InputMethodTextChanged
PropertyChange
KeyPressed
KeyReleased
KeyTyped
&nbsp;
&gt;&gt; <span style="color: #228B22;">% Use one of the object's callbacks</span>
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>javaFrameUDD,<span style="color:#A020F0;">'WindowGainedFocusCallback'</span>,@myCallbackFcn<span style="color: #080;">&#41;</span>;</pre></div></div><p>If we do not wish to use callback properties, then we can create our UDD companion in the <code>javahandle</code> package and use <b><i>handle.listener</i></b> to respond to events.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">javaFrame = javax.<span style="">swing</span>.<span style="">JFrame</span>;
javaFrameUDD = javaFrame.<span style="">handle</span>;
lis = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>javaFrameUDD,<span style="color:#A020F0;">'WindowGainedFocus'</span>,@myCallbackFcn<span style="color: #080;">&#41;</span>;</pre></div></div><h3 id="Passing">Passing UDD objects to Java code</h3><p>You can pass any UDD object to your Java classes in Matlab. Matlab will create a Java bean adapter for the UDD object. The bean adapter created is a subclass of <code>com.MathWorks.jmi.bean.UDDObject</code>. <code>UDDObject</code> implements the Java interfaces <code>com.MathWorks.jmi.bean.DynamicProperties</code>, <code>com.MathWorks.jmi.bean.MTObject</code>, <code>com.MathWorks.jmi.bean.TreeObject</code>, and <code>com.mathworks.services.Browseable</code>.</p><p>The generated bean adapter will have the methods of the parent class the methods of the UDD class, as well as <i>set</i> and <i>get</i> methods for the class properties. To understand how this works, let&#8217;s start with our <code>simple.object</code> and use the <b><i>java</i></b> method to inspect the bean adapter:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myObj = simple.<span style="">object</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'myObj'</span>, <span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;
&nbsp;
&gt;&gt; <span style="color: #228B22;">% using dot notation with the java method </span>
&gt;&gt; myObj.<span style="">java</span>.<span style="">getClass</span>
<span style="color: #0000FF;">ans</span> =
<span style="color: #0000FF;">class</span> objectBeanAdapter0
&nbsp;
&gt;&gt; myObj.<span style="">java</span>.<span style="color: #0000FF;">methods</span>
&nbsp;
<span style="color: #0000FF;">Methods</span> <span style="color: #0000FF;">for</span> <span style="color: #0000FF;">class</span> objectBeanAdapter0<span style="color: #F0F;">:</span>
&nbsp;
acquireReference                    createNullMatlabObjectListener      lastDown                            
addBelow                            createNullPropertyChangeListener    left                                
addBrowseableListener               <span style="color: #0000FF;">dialog</span>                              notify                              
addFirstBelow                       <span style="color: #0000FF;">disp</span>                                notifyAll                           
addLeft                             dispose                             objectBeanAdapter0                  
addMatlabObjectListener             equals                              releaseReference                    
addObjectPropertyChangeListener     findProperty                        removeBrowseableListener            
addRight                            firstDown                           removeMatlabObjectListener          
browseableCanHaveChildren           getChildAt                          removeObjectPropertyChangeListener  
browseableChild                     getChildCount                       right                               
browseableChildCount                getClass                            setDirtyFlag                        
browseableChildFetchCount           getClassName                        setDynamicPropertyValue             
browseableChildren                  getDynamicProperties                setName                             
browseableDataObject                getDynamicPropertyValue             setPropertyValue                    
browseableDisplayObject             getIndex                            setThreadSafetyCheckLevel           
browseableHasChildren               getIndexOfChild                     setValue                            
browseableNChildren                 getName                             toString                            
browseableNextNSiblings             getNewInstance                      up                                  
browseableNextSibling               getPropertyValue                    updateCache                         
browseableParent                    getValue                            updateChildCount                    
browseablePrevNSiblings             hashCode                            updateIndex                         
browseablePrevSibling               isDirty                             wait                                
checkThreadSafety                   isLeaf                              
clearDirtyFlag                      isObservable                        
compareTo                           isValid</pre></div></div><p>The parent class has added a large number of methods to the bean adapter for our original class. By looking at the list we can see our <i>dialog</i> and <i>disp</i> methods. There are also <i>getName</i>, <i>setName</i>, <i>getValue</i>, and <i>setValue</i> methods for our classes properties. The rest of the methods were inherited from the base <code>UDDObject</code> superclass. We can use any superclass method directly with the bean adapter object. For example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myObj.<span style="">java</span>.<span style="">getPropertyValue</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Name'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
myObj</pre></div></div><h3 id="Interface">Java interface class</h3><p>To be able to use our UDD object in user-written Java code, we need a Java interface class for it. While we could manually write an interface file, UDD provides a very handy convenience method to automatically create the interface file. For this, we use the <b><i>classhandle</i></b> method again. The <code>schema.class</code> object obtained using <b><i>classhandle</i></b> has a method called <i>createJavaInterface</i> that takes two string arguments: the Java interface classname, and the folder in which to place the interface file. The steps to create and use this interface file are:</p><ol><li>Create and test your UDD class</li><li>Create a Java interface file using <code>schema.class</code>&#8216;s <i>CreateJavaInterface</i></li><li>Modify your UDD class definition file (schema.m) to reference the Java interface file</li><li>Create the Java code that uses your class</li></ol><p>For example, to create a Java interface file for the simple object we created above, use the following commands in Matlab:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">classH = classhandle<span style="color: #080;">&#40;</span>myObj<span style="color: #080;">&#41;</span>;
classH.<span style="">createJavaInterface</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'simpleObjectInterface'</span>,<span style="color: #0000FF;">pwd</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>This will create the following simpleObjectInterface.java file in the current working directory:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> simpleObjectInterface 
       <span style="color: #000000; font-weight: bold;">extends</span> com.<span style="color: #006633;">mathworks</span>.<span style="color: #006633;">jmi</span>.<span style="color: #006633;">bean</span>.<span style="color: #006633;">TreeObject</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/* Properties */</span>
    <span style="color: #000000; font-weight: bold;">public</span> java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> getValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setValue<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/* Methods */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> dialog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> disp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>The interface file contains <i>set</i> and <i>get</i> accessor methods for our UDD object properties, and Java prototypes for the UDD methods (in our case, <i>dialog</i> and <i>disp</i>).</p><p>The next step is to modify our class definition file (schema.m) to reference the Java interface file we have created. This modification provides the information that Matlab needs to create the bean adapter that implements the Java interface:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">simpleClass = schema.<span style="color: #0000FF;">class</span><span style="color: #080;">&#40;</span>simplePackage, <span style="color:#A020F0;">'object'</span><span style="color: #080;">&#41;</span>;
simpleClass.<span style="">JavaInterfaces</span> = <span style="color: #080;">&#123;</span> <span style="color:#A020F0;">'simpleObjectInterface'</span> <span style="color: #080;">&#125;</span>;</pre></div></div><p>We can verify that the generated bean adapter implements the interface using Java Reflection techniques. As always when we have made changes to the class definition file, we need to use the <b><i>clear classes</i></b> command, and then recreate our objects:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myObj = simple.<span style="">object</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'myObj'</span>, <span style="color: #0000FF;">pi</span><span style="color: #080;">&#41;</span>
myObj =
  Name<span style="color: #F0F;">:</span> myObj
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">3.141593</span>
&nbsp;
&gt;&gt; myObjBean = java<span style="color: #080;">&#40;</span>myObj<span style="color: #080;">&#41;</span> 
myObjBean =
  Name<span style="color: #F0F;">:</span> myObj
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">3.141593</span>
&nbsp;
&gt;&gt; interfaces = myObjBean.<span style="">getClass</span>.<span style="">getInterfaces</span> 
interfaces =
java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #F0F;">:</span>
    <span style="color: #080;">&#91;</span>java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span><span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; interfaces<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> 
<span style="color: #0000FF;">ans</span> =
interface simpleObjectInterface</pre></div></div><h3 id="Usage">Using UDD in Java</h3><p>Let&#8217;s create a simple Java class that illustrates passing a UDD object to Java. Here we will just have two methods: The first gets the Value property from a class instance and doubles it; the second launches the class instance dialog:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> accessUDDClass 
<span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">double</span> localValue<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> accessUDDClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doubleValue<span style="color: #009900;">&#40;</span>simpleObjectInterface UDDObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    localValue <span style="color: #339933;">=</span> UDDObj.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    UDDObj.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">*</span>localValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> launchDialog<span style="color: #009900;">&#40;</span>simpleObjectInterface UDDObj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    UDDObj.<span style="color: #006633;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>If we have set up the Java compiler and environment variables correctly, we can compile our interface and Java class files from inside Matlab using the system command (alternately, we can compile using any external Java compiler or IDE):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; system<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javac accessUDDClass.java simpleObjectInterface.java'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
     <span style="color: #33f;">0</span></pre></div></div><p>Now test our simple Java class with the UDD object created earlier:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; javaObj = accessUDDClass
javaObj =
accessUDDClass@eb9b73
&nbsp;
&gt;&gt; javaObj.<span style="">doubleValue</span><span style="color: #080;">&#40;</span>myObj<span style="color: #080;">&#41;</span>  <span style="color: #228B22;">% pi =&gt; 2*pi</span>
&nbsp;
&gt;&gt; myObj
myObj =
  Name<span style="color: #F0F;">:</span> myObj
 Value<span style="color: #F0F;">:</span> <span style="color: #33f;">6.283185</span></pre></div></div><p>This concludes the UDD series. I would like to thank Yair for his help in preparing and presenting this information.</p><h3 id="Editor">Editor&#8217;s note</h3><p><i>I would like to thank Donn for his enourmously detailed work on UDD, and for preparing it in easy-to-follow articles. I can personally attest to the huge time investment it has taken him. I trully believe he deserves a warm &#8220;thank you&#8221; from the Matlab community. Please visit <a
target="_blank" rel="nofollow" href="http://aetoolbox.com/default.aspx">Donn&#8217;s website</a>, or add a short <a
href="http://UndocumentedMatlab.com/blog/udd-and-java/#respond">comment</a> below.</p><p>In the following weeks, I return to the regular stuff that made this website famous: solving day-to-day Matlab problems using simple undocumented built-in Matlab gems.<br/>- Yair</i></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/' rel='bookmark' title='Matlab callbacks for Java events'>Matlab callbacks for Java events</a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/jmi-java-to-matlab-interface/' rel='bookmark' title='JMI &#8211; Java-to-Matlab Interface'>JMI &#8211; Java-to-Matlab Interface</a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/extending-a-java-class-with-udd/' rel='bookmark' title='Extending a Java class with UDD'>Extending a Java class with UDD</a> <small>Java classes can easily be extended in Matlab, using pure Matlab code. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/' rel='bookmark' title='FindJObj &#8211; find a Matlab component&#8217;s underlying Java object'>FindJObj &#8211; find a Matlab component&#8217;s underlying Java object</a> <small>The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/udd-and-java/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>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/udd-and-java/' rel='bookmark' title='UDD and Java'>UDD and Java</a> <small>UDD provides built-in convenience methods to facilitate the integration of Matlab UDD objects with Java code - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A few days ago, a user <a
target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/4291769/how-do-i-notify-a-matlab-object-of-an-event-in-a-java-object">posted a question</a> on StackOverflow asking whether it is possible to trap a Java-based event in a Matlab callback.</p><p>It so happens that only a few weeks ago I completed a consulting project which required exactly this. The project was to integrate a Matlab computational engine with a Java interface to <a
target="_blank" rel="nofollow" href="http://www.interactivebrokers.com/">Interactive Brokers</a> (IB) &#8211; a well-known online brokerage firm. The idea was to use the Java interface to fetch real-time data about securities (stocks, bonds, options etc.), use a Matlab processing utility, then use the Java interface again to send Buy or Sell orders back to IB.</p><p>If you are interested in the final result (i.e., a complete and field-tested Matlab-IB interface), <a
target="_blank" href="http://undocumentedmatlab.com/ib-matlab/">look here</a>.</p><h3 id="challenge">The challenge</h3><p>A big challenge in this project (aside from handling quite a few IB interface quirks), was to propagate events from the Java interface to the Matlab application. This had to be done asynchronously, since events such as order execution can occur at any time following the order placement. Moreover, even simple requests such as retrieving security information (bid/ask prices for example) is handled by IB via Java events, not as simple function return values.</p><p>Handling Java-based events in Matlab is not a trivial task. Not only merely undocumented, but it is also not intuitive. I have spent quite a few hours trying to crack this issue. In fact, I believe it was one of my more challenging tasks in figuring out the undocumented aspects of the Matlab-Java interface. Few other challenges were as difficult, yet with a happy ending (Drag &#038; Drop is a similar issue &#8211; I will describe it in another article sometime).</p><h3 id="solution">The solution</h3><p>Fast-forward all the fruitless attempted variations, here is the bottom line. Refer to the following simple Java class example:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EventTest
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Vector</span> data <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Vector</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> addMyTestListener<span style="color: #009900;">&#40;</span>MyTestListener lis<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        data.<span style="color: #006633;">addElement</span><span style="color: #009900;">&#40;</span>lis<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> removeMyTestListener<span style="color: #009900;">&#40;</span>MyTestListener lis<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        data.<span style="color: #006633;">removeElement</span><span style="color: #009900;">&#40;</span>lis<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> MyTestListener <span style="color: #000000; font-weight: bold;">extends</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">EventListener</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">void</span> testEvent<span style="color: #009900;">&#40;</span>MyTestEvent event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyTestEvent <span style="color: #000000; font-weight: bold;">extends</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">EventObject</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">float</span> oldValue,newValue<span style="color: #339933;">;</span>        
        MyTestEvent<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> obj, <span style="color: #000066; font-weight: bold;">float</span> oldValue, <span style="color: #000066; font-weight: bold;">float</span> newValue<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">oldValue</span> <span style="color: #339933;">=</span> oldValue<span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">newValue</span> <span style="color: #339933;">=</span> newValue<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> notifyMyTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Vector</span> dataCopy<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">synchronized</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            dataCopy <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Vector</span><span style="color: #009900;">&#41;</span>data.<span style="color: #006633;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>dataCopy.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            MyTestEvent event <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyTestEvent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>MyTestListener<span style="color: #009900;">&#41;</span>dataCopy.<span style="color: #006633;">elementAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">testEvent</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>When compiling EventTest.java, three class files are created: EventTest.class, EventTest$MyTestEvent.class and EventTest$MyTestListener.class. Place them on Matlab&#8217;s Java static classpath, using <i><b>edit</b>(&#8216;classpath.txt&#8217;)</i> (using the dynamic classpath causes many problems that using the static classpath solves). They can now be accessed as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">which</span> EventTest
EventTest <span style="color: #0000FF;">is</span> a Java method  <span style="color: #228B22;">% EventTest constructor</span>
&nbsp;
&gt;&gt; evt = EventTest
evt =
EventTest@16166fc
&nbsp;
&gt;&gt; evt.<span style="color: #0000FF;">get</span>
	<span style="color: #0000FF;">Class</span> = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">1</span> by <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span> array<span style="color: #080;">&#93;</span>
	TestEventCallback = 
	TestEventCallbackData = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
&nbsp;
	BeingDeleted = off
	ButtonDownFcn = 
	Children = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	Clipping = on
	CreateFcn = 
	DeleteFcn = 
	BusyAction = queue
	HandleVisibility = on
	HitTest = on
	Interruptible = on
	Parent = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	Selected = off
	SelectionHighlight = on
	Tag = 
	<span style="color: #0000FF;">Type</span> = EventTest
	<span style="color: #0000FF;">UIContextMenu</span> = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	UserData = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	Visible = on
&nbsp;
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>evt<span style="color: #080;">&#41;</span>
	<span style="color: #0000FF;">Class</span>
	TestEventCallback<span style="color: #F0F;">:</span> string -or- <span style="color: #0000FF;">function</span> handle -or- <span style="color: #0000FF;">cell</span> array
	<span style="color: #F0F;">...</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>evt,<span style="color:#A020F0;">'TestEventCallback'</span>,@<span style="color: #080;">&#40;</span>h,e<span style="color: #080;">&#41;</span><span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span>h<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>evt<span style="color: #080;">&#41;</span>
	<span style="color: #0000FF;">Class</span> = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">1</span> by <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span> array<span style="color: #080;">&#93;</span>
	TestEventCallback = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">1</span> by <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> function_handle array<span style="color: #080;">&#93;</span>   &lt;= ok
	TestEventCallbackData = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	<span style="color: #F0F;">...</span>
&nbsp;
&gt;&gt; evt.<span style="">notifyMyTest</span>   <span style="color: #228B22;">% invoke Java event</span>
              <span style="color: #33f;">0.0009765625</span>   <span style="color: #228B22;">% &lt;= Matlab callback</span></pre></div></div><p>Note how Matlab automatically converted the Java event testEvent, declared in interface MyTestListener, into a Matlab callback TestEventCallback (the first character is always capitalized). All Java events are automatically converted in this fashion, by appending a &#8216;Callback&#8217; suffix. Here is a code snippet from R2008a&#8217;s \toolbox\matlab\uitools\@opaque\addlistener.m that shows this (slightly edited):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hSrc = handle<span style="color: #080;">&#40;</span>jobj,<span style="color:#A020F0;">'callbackproperties'</span><span style="color: #080;">&#41;</span>;
allfields = <span style="color: #0000FF;">sortrows</span><span style="color: #080;">&#40;</span>fields<span style="color: #080;">&#40;</span><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hSrc<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">for</span> <span style="color: #0000FF;"><span style="color: #33f;">i</span></span> = <span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>allfields<span style="color: #080;">&#41;</span>
   fn = allfields<span style="color: #080;">&#123;</span><span style="color: #0000FF;"><span style="color: #33f;">i</span></span><span style="color: #080;">&#125;</span>;
   <span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">findstr</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Callback'</span>,fn<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">strrep</span><span style="color: #080;">&#40;</span>fn,<span style="color:#A020F0;">'Callback'</span>,<span style="color:#A020F0;">''</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>
&nbsp;
callback = @<span style="color: #080;">&#40;</span>o,e<span style="color: #080;">&#41;</span> cbBridge<span style="color: #080;">&#40;</span>o,e,response<span style="color: #080;">&#41;</span>;
hdl = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>jobj<span style="color: #080;">&#41;</span>, eventName, callback<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">function</span> cbBridge<span style="color: #080;">&#40;</span>o,e,response<span style="color: #080;">&#41;</span>
   hgfeval<span style="color: #080;">&#40;</span>response, java<span style="color: #080;">&#40;</span>o<span style="color: #080;">&#41;</span>, e.<span style="">JavaEvent</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">end</span></pre></div></div><p>Note that <i><b>hgfeval</b></i>, which is used within the cbBridge callback function, is a semi-documented pure-Matlab built-in function, which I <a
target="_blank" href="http://UndocumentedMatlab.com/blog/hgfeval/">described</a> a few weeks ago.</p><p>If several events have the same case-insensitive name, then the additional callbacks will have an appended underscore character (e.g., &#8216;TestEventCallback_&#8217;):</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// In the Java class:</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> MyTestListener <span style="color: #000000; font-weight: bold;">extends</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">EventListener</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">void</span> testEvent<span style="color: #009900;">&#40;</span>MyTestEvent e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">void</span> testevent<span style="color: #009900;">&#40;</span>TestEvent2 e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% …and back in Matlab:</span>
&gt;&gt; evt=EventTest; evt.<span style="color: #0000FF;">get</span>
	<span style="color: #0000FF;">Class</span> = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">1</span> by <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> java.<span style="">lang</span>.<span style="color: #0000FF;">Class</span> array<span style="color: #080;">&#93;</span>
	TestEventCallback = 
	TestEventCallbackData = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	TestEventCallback_ = 
	TestEventCallback_Data = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span> 
	<span style="color: #F0F;">...</span></pre></div></div><p>To complete this discussion, it should be noted that Matlab also automatically defines corresponding Events in the Java object’s <i><b>classhandle</b></i>. Unfortunately, <i><b>classhandle</b></i> events are not differentiated in a similar manner – in this case only a single event is created, named Testevent. <i><b>classhandle</b></i> events, and their relationship to the preceding discussion, will be described in Donn Scull&#8217;s upcoming series on UDD.</p><p>An alternative to using callbacks on Java events, as shown above, is to use undocumented <a
target="_blank" href="http://UndocumentedMatlab.com/blog/continuous-slider-callback/#Event_Listener"><i><b>handle.listener</b></i>s</a>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hListener = <span style="color: #0000FF;">handle.<span style="">listener</span></span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>evt<span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'TestEvent'</span>,callback<span style="color: #080;">&#41;</span>;</pre></div></div><p>There are several other odds and ends, but this article should be sufficient for implementing a fully-functional Java event handling mechanism in Matlab. Good luck!</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/udd-events-and-listeners/' rel='bookmark' title='UDD Events and Listeners'>UDD Events and Listeners</a> <small>UDD event listeners can be used to listen to property value changes and other important events of Matlab objects...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool-callbacks/' rel='bookmark' title='uisplittool &amp; uitogglesplittool callbacks'>uisplittool &#038; uitogglesplittool callbacks</a> <small>Matlab's undocumented uisplittool and uitogglesplittool are powerful toolbar controls - this article explains how to customize their behavior...</small></li><li><a
href='http://undocumentedmatlab.com/blog/udd-and-java/' rel='bookmark' title='UDD and Java'>UDD and Java</a> <small>UDD provides built-in convenience methods to facilitate the integration of Matlab UDD objects with Java code - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/feed/</wfw:commentRss> <slash:comments>27</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           category
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-05-18 21:42:17 -->

<!-- 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:      2.835s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1337402535; expires=Sun, 19-May-2013 04:42:15 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Sat, 19 May 2012 04:42:17 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Sat, 19 May 2012 05:42:17 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               0c0f712c81db96128d7639b3a65a49f4
Content-Encoding:   gzip
-->
