<?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; Java</title> <atom:link href="http://undocumentedmatlab.com/blog/tag/java/feed/" rel="self" type="application/rss+xml" /><link>http://undocumentedmatlab.com</link> <description>Charting Matlab's unsupported hidden underbelly</description> <lastBuildDate>Thu, 02 Feb 2012 00:24:18 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.1</generator> <item><title>Using spinners in Matlab GUI</title><link>http://undocumentedmatlab.com/blog/using-spinners-in-matlab-gui/</link> <comments>http://undocumentedmatlab.com/blog/using-spinners-in-matlab-gui/#comments</comments> <pubDate>Wed, 25 Jan 2012 20:00:16 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Internal component]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2679</guid> <description><![CDATA[Spinner controls can easily be added to Matlab GUI. This article explains how.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/' rel='bookmark' title='Matlab-Java interface using a static control'>Matlab-Java interface using a static control</a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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><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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>One of the few standard Java Swing controls that does not have any Matlab uicontrol counterpart is <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html"><code>JSpinner</code></a>. <code>JSpinner</code> is basically an editbox with two tiny adjacent up/down buttons. Spinners are similar in functionality to a combo-box (a.k.a. drop-down or pop-up menu), where a user can switch between several pre-selected values. They are often used when the list of possible values is too large to display in a combo-box menu. Like combo-boxes, spinners too can be editable (meaning that the user can type a value in the editbox) or not (the user can only &#8220;spin&#8221; the value using the up/down buttons).</p><p><code>JSpinner</code> uses an internal data model, similarly to <code>JTree</code>, <code>JTable</code> and other complex controls. The default model is <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/SpinnerNumberModel.html"><code>SpinnerNumberModel</code></a>, which defines a min/max value (unlimited=[] by default) and step-size (1 by default). Additional predefined models are <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/SpinnerListModel.html"><code>SpinnerListModel</code></a> (which accepts a cell array of possible string values) and <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/SpinnerDateModel.html"><code>SpinnerDateModel</code></a> (which defines a date range and step unit).</p><p>Here&#8217;s a basic code snippet showing how to display a simple numeric spinner for numbers between 20 and 35, with an initial value of 24 and increments of 1:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jModel = javax.<span style="">swing</span>.<span style="">SpinnerNumberModel</span><span style="color: #080;">&#40;</span><span style="color: #33f;">24</span>,<span style="color: #33f;">20</span>,<span style="color: #33f;">35</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
jSpinner = javax.<span style="">swing</span>.<span style="">JSpinner</span><span style="color: #080;">&#40;</span>jModel<span style="color: #080;">&#41;</span>;
jhSpinner = javacomponent<span style="color: #080;">&#40;</span>jSpinner, <span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">10</span>,<span style="color: #33f;">60</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>The spinner value can be set using the edit-box or by clicking on one of the tiny arrow buttons, or programmatically by setting the <b>Value</b> property. The spinner object also has related read-only properties <b>NextValue</b> and <b>PreviousValue</b>. The spinner&#8217;s model object has the corresponding <b>Value</b> (settable), <b>NextValue</b> (read-only) and <b>PreviousValue</b> (read-only) properties. In addition, the model also has the settable <b>Maximum</b>, <b>Minimum</b> and <b>StepSize</b> properties.</p><p>To attach a data-change callback, set the spinner&#8217;s <b>StateChangedCallback</b> property.</p><p>I have created a small Matlab demo, <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/26970-spinnerdemo"><i><b>SpinnerDemo</b></i></a>,  which demonstrates usage of <code>JSpinner</code> in Matlab figures. Each of the three predefined models (number, list, and date) is presented, and the spinner values are inter-connected via their callbacks. The Matlab code is modeled after the <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/examples/components/SpinnerDemoProject/src/components/SpinnerDemo.java">Java code</a> that is used to document <code>JSpinner</code> in the official documentation. Readers are welcome to download this demo from the Matlab File Exchange and reuse its source code.</p><p><center><div
class="wp-caption aligncenter" style="width: 225px"><img
alt="Java's SpinnerDemo" src="http://UndocumentedMatlab.com/images/SpinnerDemoJava.png" title="Java's SpinnerDemo" width="215" height="134"/><p
class="wp-caption-text">Java's SpinnerDemo</p></div> &nbsp;&nbsp;<div
class="wp-caption aligncenter" style="width: 198px"><img
alt="My Matlab SpinnerDemo" src="http://UndocumentedMatlab.com/images/SpinnerDemoMatlab.png" title="My Matlab SpinnerDemo" width="188" height="184"/><p
class="wp-caption-text">My Matlab SpinnerDemo</p></div></center></p><p>As can be seen from the screenshot, <i><b>SpinnerDemo</b></i> also demonstrates how to attach a label to a GUI control with an associated accelerator key (Alt-D in the screenshot example, which sets the focus to the Date control).</p><p>An internal component in Matlab, namely <code>com.mathworks.mwswing.MJSpinner</code>, extends <code>javax.swing.JSpinner</code>, but in this particular case I cannot see any big advantage of using the internal <code>MJSpinner</code> rather than the standard <code>JSpinner</code>. On the contrary, using <code>JSpinner</code> will likely improve forward compatibility &#8211; MathWorks may well change <code>MJSpinner</code> in the future, but it cannot do anything to the standard Swing <code>JSpinner</code>. In other cases, internal Matlab controls do offer significant advantages over the standard Swing controls, but not here it would seem. In any case, the <i><b>SpinnerDemo</b></i> utility uses <code>MJSpinner</code>, but you can safely use <code>JSpinner</code> instead (line #86).</p><p>The internal Matlab controls are discussed in detail in Chapter 5 of my <a
target="_blank" href="http://undocumentedmatlab.com/matlab-java-book/">Matlab-Java book</a>, and <code>MJSpinner</code> is specifically discussed in section 5.2.1.</p><p>Another <code>JSpinner</code> derivative is JIDE&#8217;s <code>com.jidesoft.grid.SpinnerCellEditor</code>, which can be used as the cell-editor component in tables. An example of this was shown in the article about <a
target="_blank" href="http://undocumentedmatlab.com/blog/advanced-jide-property-grids/">Advanced JIDE Property Grids</a> (and section 5.7.5 in the book). You may also be interested in the <code>com.jidesoft.combobox.DateSpinnerComboBox</code>, which presents a control that includes both a date-selection combo-box and a spinner (section 5.7.2):</p><p><center><div
class="wp-caption aligncenter" style="width: 310px"><img
alt="A property grid with spinner control" src="http://UndocumentedMatlab.com/images/PropertyGrid_types.png" title="A property grid with spinner control" width="200" height="165" /><p
class="wp-caption-text">A property grid with spinner control</p></div> &nbsp;&nbsp;<div
class="wp-caption aligncenter" style="width: 289px"><img
alt="JIDE's DateSpinnerComboBox" src="http://UndocumentedMatlab.com/images/DateSpinnerComboBox.png" title="JIDE's DateSpinnerComboBox" width="279" height="228" /><p
class="wp-caption-text">JIDE's DateSpinnerComboBox</p></div></center></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/' rel='bookmark' title='Matlab-Java interface using a static control'>Matlab-Java interface using a static control</a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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><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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/using-spinners-in-matlab-gui/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Matlab-Java memory leaks, performance</title><link>http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/</link> <comments>http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/#comments</comments> <pubDate>Fri, 20 Jan 2012 00:56:10 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Memory]]></category> <category><![CDATA[Semi-documented feature]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[Performance]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2665</guid> <description><![CDATA[Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-performance/' rel='bookmark' title='Plot performance'>Plot performance</a> <small>Undocumented inner plot mechanisms can be used to significantly improved plotting performance...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/' rel='bookmark' title='Matlab-Java interface using a static control'>Matlab-Java interface using a static control</a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/datestr-performance/' rel='bookmark' title='datestr performance'>datestr performance</a> <small>Caching is a simple and very effective means to improve code performance, as demonstrated for the datestr function....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>There are several ways of retrieving information from a Java object into Matlab. On the face of it, all these methods look similar. But it turns out that there are important differences between them in terms of memory leakage and performance.</p><h3 id="Problem">The problem: &#8220;Matlab crashes&#8221; &#8211; now go figure&#8230;</h3><p>A client of one of my Matlab programs recently complained that Matlab crashes after several hours of extensive use of the program. The problem looked like something that is memory related (messages such as Matlab&#8217;s out-of-memory error or Java&#8217;s heap-space error). Apparently this happens even on 64-bit systems having lots of memory, where memory should never be a problem.</p><p>Well, we know that this is only in theory, but in practice Matlab&#8217;s internal memory management has problems that occasionally lead to such crashes. This is one of the reasons, by the way, that recent Matlab releases have added the preference option of increasing the default Java heap space (the previous way to do this was <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-18I2C/">a bit complex</a>). Still, even with a high Java heap space setting and lots of RAM, Matlab crashed after using my program for several hours.</p><p>Not pleasant at all, even a bit of an embarrassment for me. I&#8217;m used to crashing Matlab, but only as a result of my playing around with the internals &#8211; I would hate it to happen to my clients.</p><h3 id="Finding">Finding the leak</h3><p>While we can do little with Matlab&#8217;s internal memory manager, I started searching for the exact location of the memory leak and then to find a way to overcome it. I&#8217;ll save readers the description about the grueling task of finding out exactly where the memory leak occurred in a program that has thousands of lines of code and where events get fired asynchronously on a constant basis. <a
target="_blank" href="http://undocumentedmatlab.com/blog/undocumented-profiler-options/">Matlab Profiler&#8217;s undocumented memory profiling option</a> helped me quite a bit, as well as lots of intuition and trial-and-error. Detecting memory leak is never easy, and I consider myself somewhat lucky this time to have both detected the leak source and a workaround.</p><p>It turned out that the leakage happens in a callback that gets invoked multiple times per second by a Java object (see related articles <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">here</a> and <a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/">here</a>). Each time the Matlab callback function is invoked, it reads the event information from the supplied Java event-data (the callback&#8217;s second input parameter). Apparently, about 1KB of memory gets leaked whenever this event-data is being read. This may appear a very small leak, but multiply this by some 50-100K callback invocations per hour and you get a leakage of 50-100MB/hour. Not a small leak at all; more of a flood you could say&#8230;</p><h3 id="get">Using <i><b>get</b>()</i></h3><p>The leakage culprit turned out to be the following code snippet:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% 160 uSecs per call, with memory leak</span>
eventData  = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hEventData,<span style="color: #080;">&#123;</span><span style="color:#A020F0;">'EventName'</span>,<span style="color:#A020F0;">'ParamNames'</span>,<span style="color:#A020F0;">'EventData'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
eventName  = eventData<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span>;
paramNames = eventData<span style="color: #080;">&#123;</span><span style="color: #33f;">2</span><span style="color: #080;">&#125;</span>;
paramData  = eventData<span style="color: #080;">&#123;</span><span style="color: #33f;">3</span><span style="color: #080;">&#125;</span>.<span style="color: #0000FF;">cell</span>;</pre></div></div><p>In this innocent-looking code, <code>hEventData</code> is a Java object that contains the <b>EventName, ParamNames, EventData</b> properties: <b>EventName</b> is a Java <code>String</code>, that is automatically converted by Matlab&#8217;s <i><b>get</b>()</i> function into a Matlab string (<i><b>char</b></i> array); <b>ParamNames</b> is a Java array of <code>String</code>s, that gets automatically converted into a Matlab cell-array of string; and <b>EventData</b> is a Java array of <code>Object</code>s that needs to be converted into a Matlab cell array using the built-in <i><b>cell</b></i> function, as <a
target="_blank" href="http://undocumentedmatlab.com/blog/converting-java-vectors-to-matlab-arrays/">described</a> in one of my recent articles.</p><p>The code is indeed innocent, works really well and is actually extremely fast: each invocation takes of this code segment takes less than 0.2 millisecs. Unfortunately, because of the memory leak I needed to find a better alternative.</p><h3 id="handle">Using <i><b>handle</b>()</i></h3><p>The first idea was to use the built-in <i><b>handle</b>()</i> function, under the assumption that it would solve the memory leak, as <a
target="_blank" rel="nofollow" href="http://mathforum.org/kb/message.jspa?messageID=5950839">reported here</a>. In fact, MathWorks specifically advises to use <i><b>handle</b>()</i> rather than to work with &#8220;naked&#8221; Java objects, when <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/#memory_leak">setting Java object callbacks</a>. The official documentation of the <i><b>set</b></i> function <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/ref/set.html#f67-433534">says</a>:</p><blockquote><p>Do not use the set function on Java objects as it will cause a memory leak.</p></blockquote><p>It stands to reason then that a similar memory leak happens with <i><b>get</b></i> and that a similar use of <i><b>handle</b></i> would solve this problem:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% 300 uSecs per call, with memory leak</span>
s = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>hEventData<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
eventName  = s.<span style="">EventName</span>;
paramNames = s.<span style="">ParamNames</span>;
paramData  = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>s.<span style="">EventData</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Unfortunately, this variant, although working correctly, still leaks memory, and also performs almost twice as worse than the original version, taking some 0.3 milliseconds to execute per invocation. Looks like this is a dead end.</p><h3 id="accessor">Using Java accessor methods</h3><p>The next attempt was to use the Java object&#8217;s internal accessor methods for the requested properties. These are <code>public</code> methods of the form <i>getXXX(), isXXX(), setXXX()</i> that enable Matlab to treat XXX as a property by its <i><b>get</b></i> and <i><b>set</b></i> functions. In our case, we need to use the getter methods, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% 380 uSecs per call, no memory leak</span>
eventName  = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>hEventData.<span style="">getEventName</span><span style="color: #080;">&#41;</span>;
paramNames = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>hEventData.<span style="">getParamNames</span><span style="color: #080;">&#41;</span>;
paramData  = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>hEventData.<span style="">getEventData</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Here, the method <i>getEventName()</i> returns a Java <code>String</code>, that we convert into a Matlab string using the <i><b>char</b></i> function. In our previous two variants, the <i><b>get</b></i> function did this conversion for us automatically, but when we use the Java method directly we need to convert the results ourselves. Similarly, when we call <i>getParamNames()</i>, we need to use the <i><b>cell</b></i> function to convert the Java <code>String[]</code> array into a Matlab cell array.</p><p>This version at last doesn&#8217;t leak any memory. Unfortunately, it has an even worse performance: each invocation takes almost 0.4 milliseconds. The difference may seem insignificant. However, recall that this callback gets called dozens of times each second, so the total adds up quickly. It would be nice if there were a faster alternative that does not leak any memory.</p><h3 id="struct">Using <i><b>struct</b>()</i></h3><p>Luckily, I found just such an alternative. At 0.24 millisecs per invocation, it is almost as fast as the leaky best-performance original <i><b>get</b></i> version. Best of all, it leaks no memory, at least none that I could detect.</p><p>The mechanism relies on the little-known fact that public fields of Java objects can be retrieved in Matlab using the built-in <i><b>struct</b></i> function. For example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; fields = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="color: #0000FF;">Rectangle</span><span style="color: #080;">&#41;</span>
fields = 
             x<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
             y<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
         width<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
        height<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
      OUT_LEFT<span style="color: #F0F;">:</span> <span style="color: #33f;">1</span>
       OUT_TOP<span style="color: #F0F;">:</span> <span style="color: #33f;">2</span>
     OUT_RIGHT<span style="color: #F0F;">:</span> <span style="color: #33f;">4</span>
    OUT_BOTTOM<span style="color: #F0F;">:</span> <span style="color: #33f;">8</span>
&nbsp;
&gt;&gt; fields = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Dimension</span><span style="color: #080;">&#41;</span>
fields = 
     width<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
    height<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span></pre></div></div><p>Note that this useful mechanism is not mentioned in <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/matlab_external/f4873.html#f46643">the main documentation page for accessing Java object fields</a>, although it is indeed mentioned in <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/matlab_external/f6671.html#f61403">another doc-page</a> &#8211; I guess this is a documentation oversight.</p><p>In any case, I converted my Java object to use public (rather than private) fields, so that I could use this <i><b>struct</b></i> mechanism (Matlab can only access public fields). Yes I know that using private fields is a better programming practice and all that (I&#8217;ve programmed OOP for some 15 years&#8230;), but sometimes we need to do ugly things in the interest of performance. The latest version now looks like this:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% 240 uSecs per call, no memory leak</span>
s = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span>hEventData<span style="color: #080;">&#41;</span>;
eventName  = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>s.<span style="">eventName</span><span style="color: #080;">&#41;</span>;
paramNames = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>s.<span style="">paramNames</span><span style="color: #080;">&#41;</span>;
paramData  = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>s.<span style="">eventData</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>This solved the memory leakage issue for my client. I felt fortunate that I was not only able to detect Matlab&#8217;s memory leak but also find a working workaround without sacrificing performance or functionality.</p><p>In this particular case, I was lucky to have full control over my Java object, to be able to convert its fields to become public. Unfortunately, we do not always have similar control over the object that we use, because they were coded by a third party.</p><p>By the way, Matlab itself uses this <i><b>struct</b></i> mechanism in its code-base. For example, Matlab timers are implemented using Java objects (<code>com.mathworks.timer.TimerTask</code>). The timer callback in Matlab code converts the Java timer event data into a Matlab struct using the <i><b>struct</b></i> function, in <i>%matlabroot%/toolbox/matlab/iofun/@timer/timercb.m</i>. The users of the timer callbacks then get passed a simple Matlab EventData struct without ever knowing that the original data came from a Java object.</p><p>As an interesting corollary, this same <i><b>struct</b></i> mechanism can be used to detect internal properties of Matlab class objects. For example, in the timers again, we can get the underlying timer&#8217;s Java object as follows (note the highlighted warning, which I find a bit ironic given the context):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; timerObj = timerfind
&nbsp;
   Timer Object<span style="color: #F0F;">:</span> timer-<span style="color: #33f;">1</span>
&nbsp;
   Timer Settings
      ExecutionMode<span style="color: #F0F;">:</span> singleShot
             Period<span style="color: #F0F;">:</span> <span style="color: #33f;">1</span>
           BusyMode<span style="color: #F0F;">:</span> drop
            Running<span style="color: #F0F;">:</span> off
&nbsp;
   Callbacks
           TimerFcn<span style="color: #F0F;">:</span> @myTimerFcn
           ErrorFcn<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
           StartFcn<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
            StopFcn<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
&nbsp;
&gt;&gt; timerFields = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span>timerObj<span style="color: #080;">&#41;</span>
<span style="display:block;background-color: #ffc;"><span style="color: #0000FF;">Warning</span><span style="color: #F0F;">:</span> Calling <span style="color: #0000FF;">STRUCT</span> on an object prevents the object from hiding its implementation details and should thus be avoided.</span><span style="display:block;background-color: #ffc;"><span style="">Use</span> <span style="color: #0000FF;">DISP</span> or DISPLAY to see the visible public details of an object. <span style="">See</span> <span style="color:#A020F0;">'help struct'</span> <span style="color: #0000FF;">for</span> <span style="color: #0000FF;">more</span> information.</span><span style="display:block;background-color: #ffc;"><span style="color: #080;">&#40;</span><span style="color: #0000FF;">Type</span> &quot;warning off MATLAB<span style="color: #F0F;">:</span>structOnObject&quot; to suppress this <span style="color: #0000FF;">warning</span>.<span style="color: #080;">&#41;</span></span>timerFields = 
         ud<span style="color: #F0F;">:</span> <span style="color: #080;">&#123;</span><span style="color: #080;">&#125;</span>
    jobject<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 javahandle.<span style="">com</span>.<span style="">mathworks</span>.<span style="">timer</span>.<span style="">TimerTask</span><span style="color: #080;">&#93;</span></pre></div></div><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-performance/' rel='bookmark' title='Plot performance'>Plot performance</a> <small>Undocumented inner plot mechanisms can be used to significantly improved plotting performance...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/' rel='bookmark' title='Matlab-Java interface using a static control'>Matlab-Java interface using a static control</a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/datestr-performance/' rel='bookmark' title='datestr performance'>datestr performance</a> <small>Caching is a simple and very effective means to improve code performance, as demonstrated for the datestr function....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/feed/</wfw:commentRss> <slash:comments>9</slash:comments> </item> <item><title>Converting Java vectors to Matlab arrays</title><link>http://undocumentedmatlab.com/blog/converting-java-vectors-to-matlab-arrays/</link> <comments>http://undocumentedmatlab.com/blog/converting-java-vectors-to-matlab-arrays/#comments</comments> <pubDate>Wed, 14 Dec 2011 18:00:47 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[Undocumented function]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2613</guid> <description><![CDATA[Converting Java vectors to Matlab arrays is pretty simple - this article explains how.<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/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/jboost-integrating-an-external-java-library-in-matlab/' rel='bookmark' title='JBoost &#8211; Integrating an external Java library in Matlab'>JBoost &#8211; Integrating an external Java library in Matlab</a> <small>This article shows how an external Java library can be integrated in Matlab...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/' rel='bookmark' title='Matlab-Java interface using a static control'>Matlab-Java interface using a static control</a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Matlab includes built-in support for automatic conversion of Matlab cell arrays into Java arrays. This is important in cases when we need to pass information to a Java function that expects an array (e.g., <code>String[]</code>).</p><h3 id="Numeric">Numeric data array</h3><p>In some cases, namely Java numeric arrays, Matlab also automatically converts the Java array into Matlab arrays. This is actually inconvenient when we would like to access the original Java reference in order to modify some value &#8211; since the Java reference is inaccessible from Matlab in this case, the data is immutable.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; jColor = java.<span style="">awt</span>.<span style="">Color</span>.<span style="">red</span>
jColor =
java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#91;</span>r=<span style="color: #33f;">255</span>,g=<span style="color: #33f;">0</span>,b=<span style="color: #33f;">0</span><span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; matlabData = jColor.<span style="">getColorComponents</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
matlabData =
     <span style="color: #33f;">1</span>
     <span style="color: #33f;">0</span>     <span style="color: #228B22;">% &lt; = immutable array of numbers, not a reference to int[]</span>
     <span style="color: #33f;">0</span></pre></div></div><h3 id="Nonnumeric">Non-numeric array</h3><p>Very often we encounter cases in Java where the information is stored in an array of non-numeric data. In such cases we need to apply a non-automatic conversion from Java into Matlab.</p><p>If the objects are of exactly the same type, then we could store them in a simple Matlab array; otherwise (as can be seen in the example below), we could store them in either a simple array of <i><b>handle</b></i>s, or in a simple cell array:</p> </pre><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; jFrames = java.<span style="">awt</span>.<span style="">Frame</span>.<span style="">getFrames</span>
jFrames =
java.<span style="">awt</span>.<span style="">Frame</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #F0F;">:</span>
    <span style="color: #080;">&#91;</span>javax.<span style="">swing</span>.<span style="">SwingUtilities</span>$SharedOwnerFrame <span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLMainFrame</span>          <span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLMultipleClientFrame</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">MJFrame</span>               <span style="color: #080;">&#93;</span>
&nbsp;
<span style="color: #228B22;">% Alternative #1 - use a loop</span>
&gt;&gt; mFrames = handle<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">for</span> idx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>jFrames<span style="color: #080;">&#41;</span>; mFrames<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span>=handle<span style="color: #080;">&#40;</span>jFrames<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">end</span>
&gt;&gt; mFrames
mFrames =
	handle<span style="color: #F0F;">:</span> <span style="color: #33f;">1</span>-by-<span style="color: #33f;">4</span>
&gt;&gt; mFrames<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
	javahandle.<span style="">javax</span>.<span style="">swing</span>.<span style="">SwingUtilities</span>$SharedOwnerFrame
&gt;&gt; mFrames<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
	javahandle.<span style="">com</span>.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLMainFrame</span>
&nbsp;
<span style="color: #228B22;">% Alternative #2a - convert into a Matlab cell array</span>
&gt;&gt; mFrames = jFrames.<span style="color: #0000FF;">cell</span>
mFrames = 
    <span style="color: #080;">&#91;</span>1x1 javax.<span style="">swing</span>.<span style="">SwingUtilities</span>$SharedOwnerFrame <span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>1x1 com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLMainFrame</span>          <span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>1x1 com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLMultipleClientFrame</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>1x1 com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">MJFrame</span>               <span style="color: #080;">&#93;</span>
&nbsp;
<span style="color: #228B22;">% Alternative #2b - convert to a cell array (equivalent variant of alternative 2a)</span>
&gt;&gt; mFrames = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>jFrames<span style="color: #080;">&#41;</span>;</pre></div></div><p>Note that if we only need to access a particular item in the Java vector or array, we could do that directly, without needing to convert the entire data into Matlab first. Simply use <code>jFrames(1)</code> to directly access the first item in the <code>jFrames</code> array, for example.</p><p>(note: Java Frames are discussed in chapters 7 and 8 of my Matlab-Java book).</p><h3 id="Collections">Vectors and other Collections</h3><p>Very often we encounter cases in Java where the information is stored in a <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/collections/index.html">Java Collection</a> rather than in a simple Java array. The basic mechanism for the conversion in this case is to first convert the Java data into a simple Java array (in cases it was not so in the first place), and then to convert this into a Matlab array using either the automated conversion (if the data is numeric), or using a for loop (ugly and slow!), or into a cell array using the <b><i>cell</i></b> function, as explained above.</p><p>Different Collections have different manners of converting into a Java array: some Collections return an Iterator/Enumerator that can be processed in a loop (be careful not to reset the iterator reference by re-reading it within the loop):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Wrong way - causes an infinite loop</span>
idx = <span style="color: #33f;">1</span>;
props = java.<span style="">lang</span>.<span style="">System</span>.<span style="">getProperties</span>;
<span style="color: #0000FF;">while</span> props.<span style="">elements</span>.<span style="">hasMoreElements</span>
    mPropValue<span style="color: #080;">&#123;</span>idx<span style="color: #080;">&#125;</span> = props.<span style="">elements</span>.<span style="">nextElement</span>;
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Right way</span>
idx = <span style="color: #33f;">1</span>;
propValues = java.<span style="">lang</span>.<span style="">System</span>.<span style="">getProperties</span>.<span style="">elements</span>;  <span style="color: #228B22;">% Enumerator</span>
<span style="color: #0000FF;">while</span> propValues.<span style="">hasMoreElements</span>
    mPropValue<span style="color: #080;">&#123;</span>idx<span style="color: #080;">&#125;</span> = propValues.<span style="">nextElement</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>(note: system properties are discussed in section 1.9 of my Matlab-Java book; Collections are discussed in section 2.1)</p><p>Other Collections, such as <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Vector.html"><code>java.util.Vector</code></a>, have a <i>toArray()</i> method that directly converts into a Java array, and we can process from there as described above:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; jVector = java.<span style="">util</span>.<span style="">Vector</span>;
&gt;&gt; jVector.<span style="">add</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>; jVector.<span style="">add</span><span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>; jVector.<span style="">add</span><span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>;
&gt;&gt; jVector.<span style="">addAll</span><span style="color: #080;">&#40;</span>jv<span style="color: #080;">&#41;</span>; jVector.<span style="">addAll</span><span style="color: #080;">&#40;</span>jv<span style="color: #080;">&#41;</span>;
&gt;&gt; jVector
jVector =
<span style="color: #080;">&#91;</span><span style="color: #33f;">1.0</span>, <span style="color: #33f;">2.0</span>, <span style="color: #33f;">3.0</span>, <span style="color: #33f;">1.0</span>, <span style="color: #33f;">2.0</span>, <span style="color: #33f;">3.0</span>, <span style="color: #33f;">1.0</span>, <span style="color: #33f;">2.0</span>, <span style="color: #33f;">3.0</span>, <span style="color: #33f;">1.0</span>, <span style="color: #33f;">2.0</span>, <span style="color: #33f;">3.0</span><span style="color: #080;">&#93;</span>
&nbsp;
<span style="color: #228B22;">% Now convert into a Matlab cell array via a Java simple array</span>
&gt;&gt; mCellArray = jVector.<span style="">toArray</span>.<span style="color: #0000FF;">cell</span>
mCellArray = 
    <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;">2</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</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;">2</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</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;">2</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</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;">2</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span></pre></div></div><h3 id="Performance">Performance</h3><p>It so happens, that the undocumented built-in <a
target="_blank" href="http://undocumentedmatlab.com/blog/undocumented-feature-function/"><i><b>feature</b></i> function</a> (or its near-synonym <i><b>system_dependent</b></i>) enables improved performance in this conversion process. <i><b>feature</b></i>(44) accepts a <code>java.util.Vector</code> and converts it directly into a Matlab cell-array, in one third to one-half the time that it would take the equivalent <i>toArray.cell()</i> (the third input argument is the number of columns in the result - the reshaping is done automatically):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; mCellArray = feature<span style="color: #080;">&#40;</span><span style="color: #33f;">44</span>,jVector,jVector.<span style="color: #0000FF;">size</span><span style="color: #080;">&#41;</span>   <span style="color: #228B22;">% jVector.size = 12</span>
mCellArray = 
    <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;">2</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</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;">2</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</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;">2</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</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;">2</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; mCellArray = feature<span style="color: #080;">&#40;</span><span style="color: #33f;">44</span>,jVector,<span style="color: #33f;">4</span><span style="color: #080;">&#41;</span>
mCellArray = 
    <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;">1</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;">1</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>    <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>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span></pre></div></div><p>The conversion process is pretty efficient: On my system, the regular <i>toArray.cell()</i> takes 0.45 seconds for a 100K vector, compared to 0.21 seconds for the <i><b>feature</b></i> alternative. However, this small difference could be important in cases where performance is crucial, for example in <a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/">processing of highly-active Java events in Matlab callbacks</a>, or when retrieving data from a database. And this latter case is indeed where a sample usage of this <i><b>feature</b></i> can be found, namely in the <i><b>cursor.fetch.m</b></i> function (where it appears as <i><b>system_dependent(44)</b></i>).</p><p>Please note that both <i><b>feature</b></i> and <i><b>system_dependent</b></i> are highly prone to change without prior warning in some future Matlab release. On the other hand, the conversion methods that I presented above, excluding <i><b>feature</b></i>, will probably still be valid in all Matlab releases in the near future.</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/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/jboost-integrating-an-external-java-library-in-matlab/' rel='bookmark' title='JBoost &#8211; Integrating an external Java library in Matlab'>JBoost &#8211; Integrating an external Java library in Matlab</a> <small>This article shows how an external Java library can be integrated in Matlab...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/' rel='bookmark' title='Matlab-Java interface using a static control'>Matlab-Java interface using a static control</a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/converting-java-vectors-to-matlab-arrays/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Common javacomponent problems</title><link>http://undocumentedmatlab.com/blog/common-javacomponent-problems/</link> <comments>http://undocumentedmatlab.com/blog/common-javacomponent-problems/#comments</comments> <pubDate>Wed, 07 Dec 2011 18:00:38 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[GUIDE]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2607</guid> <description><![CDATA[The javacomponent function is very useful for placing Java components on-screen, but has a few quirks.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/javacomponent/' rel='bookmark' title='The javacomponent function'>The javacomponent function</a> <small>Matlab's built-in javacomponent function can be used to display Java components in Matlab application - this article details its usages and limitations...</small></li><li><a
href='http://undocumentedmatlab.com/blog/javacomponent-background-color/' rel='bookmark' title='Javacomponent background color'>Javacomponent background color</a> <small>This article explains how to align Java component background color with a Matlab color....</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/customizing-uitree-nodes-2/' rel='bookmark' title='Customizing uitree nodes &#8211; part 2'>Customizing uitree nodes &#8211; part 2</a> <small>This article shows how Matlab GUI tree nodes can be customized with checkboxes and similar controls...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>The <i><b>javacomponent</b></i> function, which I described <a
target="_blank" href="http://UndocumentedMatlab.com/blog/javacomponent/">here</a> last year, is a very important built-in Matlab function that enables placing Java components in Matlab figure GUI. Using <i><b>javacomponent</b></i> is pretty straight-forward. However, there are a few quirks that users should be aware of. In today&#8217;s article I&#8217;ll try to highlight some of them and discuss workarounds.</p><h3 id="figure-vis">Figure visibility</h3><p>Java components can only be placed onscreen when their containing Matlab figure has been made visible. This means that calls to <i><b>javacomponent</b></i> cannot be placed at the GUIDE-created *_OpeningFcn() function, because that function is invoked <i>before</i> the figure window is made visible.</p><p>Of course, we can always force the figure to become visible by setting the figure&#8217;s <b>Visible</b> property to <code>'on'</code> in this function, before calling our <i><b>javacomponent</b></i>s. But IMHO, a better option would be to simply place the <i><b>javacomponent</b></i>s in the corresponding GUIDE-created *_OutputFcn() function, which is invoked <i>after</i> the figure window is made visible.</p><h3 id="parent-vis">Auto-hiding with parent container</h3><p>Java components are not automatically hidden with their ancestor container panel. This is also the root cause of the failure of Java components to disappear when switching tabs in a <i><b>uitab</b></i>.</p><p>One simple workaround for this that I often use is to link the <b>Visible</b> properties of the <i><b>javacomponent</b></i> container and the parent container:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jButton = javax.<span style="">swing</span>.<span style="">JButton</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'click me!'</span><span style="color: #080;">&#41;</span>;
<span style="color: #080;">&#91;</span>jhButton, hContainer<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span>jButton, <span style="color: #080;">&#91;</span><span style="color: #33f;">100</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">60</span>,<span style="color: #33f;">30</span><span style="color: #080;">&#93;</span>, hParent<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">setappdata</span><span style="color: #080;">&#40;</span>hParent, <span style="color:#A020F0;">'linked_props__'</span>, linkprop<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span>hParent,hContainer<span style="color: #080;">&#93;</span>,<span style="color:#A020F0;">'Visible'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>This has indeed been fixed in R2010b. If you ask me, this should have been standard behavior of <i><b>javacomponent</b></i> since the very beginning&#8230;</p><p>Although there is no need for the workaround in R2010b onward, I usually keep the workaround because it doesn&#8217;t hurt and enables backward compatibility for users who may have an older Matlab release.</p><h3 id="parent-types">Possible parent container types</h3><p><i><b>javacomponent</b></i> accepts parent handles that are figures, toolbars, <i><b>uipanel</b></i>s, or <a
target="_blank" href="http://UndocumentedMatlab.com/blog/matlab-layout-managers-uicontainer-and-relatives"><i><b>uicontainer</b></i>s</a> (some of these are not documented as possible parents in some Matlab releases, but they are). Since R2008a, parents of type <a
target="_blank" href="http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool/"><i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i></a> can also be used. Unfortunately, frames are not <i><b>uicontainer</b></i>s and, therefore, cannot be used as <i><b>javacomponent</b></i> parents.</p><p>Note: Due to a bug in R2007a, <i><b>javacomponent</b></i>s cannot be added to <i><b>uicontainer</b></i>s, since <i>javacomponent.m</i> checks if <code>isa(hParent,'uicontainer')</code> (and similarly for <code>'uiflowcontainer', 'uigridcontainer'</code>), instead of <code>isa(hParent,'hg.uicontainer')</code> (and similarly for the others). If we modify <i>javacomponent.m</i> accordingly (add &#8220;hg.&#8221; in lines 98-100), this bug will be fixed. Since R2007b, <code>isa(…,'hg.uicontainer')</code> is equivalent to <code>isa(…,'uicontainer')</code>, so this fix is unnecessary.</p><h3 id="inputs">Input parameters</h3><p>Unlike many other Matlab functions, <i><b>javacomponent</b></i> does not accept optional parameter-value (P-V) pairs. If we want to customize the appearance of the Java component, it is better to create it , customize it, and only then to present it onscreen using <i><b>javacomponent</b></i>. If we first present the component and then customize it, there might be all sorts of undesirable flicker effects while the component is changing its properties.</p><p>One of the parameters that unfortunately cannot be customized before calling <i><b>javacomponent</b></i> is the component&#8217;s position onscreen. <i><b>javacomponent</b></i> only accepts a position vector in pixel units. To modify the component to use normalized units, we need to modify the container&#8217;s properties:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jButton = javax.<span style="">swing</span>.<span style="">JButton</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'click me!'</span><span style="color: #080;">&#41;</span>;
<span style="color: #080;">&#91;</span>jhButton, hContainer<span style="color: #080;">&#93;</span> = javacomponent<span style="color: #080;">&#40;</span>jButton, <span style="color: #080;">&#91;</span><span style="color: #33f;">100</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">60</span>,<span style="color: #33f;">30</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hContainer, <span style="color:#A020F0;">'Units'</span>,<span style="color:#A020F0;">'norm'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Similarly, we can set the container&#8217;s <b>UserData</b> and <b>ApplicationData</b> only after the call to <i><b>javacomponent</b></i>.</p><h3 id="bg-color">Background color</h3><p>The default background color of <i><b>javacomponent</b></i>s is a slightly different shade of gray than the default <i><b>uicontrol</b></i> background color. Please refer to my recent <a
target="_blank" href="http://UndocumentedMatlab.com/blog/javacomponent-background-color/">article</a> for a detailed discussion of this issue.</p><h3 id="alignment">Vertical alignment</h3><p>Java components are slightly mis-aligned vertically with combo-box (<b>Style</b>=&#8217;popup&#8217;) <i><b>uicontrol</b></i>s, even when positioned using the same Y position. This is actually due to an apparent bug in Matlab&#8217;s implementation of the combo-box <i><b>uicontrol</b></i>, and not in the Java component&#8217;s: Apparently, the Matlab control does not obey its specified height and uses some other default height.</p><p>If we place <i><b>javacomponent</b></i>s side-by-side with a regular Matlab popup <i><b>uicontrol</b></i>s and give them all the same Y position, we can see this mis-alignment. It is only a few pixels, but the effect is visible and disturbing. To fix it, we need to add a small offset to the <i><b>javacomponent</b></i>&#8216;s container&#8217;s <b>Position</b> property to make both the initial Y position slightly lower, and the height value slightly higher than the values for the corresponding Matlab combo-box control.</p><h3 id="callbacks">Callbacks</h3><p>Unlike Matlab <i><b>uicontrol</b></i> callbacks, Java callbacks are activated even when the affected value does not change. Therefore, setting a value in the component&#8217;s callback could well cause an infinite loop of invoked callbacks.</p><p>Matlab programmers often use the practice of modifying component value within the callback function, as a means of fixing user-entered values to be within a certain data range, or in order to format the displayed value. This is relatively harmless in Matlab (if done correctly) since updating a Matlab <i><b>uicontrol</b></i>&#8216;s value to the current value does not retrigger the callback. But since this is not the case with Java callbacks, users should beware not to use the same practice there. In cases where this cannot be avoided, users should at least implement some sort of <a
target="_blank" href="http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy/">callback re-entrancy prevention logic</a>.</p><h3 id="EDT">EDT</h3><p>Java components typically need to use the independent Java Event processing Thread (EDT). EDT is very important for Matlab GUI, as explained in <a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/">this article</a>. Failure to use EDT properly in Matlab can lead to unexpected GUI behavior and even Matlab hangs or crashes.</p><p>If the <i><b>javacomponent</b></i> function is called in a very specific syntax format where the first input arg is a string (the name of the Java class to be created), then the newly-created component is placed on the EDT. However, this is not the normal manner in which <i><b>javacomponent</b></i> is used: A much more typical use-case is where <i><b>javacomponent</b></i> is passed a reference handle to a previously-created Java component. In such cases, it is the user&#8217;s responsibility to place the component on the EDT. Until R2008a this should be done using the <i><b>awtcreate</b></i> function; since R2008b, we can use the much simpler <i><b>javaObjectEDT</b></i> function (<i><b>javaObjectEDT</b></i> actually existed since R2008a, but was buggy on that release so I suggest using it only starting in R2008b; it became documented starting in R2009a). In fact, modern <i><b>javacomponent</b></i> saves us the trouble, by automatically using <i><b>javaObjectEDT</b></i> to auto-delegate the component onto the EDT, even if we happen to have created it on the MT.</p><p>The paragraph above may lead us to believe that we only need to worry about EDT in R2008a and earlier. Unfortunately, this is not the case. Modern GUI requires using many sub-components, that are attached to their main component (e.g., CellRenderers and CellEditors). <i><b>javacomponent</b></i> only bothers to place the main component on the EDT &#8211; not any of the sub-components. We need to handle these separately. Liberally auto-delegating components to the EDT seems like a safe and painless habit to have.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/javacomponent/' rel='bookmark' title='The javacomponent function'>The javacomponent function</a> <small>Matlab's built-in javacomponent function can be used to display Java components in Matlab application - this article details its usages and limitations...</small></li><li><a
href='http://undocumentedmatlab.com/blog/javacomponent-background-color/' rel='bookmark' title='Javacomponent background color'>Javacomponent background color</a> <small>This article explains how to align Java component background color with a Matlab color....</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/customizing-uitree-nodes-2/' rel='bookmark' title='Customizing uitree nodes &#8211; part 2'>Customizing uitree nodes &#8211; part 2</a> <small>This article shows how Matlab GUI tree nodes can be customized with checkboxes and similar controls...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/common-javacomponent-problems/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Reasons for undocumented Matlab aspects</title><link>http://undocumentedmatlab.com/blog/reasons-for-undocumented-matlab-aspects/</link> <comments>http://undocumentedmatlab.com/blog/reasons-for-undocumented-matlab-aspects/#comments</comments> <pubDate>Wed, 30 Nov 2011 18:00:10 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Java]]></category> <category><![CDATA[Mex]]></category> <category><![CDATA[Internal component]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2535</guid> <description><![CDATA[There are many reasons for the numerous undocumented aspects in Matlab - this article explains them.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/' rel='bookmark' title='Types of undocumented Matlab aspects'>Types of undocumented Matlab aspects</a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li><li><a
href='http://undocumentedmatlab.com/blog/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><li><a
href='http://undocumentedmatlab.com/blog/more-undocumented-timing-features/' rel='bookmark' title='More undocumented timing features'>More undocumented timing features</a> <small>There are several undocumented ways in Matlab to get CPU and clock data...</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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>Why are there so many undocumented aspects in Matlab?</i></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, and I wanted to expand on it in today&#8217;s article.</p><h3 id="Unofficial">Unofficial explanations</h3><p>There appear to be several different reasons for Matlab&#8217;s undocumented/unsupported features, whose types I categorized in last week&#8217;s article. Note that the following are all my personal interpretation and are not officially sanctioned in any manner:</p><ul><li><b>Pre-release (beta mode)</b><br
/> MathWorks takes pride in only releasing features/functions for mass use after extensive quality testing. Some features/functions are simply not at the requested level for public use under this criterion. For example, they may work only in a certain situation and not another; may not have bullet-proof error handling etc.</p></li><li><b>Internal use within Matlab</b><br
/> These are functions or properties which are used by other (documented) Matlab functions, but deemed unsuitable for mass use for similar reasons, or because TMW did not believe they might be of any practical use to users.</p></li><li><b>Grandfathered (deprecated)</b><br
/> Some features and functions are replaced with newer versions in newer Matlab versions. The old features/functions are often preserved for the sake of backward compatibility, for one or more such future Matlab versions.</p></li><li><b>Mis-documentation / documentation error / oversight</b><br
/> Matlab is an extensively documented product, and as in any such technical documentation of a rapidly-evolving product, documentation errors are bound to appear. Unfortunately, Matlab users rely on this documentation in order to use Matlab, so any mis- or un-documentation directly affects usage.</p></li><li><b>Java</b><br
/> This is perhaps the largest source of undocumented features within Matlab. For a variety of reasons, the Matlab-Java interface has not been documented to the same extent as the interface with other programming languages like Fortran or C/C++. Matlab itself is based on Java to a large extent, and perhaps TMW does not feel comfortable with users playing around with Matlab internals. Or perhaps TMW is afraid that improper use of the EDT (discussed later in this book) would result in system hangs or crashes and/or an overall bad user experience.</p></li><li><b>Mex</b><br
/> Mex is the Matlab interface to pre-compiled external functions, typically coded in C/C++. Mex functions have access to important Matlab engine functionality, but many of these functionalities are undocumented.</li></ul><h3 id="Official">Official explanations</h3><p>Here is an important CSSM <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/20047#47988">thread snippet</a> about the official reasons, with a rare addendum from Cleve Moler – Matlab&#8217;s inventor and TMW founder &#038; chairman. It is over a decade old, yet still just as relevant today as it was back then:</p><blockquote><p> There are parts of MATLAB that we *don&#8217;t* want users to use. This includes new functionality that&#8217;s not yet completely implemented, experimental code, and code that we know will change in the future.</p><p>The functionality that we don&#8217;t document is not ready for use and *shouldn&#8217;t* be used unless its users are aware of its experimental nature. These undocumented functions/options are ones whose behavior is likely to change between versions. Future uses of it may be either invalid or produce completely different results, even between minor revisions of MATLAB. These functions are essentially “developmentally unstable;” they are not fit for use by anyone who needs a reliable development environment.</p><p>We have absolutely no plans to document these under-development areas of MATLAB until we feel they are ready for general use. Until then, we only share these features to our test audience and interested parties on CSSM. Both of these groups consist of our most dedicated MATLAB users who give excellent feedback concerning their use. But it would be irresponsible of us to document these features to the general MATLAB public when we know their use and implementation may change dramatically in the short term, making the development environment unstable. We don&#8217;t want to mislead you into using features we know not to be properly implemented.</p><p>This situation is not unique to The MathWorks. What is unique to us, however, is our honesty in letting you know these features are there. If you are in a situation where you may need to use a yet unsupported feature and we feel that it&#8217;s appropriate for us to do so, we&#8217;ll let you know that it exists. We have no intention of trying to make you look foolish; pointing out these hidden functions is meant to be helpful. We could adopt the policies taken by other software companies and simply not reveal any of these features in a public forum such as this one. However, we feel that would be counterproductive to our intentions. So instead we document the features we support, and are very willing to discuss with you unsupported and experimental features that we feel may be useful to you in your work. But we won&#8217;t document functionality whose use we aren&#8217;t sure about supporting.</p><p>We also have a responsibility to our customers to make sure that MATLAB is a stable development environment that can be used consistently from version to version. It&#8217;s difficult enough for us to advance the functionality of the code that we do support while still remaining backwards compatible with code written 10-15 years ago (check comments made in a few earlier threads). The last thing we&#8217;d want is for the general population of MATLAB users to rely on code that we don&#8217;t intend to fully support even in the near future. These undocumented features/ functions/etc fall into that category.</p><p>Naturally, this isn&#8217;t the only stance we could take. We could document everything or just not even include these features in released versions. But we&#8217;ve chosen to provide them to individuals on a case-by-case basis as we see fit; this seems to be most in line with our goals. You may not agree that this is the best choice, but it&#8217;s the one we&#8217;ve currently taken. Naturally, this could change in the future.<br
/> …<br
/> &#8211; Nabeel</p><p> &nbsp;<br
/> It&#8217;s not so much the time to do complete documention. Rather, it&#8217;s the committment to continued support of a particular feature. We have things in MATLAB that are experimental, unfinished or incomplete. They may be good ideas, but they may not. If we fully advertise and document them, then we feel strongly obligated to support them, and to keep them in MATLAB forever.<br
/> &#8211; Cleve Moler</p></blockquote><p>&nbsp;<br
/> A not unsimilar explanation was provided by Doug Hull in a <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/videos/2008/05/07/matlab-basics-adding-a-table-to-a-gui/#comment-967">comment</a> on one of Matlab&#8217;s official blogs:</p><blockquote><p> UITable was undocumented, but available, for some releases in the past.</p><p>Some features like this are available, but undocumented, for various reasons. Sometimes they are there so that we can use them in making tools for MATLAB. These features are undocumented because we need the flexibility to change the interface for a while. If you find undocumented features, you need to be aware that the interface can change or be removed in future releases.<br
/> -Doug</p></blockquote><hr
/> <b><u>Note</u></b>: For all the people who have noted about <a
target="_blank" href="http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/"><i><b>cprintf</b></i></a>&#8216;s issues with R2011b, a new version was uploaded on Monday (November 28), that fixes those problems, as well as the issue of the space (on R2011b only). In R2011b, colored text can now be placed immediately adjacent to each other, without the technical need for an intervening space character. Unfortunately, this relies on a fix that MathWorks made to the Command Window in R2011b, and which is not available in R2011a and earlier. <i><b>cprintf</b></i> automatically checks the Matlab release version and adds a space if necessary.</p><p>While I was at it, I also updated the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i></a> utility (a few bug fixes).</p><hr
/><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/' rel='bookmark' title='Types of undocumented Matlab aspects'>Types of undocumented Matlab aspects</a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li><li><a
href='http://undocumentedmatlab.com/blog/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><li><a
href='http://undocumentedmatlab.com/blog/more-undocumented-timing-features/' rel='bookmark' title='More undocumented timing features'>More undocumented timing features</a> <small>There are several undocumented ways in Matlab to get CPU and clock data...</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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/reasons-for-undocumented-matlab-aspects/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Types of undocumented Matlab aspects</title><link>http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/</link> <comments>http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/#comments</comments> <pubDate>Thu, 24 Nov 2011 18:00:36 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented feature]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JMI]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[Undocumented property]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2534</guid> <description><![CDATA[This article lists the different types of undocumented/unsupported/hidden aspects in Matlab<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/reasons-for-undocumented-matlab-aspects/' rel='bookmark' title='Reasons for undocumented Matlab aspects'>Reasons for undocumented Matlab aspects</a> <small>There are many reasons for the numerous undocumented aspects in Matlab - this article explains them....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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/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></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/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/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></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>Matlab-Java book</title><link>http://undocumentedmatlab.com/blog/matlab-java-book/</link> <comments>http://undocumentedmatlab.com/blog/matlab-java-book/#comments</comments> <pubDate>Thu, 17 Nov 2011 16:14:15 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Java]]></category> <category><![CDATA[Book]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2577</guid> <description><![CDATA[Undocumented Secrets of Matlab-Java Programming (ISBN 9781439869031) is a book dedicated to the integration of Matlab and Java.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/matlab-java-book/' rel='bookmark' title='Matlab-Java book'>Matlab-Java book</a> <small>Quick links: &nbsp;&nbsp;&nbsp; Table of Contents &nbsp;&nbsp;&nbsp; Book organization &nbsp;&nbsp;&nbsp; FAQ &nbsp;&nbsp;&nbsp; About the author &nbsp;&nbsp;&nbsp; Errata list For a variety of reasons, the Matlab-Java interface was never fully documented....</small></li><li><a
href='http://undocumentedmatlab.com/blog/ideas-for-a-new-book/' rel='bookmark' title='Ideas for a new book'>Ideas for a new book</a> <small>With my Matlab-Java book being published, reader feedback is requested about the next book project. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/converting-java-vectors-to-matlab-arrays/' rel='bookmark' title='Converting Java vectors to Matlab arrays'>Converting Java vectors to Matlab arrays</a> <small>Converting Java vectors to Matlab arrays is pretty simple - this article explains how....</small></li><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><span
class="alignright"><a
target="_blank" rel="nofollow" href="http://www.crcpress.com/product/isbn/9781439869031#post-img"><img
title="Undocumented Secrets of Matlab-Java Programming book" src="http://undocumentedmatlab.com/book/book-front-cover-CRC-3D-320x453.jpg" alt="Undocumented Secrets of Matlab-Java Programming book" width="320" height="453"/></a><br
/> <a
target="_blank" rel="nofollow" href="http://www.crcpress.com/product/isbn/9781439869031#post-banner"><img
title="CRC promo code" alt="CRC promo code" src="http://undocumentedmatlab.com/book/CRChoriz260x69_MZK07.png" class="aligncenter" style="margin: 20px 0pt 0pt 30px;" width="260" height="69"/></a><br
/> </span><center>Quick links: &nbsp;&nbsp;&nbsp; <a
href="http://undocumentedmatlab.com/matlab-java-book/#TOC">Table of Contents</a> &nbsp;&nbsp;&nbsp; <a
href="http://undocumentedmatlab.com/matlab-java-book/#Organization">Book organization</a> &nbsp;&nbsp;&nbsp; <a
href="http://undocumentedmatlab.com/matlab-java-book/#FAQ">FAQ</a> &nbsp;&nbsp;&nbsp; <a
href="http://undocumentedmatlab.com/matlab-java-book/#Author">About the author</a> &nbsp;&nbsp;&nbsp; <a
href="http://undocumentedmatlab.com/matlab-java-book/#Errata">Errata list</a></center></p><p>I am extremely pleased to announce that after five years of research and hard work, my book on <b>Undocumented Secrets of Matlab-Java Programming</b> is finally published.</p><p>For a variety of reasons, the Matlab-Java interface was never fully documented. This is really quite unfortunate: Java is one of the most widely used programming languages, having many times the number of programmers and programming resources as Matlab. Also unfortunate is the popular claim that while Matlab is a fine programming platform for prototyping, it is not suitable for real-world, modern-looking applications.</p><p><i><b>Undocumented Secrets of Matlab-Java Programming</b></i> (<a
target="_blank" rel="nofollow" href="http://www.crcpress.com/product/isbn/9781439869031#post-isbn">CRC Press, ISBN 9781439869031</a>) aims to correct this.</p><p>This book shows how using Java can significantly improve Matlab program appearance and functionality. This can be done easily and even <i>without any prior Java knowledge</i>.</p><p>Readers are led step-by-step from simple to complex customizations. Within the book&#8217;s 700 pages, thousands of code snippets, hundreds of screenshots and ~1500 online references are provided to enable the utilization of this book as both a sequential tutorial and as a random-access reference suited for immediate use.</p><p>This book demonstrates how:</p><ul><li>The Matlab programming environment relies on Java for numerous tasks, including networking, data-processing algorithms and graphical user-interface (GUI)</li><li>We can use Matlab for easy access to external Java functionality, either third-party or user-created</li><li>Using Java, we can extensively customize the Matlab environment and application GUI, enabling the creation of visually appealing and usable applications</li></ul><p><b>No prior Java knowledge is required</b>. All code snippets and examples are self-contained and can generally be used as-is. Advanced Java concepts are sometimes used, but understanding them is not required to run the code. Java-savvy readers will find it easy to tailor code samples for their particular needs; for Java newcomers, an introduction to Java and numerous online references are provided.</p><p><a
href="http://undocumentedmatlab.com/matlab-java-book/">More info about the book</a>, including detailed Table-of-Contents, book structure and FAQ, can be found on <a
href="http://undocumentedmatlab.com/matlab-java-book/">the book&#8217;s webpage</a>.</p><p><center><span
style="background-color:#E7E7E7;"><b><a
target="_blank" rel="nofollow" href="http://www.crcpress.com/product/isbn/9781439869031#post-cta">Get your book copy now</a>!</b><br
/> Use promo code <span
style="background-color: rgb(255, 255, 0);"><b>MZK07</b></span> for a 20% discount and free worldwide shipping.</span></center></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/matlab-java-book/' rel='bookmark' title='Matlab-Java book'>Matlab-Java book</a> <small>Quick links: &nbsp;&nbsp;&nbsp; Table of Contents &nbsp;&nbsp;&nbsp; Book organization &nbsp;&nbsp;&nbsp; FAQ &nbsp;&nbsp;&nbsp; About the author &nbsp;&nbsp;&nbsp; Errata list For a variety of reasons, the Matlab-Java interface was never fully documented....</small></li><li><a
href='http://undocumentedmatlab.com/blog/ideas-for-a-new-book/' rel='bookmark' title='Ideas for a new book'>Ideas for a new book</a> <small>With my Matlab-Java book being published, reader feedback is requested about the next book project. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/converting-java-vectors-to-matlab-arrays/' rel='bookmark' title='Converting Java vectors to Matlab arrays'>Converting Java vectors to Matlab arrays</a> <small>Converting Java vectors to Matlab arrays is pretty simple - this article explains how....</small></li><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-java-book/feed/</wfw:commentRss> <slash:comments>14</slash:comments> </item> <item><title>Uitable cell colors</title><link>http://undocumentedmatlab.com/blog/uitable-cell-colors/</link> <comments>http://undocumentedmatlab.com/blog/uitable-cell-colors/#comments</comments> <pubDate>Thu, 27 Oct 2011 01:51:53 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[JIDE]]></category> <category><![CDATA[uitable]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2476</guid> <description><![CDATA[A few Java-based customizations can transform a plain-looking data table into a lively colored one.<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-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/uitab-colors-icons-images/' rel='bookmark' title='Uitab colors, icons and images'>Uitab colors, icons and images</a> <small>Matlab's semi-documented tab panels can be customized using some undocumented hacks...</small></li><li><a
href='http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/' rel='bookmark' title='Changing Matlab&#8217;s Command Window colors'>Changing Matlab&#8217;s Command Window colors</a> <small>Matlab's Command Window foreground and background colors can be modified programmatically, using some of Matlab's undocumented internal Java classes. Here's how....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A client recently asked me to develop an application that will help discover technical indicators for trading on the stock market. The application was very data-intensive and the analysis naturally required visual presentations of number-crunching results in a readable manner. One of the requirements was to present numeric results in a data table, so we naturally use <i><b>uitable</b></i> for this.</p><p>Today I will show how using some not-so-complex Java we can transform the standard Matlab <i><b>uitable</b></i> into something much more useful.</p><h3 id="basic">First pass &#8211; basic data table</h3><p>We start by displaying the data in a simple <i><b>uitable</b></i>. The essence of the relevant code snippet was something like this:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">headers = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'Periods'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Any period&lt;br /&gt;returns&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Avg return&lt;br /&gt;signal&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Avg&lt;br /&gt;gain&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Avg&lt;br /&gt;draw&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Gain/draw&lt;br /&gt;ratio&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Max&lt;br /&gt;gain&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Max&lt;br /&gt;draw&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Random&lt;br /&gt;% pos&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'&lt;html&gt;&lt;center&gt;Signal&lt;br /&gt;% pos&lt;/center&gt;&lt;/html&gt;'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'Payout'</span>, <span style="color:#A020F0;">'% p-value'</span><span style="color: #080;">&#125;</span>;
hTable = uitable<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Data'</span>,data, <span style="color:#A020F0;">'ColumnEditable'</span>,<span style="color: #0000FF;">false</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'ColumnName'</span>,headers, <span style="color:#A020F0;">'RowName'</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'ColumnFormat'</span>,<span style="color: #080;">&#91;</span><span style="color:#A020F0;">'numeric'</span>,<span style="color: #0000FF;">repmat</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#123;</span><span style="color:#A020F0;">'bank'</span><span style="color: #080;">&#125;</span>,<span style="color: #33f;">1</span>,<span style="color: #33f;">11</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'ColumnWidth'</span>,<span style="color:#A020F0;">'auto'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'Units'</span>,<span style="color:#A020F0;">'norm'</span>, <span style="color:#A020F0;">'Position'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>,.75,<span style="color: #33f;">1</span>,.25<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 606px"><img
alt="Plain data table - so boooooring..." src="http://UndocumentedMatlab.com/images/uitable_plain1.png" title="Plain data table - so boooooring..." width="596" /><p
class="wp-caption-text">Plain data table - so boooooring...</p></div></center></p><p>We can see from this simple example how I have used HTML to format the header labels into two rows, to enable compact columns. I have already <a
target="_blank" href="http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/">described</a> using HTML formatting in Matlab controls in several articles on this website. I have not done this here, but you can easily use HTML formatting for other effect such as superscript (&lt;sup&gt;), subscript (&lt;sub&gt;), bold (&lt;b&gt;), italic (&lt;i&gt;), font sizes and colors (&lt;font&gt;) and other standard HTML effects.</p><p>Even with the multi-line headers, the default column width appears to be too wide. This is apparently an internal Matlab bug, not taking HTML into consideration. This causes only part of the information to be displayed on screen, and requires the user to either scroll right and left, or to manually resize the columns.</p><h3 id="auto-resizing">Second pass &#8211; auto-resizing that actually works&#8230;</h3><p>To solve the auto-resizing issue, we resort to a bit of Java magic powder. We start by using the <a
target="_blank" href="http://UndocumentedMatlab.com/blog/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i> utility</a> to get the table&#8217;s underlying Java reference handle. This is the containing scrollpane, and we are interested in the actual data table inside. We then use the <i>setAutoResizeMode</i>, as <a
target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/javax/swing/JTable.html#setAutoResizeMode%28int%29">described</a> in the official Java documentation.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jScroll = findjobj<span style="color: #080;">&#40;</span>hTable<span style="color: #080;">&#41;</span>;
jTable = jScroll.<span style="">getViewport</span>.<span style="">getView</span>;
jTable.<span style="">setAutoResizeMode</span><span style="color: #080;">&#40;</span>jTable.<span style="">AUTO_RESIZE_SUBSEQUENT_COLUMNS</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 606px"><img
alt="Auto-resized columns that actually work" src="http://UndocumentedMatlab.com/images/uitable_plain2.png" title="Auto-resized columns that actually work" width="596" /><p
class="wp-caption-text">Auto-resized columns that actually work</p></div></center></p><h3 id="colors">Third pass &#8211; adding colors</h3><p>There are still quite a few other customizations needed here: enable <a
target="_blank" href="http://undocumentedmatlab.com/blog/uitable-sorting/">sorting</a>; remove the border outline; set a white background; set row (rather than cell) selection and several other fixes that may seem trivial by themselves but together giver a much more stylish look to the table&#8217;s look and feel. I&#8217;ll skip these for now (interested readers can read all about them, and more, in my detailed <a
target="_blank" href="http://undocumentedmatlab.com/blog/uitable-customization-report/"><i><b>uitable</b></i> customization report</a>).</p><p>One of the more important customizations is to add colors depending on the data. In my client&#8217;s case, there were three requirements:</p><ul><li>data that could be positive or negative should be colored in <b><font
color="green">green</font></b> or <b><font
color="red">red</font></b> foreground color respectively</li><li>large payouts (abs &gt; 2) should be colored in <b><font
color="blue">blue</font></b></li><li>data rows that have statistical significance (%p-value &lt; 5) should have a <span
style="background-color:#ffff00;">yellow background highlight</span></li></ul><p>While we could easily use HTML or CSS formatting to do this, this would be bad for performance in a large data table, may cause some issues when editing table cells or sorting columns. I chose to use the alternative method of <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#renderer">cell renderers</a>.</p><p><code>ColoredFieldCellRenderer</code> is a simple table cell renderer that enables setting cell-specific foreground/background colors and tooltip messages (it also has a few other goodies like smart text alignment etc.). This requires some Java knowledge to program, but in this case you can simply <a
href="http://UndocumentedMatlab.com/files/ColoredFieldCellRenderer.zip">download</a> the ColoredFieldCellRenderer.zip file and use it, even if you don&#8217;t know Java. The source code is included in the zip file, for anyone who is interested.</p><p>After using <i><b>javaaddpath</b></i> to add the zip file to the dynamic Java classpath (you can add it to the static <i>classpath.txt</i> file instead), the contained Java class file is available for use in Matlab. We configure it according to our data and then assign it to all our table&#8217;s columns.</p><p>Java savvy readers might complain that the data-processing should perhaps be done in the renderer class rather than in Matlab. I have kept it in Matlab because this would enable very easy modification of the highlighting algorithm, without any need to modify the generic Java renderer class.</p><p>Unfortunately, in the new <i><b>uitable</b></i> design (the version available since R2008a), JIDE and Matlab have apparently broken the <a
target="_blank" rel="nofollow" href="http://java.sun.com/products/jfc/tsc/articles/architecture">standard MVC approach</a> by using a table model that not only controls the data but also sets the table&#8217;s appearance (row-striping background colors, for example), and disregards column cell-renderers. In order for our custom cell renderer to have any effect, we must therefore replace Matlab&#8217;s standard <code>DefaultUIStyleTableModel</code> with a simple <code>DefaultTableModel</code>. This in itself is not enough – we also need to ensure that the <i><b>uitable</b></i> has an empty <strong>ColumnFormat</strong> property, because if it is non-empty then it overrides our cell-renderer.</p><p>In the following code, note that Java row and column indices start at 0, not at 1 like in Matlab. We need to be careful about indices when programming java in Matlab.</p><p>The rest of the code should be pretty much self explanatory (again &#8211; more details can be found in the above-mentioned report):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Initialize our custom cell renderer class object</span>
javaaddpath<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'ColoredFieldCellRenderer.zip'</span><span style="color: #080;">&#41;</span>;
cr = ColoredFieldCellRenderer<span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Color</span>.<span style="">white</span><span style="color: #080;">&#41;</span>;
cr.<span style="">setDisabled</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% to bg-color the entire column</span>
&nbsp;
<span style="color: #228B22;">% Set specific cell colors (background and/or foreground)</span>
<span style="color: #0000FF;">for</span> rowIdx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">size</span><span style="color: #080;">&#40;</span>data,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #228B22;">% Red/greed foreground color for the numeric data</span>
  <span style="color: #0000FF;">for</span> colIdx = <span style="color: #33f;">2</span> <span style="color: #F0F;">:</span> <span style="color: #33f;">8</span>
    <span style="color: #0000FF;">if</span> data<span style="color: #080;">&#40;</span>rowIdx,colIdx<span style="color: #080;">&#41;</span> &lt; <span style="color: #33f;">0</span>
      cr.<span style="">setCellFgColor</span><span style="color: #080;">&#40;</span>rowIdx-<span style="color: #33f;">1</span>,colIdx-<span style="color: #33f;">1</span>,java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;    <span style="color: #228B22;">% red</span>
    <span style="color: #0000FF;">elseif</span> data<span style="color: #080;">&#40;</span>rowIdx,colIdx<span style="color: #080;">&#41;</span> &gt; <span style="color: #33f;">0</span>
      cr.<span style="">setCellFgColor</span><span style="color: #080;">&#40;</span>rowIdx-<span style="color: #33f;">1</span>,colIdx-<span style="color: #33f;">1</span>,java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0.5</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% green</span>
    <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% Yellow background for significant rows based on p-value</span>
  <span style="color: #0000FF;">if</span> data<span style="color: #080;">&#40;</span>rowIdx,<span style="color: #33f;">12</span><span style="color: #080;">&#41;</span> &lt; = <span style="color: #33f;">5</span> <span style="color: #F0F;">&amp;&amp;</span> data<span style="color: #080;">&#40;</span>rowIdx,<span style="color: #33f;">11</span><span style="color: #080;">&#41;</span>~=<span style="color: #33f;">0</span>
    <span style="color: #0000FF;">for</span> colIdx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>headers<span style="color: #080;">&#41;</span>
      cr.<span style="">setCellBgColor</span><span style="color: #080;">&#40;</span>rowIdx-<span style="color: #33f;">1</span>,colIdx-<span style="color: #33f;">1</span>,java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">1</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;    <span style="color: #228B22;">% yellow</span>
    <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% Bold blue foreground for significant payouts</span>
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">abs</span><span style="color: #080;">&#40;</span>data<span style="color: #080;">&#40;</span>rowIdx,<span style="color: #33f;">11</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> &gt;= <span style="color: #33f;">2</span>
    cr.<span style="">setCellFgColor</span><span style="color: #080;">&#40;</span>rowIdx-<span style="color: #33f;">1</span>,<span style="color: #33f;">10</span>,java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;    <span style="color: #228B22;">% blue</span>
&nbsp;
    <span style="color: #228B22;">% Note: the following could easily be done in the renderer, just like the colors</span>
    boldPayoutStr = <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;&lt;b&gt;'</span> <span style="color: #0000FF;">num2str</span><span style="color: #080;">&#40;</span>data<span style="color: #080;">&#40;</span>rowIdx,<span style="color: #33f;">11</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'%.2f'</span><span style="color: #080;">&#41;</span> <span style="color:#A020F0;">'&lt;/b&gt;&lt;/html&gt;'</span><span style="color: #080;">&#93;</span>;
    <span style="color: #228B22;">%jTable.setValueAt(boldPayoutStr,rowIdx-1,10);  % this is no good: overridden when table model is replaced below...</span>
    dataCells<span style="color: #080;">&#123;</span>rowIdx,<span style="color: #33f;">11</span><span style="color: #080;">&#125;</span> = boldPayoutStr;
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Replace Matlab's table model with something more renderer-friendly...</span>
jTable.<span style="">setModel</span><span style="color: #080;">&#40;</span>javax.<span style="">swing</span>.<span style="">table</span>.<span style="">DefaultTableModel</span><span style="color: #080;">&#40;</span>dataCells,headers<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hTable,<span style="color:#A020F0;">'ColumnFormat'</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Finally assign the renderer object to all the table columns</span>
<span style="color: #0000FF;">for</span> colIdx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>headers<span style="color: #080;">&#41;</span>
  jTable.<span style="">getColumnModel</span>.<span style="">getColumn</span><span style="color: #080;">&#40;</span>colIdx-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>.<span style="">setCellRenderer</span><span style="color: #080;">&#40;</span>cr<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 600px"><img
alt="Finally something lively!" src="http://UndocumentedMatlab.com/images/uitable_colored.png" title="Finally something lively!" width="590" /><p
class="wp-caption-text">Finally something lively!</p></div></center></p><p>This may not take a beauty contest prize, but you must admit that it now looks more useful than the original table at the top of this article.</p><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-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/uitab-colors-icons-images/' rel='bookmark' title='Uitab colors, icons and images'>Uitab colors, icons and images</a> <small>Matlab's semi-documented tab panels can be customized using some undocumented hacks...</small></li><li><a
href='http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/' rel='bookmark' title='Changing Matlab&#8217;s Command Window colors'>Changing Matlab&#8217;s Command Window colors</a> <small>Matlab's Command Window foreground and background colors can be modified programmatically, using some of Matlab's undocumented internal Java classes. Here's how....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/uitable-cell-colors/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Tri-state checkbox</title><link>http://undocumentedmatlab.com/blog/tri-state-checkbox/</link> <comments>http://undocumentedmatlab.com/blog/tri-state-checkbox/#comments</comments> <pubDate>Wed, 19 Oct 2011 14:04:20 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JIDE]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2467</guid> <description><![CDATA[Matlab checkboxes can easily be made to support tri-state functionality.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/recovering-previous-editor-state/' rel='bookmark' title='Recovering previous editor state'>Recovering previous editor state</a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/determining-axes-zoom-state/' rel='bookmark' title='Determining axes zoom state'>Determining axes zoom state</a> <small>The information of whether or not an axes is zoomed or panned can easily be inferred from an internal undocumented object....</small></li><li><a
href='http://undocumentedmatlab.com/blog/jide-property-grids/' rel='bookmark' title='JIDE Property Grids'>JIDE Property Grids</a> <small>The JIDE components pre-bundled in Matlab enable creating user-customized property grid tables...</small></li><li><a
href='http://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy/' rel='bookmark' title='FindJObj GUI &#8211; display container hierarchy'>FindJObj GUI &#8211; display container hierarchy</a> <small>The FindJObj utility can be used to present a GUI that displays a Matlab container's internal Java components, properties and callbacks....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>When presenting information visually in graphical user interfaces (GUIs), we often need to present and enable interaction with state data (such as On/Off). In most cases, we would naturally use a checkbox to present the information and enable interaction. But What can we do if the data has three possible states. For example, Yes/No/Maybe, or: Full/Empty/Partial, or: Up/Down/Undetermined ?</p><p>Until today, Matlab GUIs had to resort to using drop-down (aka combo-box or popup-menu) or radio-button controls to present such information. However, would it not be nicer if we could still use a checkbox? Outside Matlab, such a control is known as a tri-state checkbox and many modern GUI frameworks support it. Well, surprise surprise, so does Matlab (although you would never guess it from the documentation).</p><h3 id="CheckBoxTree">CheckBoxTree</h3><p>Last year, I have already <a
target="_blank" href="http://undocumentedmatlab.com/blog/customizing-uitree-nodes-2/#Built-in-classes">described</a> a built-in Matlab tree control whose nodes have tri-state checkboxes:</p><p><center><div
class="wp-caption aligncenter" style="width: 321px"><img
alt="a regular MJTree (left) and a CheckBoxTree (right)" src="http://UndocumentedMatlab.com/images/CheckBoxTree.png" title="a regular MJTree (left) and a CheckBoxTree (right)" width="311" height="218" /><p
class="wp-caption-text">a regular MJTree (left) and a CheckBoxTree (right)</p></div></center></p><p>Today I will show how we can use these checkboxes as independent GUI controls.</p><h3 id="uicontrol">Modifying the standard Matlab checkbox uicontrol</h3><p>In order to modify the standard Matlab checkbox <i><b>uicontrol</b></i>, we need to first get its underlying Java component reference. This is done using the <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i> utility</a>. We then update its UI wrapper to be the same as the <code>CheckBoxTree</code>&#8216;s checkbox control.</p><p>To programmatically set a mixed state we update the &#8216;selectionState&#8217; client property to <code>SelectionState.MIXED</code> (<code>SelectionState</code> also has the <code>SELECTED</code> and <code>NOT_SELECTED</code> values).</p><p>Here is an end-to-end example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hCB = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Style'</span>,<span style="color:#A020F0;">'checkbox'</span>, <span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span>;
jCB = findjobj<span style="color: #080;">&#40;</span>hCB<span style="color: #080;">&#41;</span>;
jCB.<span style="">setUI</span><span style="color: #080;">&#40;</span>com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">checkboxtree</span>.<span style="">TriStateButtonUI</span><span style="color: #080;">&#40;</span>jCB.<span style="">getUI</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Update the checkbox state to MIXED</span>
newState = com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">checkboxtree</span>.<span style="">SelectionState</span>.<span style="">MIXED</span>;
jCB.<span style="">putClientProperty</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'selectionState'</span>, newState<span style="color: #080;">&#41;</span>;
jCB.<span style="">repaint</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 299px"><img
alt="Matlab checkboxes displaying mixed states" src="http://UndocumentedMatlab.com/images/CheckBox_TriState.png" title="Matlab checkboxes displaying mixed states" width="289" /><p
class="wp-caption-text">Matlab checkboxes displaying mixed states</p></div></center></p><h3 id="independent">Displaying as an independent Java control</h3><p>Instead of retrofitting a standard Matlab <i><b>uicontrol</b></i> as described above, we can directly use the standard <code>javax.swing.JCheckBox</code> which does not normally support tri-state (it only has two states), displaying it in our GUI with the built-in <a
target="_blank" href="http://undocumentedmatlab.com/blog/javacomponent/"><i><b>javacomponent</b></i> function</a>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display the checkbox (UNSELECTED state at first)</span>
jCB = javax.<span style="">swing</span>.<span style="">JCheckBox</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'JCheckBox - mixed'</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
javacomponent<span style="color: #080;">&#40;</span>jCB, <span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">70</span>,<span style="color: #33f;">150</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Update the checkbox state to MIXED</span>
import com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">checkboxtree</span>.*
jCB.<span style="">setUI</span><span style="color: #080;">&#40;</span>TriStateButtonUI<span style="color: #080;">&#40;</span>jCB.<span style="">getUI</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
jCB.<span style="">putClientProperty</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'selectionState'</span>, SelectionState.<span style="">MIXED</span><span style="color: #080;">&#41;</span>;
jCB.<span style="">repaint</span>;</pre></div></div><p>Note that instead of using <code>javax.swing.JCheckBox</code>, we could use the internal Matlab class <code>com.mathworks.mwswing.MJCheckBox</code>, which directly extends <code>JCheckBox</code> and adds mnemonic (shortcut-key) support, but is otherwise fully compatible with <code>JCheckBox</code>. Actually, it is <code>MJCheckBox</code> which is the underlying Java component of the standard Matlab checkbox <i><b>uicontrol</b></i>.</p><h3 id="alternatives">Alternative controls</h3><p>Now that we have seen that Matlab includes built-in support (well, at least support in the technical sense, not the official customer-support sense), would you be surprised to learn that it includes similar support in other internal components as well?</p><p>The internal Matlab class <code>com.mathworks.mwt.MWCheckbox</code> directly supports a tri-state (yes/no/maybe) checkbox, without any need to update its UI, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display the checkbox (UNSELECTED state at first)</span>
jCB = com.<span style="">mathworks</span>.<span style="">mwt</span>.<span style="">MWCheckbox</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'MWCheckbox - mixed'</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
javacomponent<span style="color: #080;">&#40;</span>jCB, <span style="color: #080;">&#91;</span><span style="color: #33f;">10</span>,<span style="color: #33f;">70</span>,<span style="color: #33f;">150</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Update the checkbox state to MIXED</span>
jCB.<span style="">setMixedState</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Retrieve the current state</span>
isMixedFlag = jCB.<span style="">isMixedState</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% true/false</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 299px"><img
alt="MJCheckBox vs. MWCheckbox" src="http://UndocumentedMatlab.com/images/CheckBox_MWT_MWSwing.png" title="MJCheckBox vs. MWCheckbox" width="289" /><p
class="wp-caption-text">MJCheckBox vs. MWCheckbox</p></div></center></p><p>Note that the State property, which controls the standard selected/unselected state, is entirely independent from the MixedState property. Both State and MixedState are boolean properties, so to get the actual checkbox state we need to query both of these properties.</p><p>Another internal Matlab class that we can use is JIDE&#8217;s <code>com.jidesoft.swing.TristateCheckBox</code> (which is pre-bundled in Matlab and is fully documented <a
target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/swing/TristateCheckBox.html">here</a>).</p><p>There are many other tri-state checkbox alternatives available online (for example, <a
target="_blank" rel="nofollow" href="https://forums.oracle.com/forums/search.jspa?threadID=&#038;q=%28tri-state+OR+tristate%29+AND+checkbox&#038;objID=c285&#038;dateRange=all&#038;userID=&#038;numResults=30&#038;rankBy=10001">here</a>, <a
target="_blank" rel="nofollow" href="http://www.javaspecialists.eu/archive/Issue145.html">here</a> and <a
target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/1263323/tristate-checkboxes-in-java">here</a>). We can easily include them in our Matlab GUI with the <i><b>javacomponent</b></i> function. Using external components we can be more certain of the compatibility issues in past and future Matlab releases. On the other hand, internal Matlab classes do have the advantage of being inherently accessible on all platforms of the same Matlab release, whereas non-Matlab components must be included in our deployment package.</p><p>Do you use any tri-state controls, either Matlab or external, in your work? If so, please share your experience in a <a
href="http://UndocumentedMatlab.com/blog/tri-state-checkbox/#respond">comment below</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/recovering-previous-editor-state/' rel='bookmark' title='Recovering previous editor state'>Recovering previous editor state</a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/determining-axes-zoom-state/' rel='bookmark' title='Determining axes zoom state'>Determining axes zoom state</a> <small>The information of whether or not an axes is zoomed or panned can easily be inferred from an internal undocumented object....</small></li><li><a
href='http://undocumentedmatlab.com/blog/jide-property-grids/' rel='bookmark' title='JIDE Property Grids'>JIDE Property Grids</a> <small>The JIDE components pre-bundled in Matlab enable creating user-customized property grid tables...</small></li><li><a
href='http://undocumentedmatlab.com/blog/findjobj-gui-display-container-hierarchy/' rel='bookmark' title='FindJObj GUI &#8211; display container hierarchy'>FindJObj GUI &#8211; display container hierarchy</a> <small>The FindJObj utility can be used to present a GUI that displays a Matlab container's internal Java components, properties and callbacks....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/tri-state-checkbox/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Javacomponent background color</title><link>http://undocumentedmatlab.com/blog/javacomponent-background-color/</link> <comments>http://undocumentedmatlab.com/blog/javacomponent-background-color/#comments</comments> <pubDate>Wed, 12 Oct 2011 14:39:43 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Undocumented feature]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2459</guid> <description><![CDATA[This article explains how to align Java component background color with a Matlab color.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/common-javacomponent-problems/' rel='bookmark' title='Common javacomponent problems'>Common javacomponent problems</a> <small>The javacomponent function is very useful for placing Java components on-screen, but has a few quirks. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/javacomponent/' rel='bookmark' title='The javacomponent function'>The javacomponent function</a> <small>Matlab's built-in javacomponent function can be used to display Java components in Matlab application - this article details its usages and limitations...</small></li><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/cprintf-display-formatted-color-text-in-command-window/' rel='bookmark' title='cprintf &#8211; display formatted color text in the Command Window'>cprintf &#8211; display formatted color text in the Command Window</a> <small>cprintf is a utility that utilized undocumented Matlab desktop functionalities to display color and underline-styled formatted text in the Command Window...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>In this website, I have often shown how Matlab application functionality can be significantly enhanced by including Java components in the Matlab GUI, using the built-in semi-documented <i><b>javacomponent</b></i> function (which I <a
target="_blank" href="http://undocumentedmatlab.com/blog/javacomponent/">described here</a> last year).</p><p>Using java components in Matlab is simple and easy, but there are a few annoying catches. One of these is the fact that the default Matlab figure background color ([0.8, 0.8, 0.8]) is a different shade of gray than the default <i><b>uicontrol</b></i> background color ([0.9255, 0.9137, 0.847]). <i><b>javacomponent</b></i>s use the same background color as <i><b>uicontrol</b></i>s.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hPanel = uipanel<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'units'</span>,<span style="color:#A020F0;">'pixe'</span>, <span style="color:#A020F0;">'pos'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">30</span>,<span style="color: #33f;">30</span>,<span style="color: #33f;">200</span>,<span style="color: #33f;">100</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'title'</span>,<span style="color:#A020F0;">'Matlab uipanel'</span><span style="color: #080;">&#41;</span>;
jLabel = javax.<span style="">swing</span>.<span style="">JLabel</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Java Label'</span><span style="color: #080;">&#41;</span>;
<span style="color: #080;">&#91;</span>jhlabel,jContainer<span style="color: #080;">&#93;</span>=javacomponent<span style="color: #080;">&#40;</span>jLabel, <span style="color: #080;">&#91;</span><span style="color: #33f;">250</span>,<span style="color: #33f;">50</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">50</span><span style="color: #080;">&#93;</span>, <span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 404px"><img
alt="Javacomponents use different default background color than figures" src="http://UndocumentedMatlab.com/images/javacomponent_bgcolor.png" title="Javacomponents use different default background color than figures" width="394" /><p
class="wp-caption-text">Javacomponents use different default background color than figures</p></div></center></p><p>While Matlab users are familiar with updating Matlab colors, doing so with Java colors is a bit different. We have two basic ways to align the <i><b>javacomponent</b></i> background color with the figure&#8217;s: either change the figure&#8217;s <b>Color</b> property to the Java component&#8217;s bgcolor, or vice versa:</p><h3 id="Option1">Changing the <i><b>javacomponent</b></i>&#8216;s background color to the figure color</h3><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Fix the Java component:</span>
<span style="color: #228B22;">% Variant #1</span>
bgcolor = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>, <span style="color:#A020F0;">'Color'</span><span style="color: #080;">&#41;</span>;
jLabel.<span style="">setBackground</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span>bgcolor<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>,bgcolor<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>,bgcolor<span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Variant #2</span>
bgcolor = <span style="color: #0000FF;">num2cell</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>, <span style="color:#A020F0;">'Color'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
jLabel.<span style="">setBackground</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#40;</span>bgcolor<span style="color: #080;">&#123;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Fix the Matlab panel uicontrol</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hPanel, <span style="color:#A020F0;">'BackgroundColor'</span>, <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>,<span style="color:#A020F0;">'Color'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 404px"><img
alt="controls with fixed background colors" src="http://UndocumentedMatlab.com/images/javacomponent_bgcolor2.png" title="controls with fixed background colors" width="394" /><p
class="wp-caption-text">controls with fixed background colors</p></div></center></p><h3 id="Option2">Changing the figure&#8217;s color to the <i><b>javacomponent</b></i>&#8216;s background color</h3><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">bgcolor = jLabel.<span style="">getBackground</span>.<span style="">getComponents</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>, <span style="color:#A020F0;">'Color'</span>, bgcolor<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 404px"><img
alt="figure with modified color to match the controls" src="http://UndocumentedMatlab.com/images/javacomponent_bgcolor3.png" title="figure with modified color to match the controls" width="394" /><p
class="wp-caption-text">figure with modified color to match the controls</p></div></center></p><p>Look at the list of related posts below for other articles related to colors in Matlab.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/common-javacomponent-problems/' rel='bookmark' title='Common javacomponent problems'>Common javacomponent problems</a> <small>The javacomponent function is very useful for placing Java components on-screen, but has a few quirks. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/javacomponent/' rel='bookmark' title='The javacomponent function'>The javacomponent function</a> <small>Matlab's built-in javacomponent function can be used to display Java components in Matlab application - this article details its usages and limitations...</small></li><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/cprintf-display-formatted-color-text-in-command-window/' rel='bookmark' title='cprintf &#8211; display formatted color text in the Command Window'>cprintf &#8211; display formatted color text in the Command Window</a> <small>cprintf is a utility that utilized undocumented Matlab desktop functionalities to display color and underline-styled formatted text in the Command Window...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/javacomponent-background-color/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           tag
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-02-04 02:49:25 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/tag/java/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      2.036s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1328348964; expires=Sun, 03-Feb-2013 09:49:24 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Sat, 04 Feb 2012 09:49:25 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Sat, 04 Feb 2012 10:49:25 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               a383f630c16b75eb0ceb77aee2dd55d9
Content-Encoding:   gzip
-->
