<?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; GUI</title> <atom:link href="http://undocumentedmatlab.com/blog/category/gui/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>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>Multi-line tooltips</title><link>http://undocumentedmatlab.com/blog/multi-line-tooltips/</link> <comments>http://undocumentedmatlab.com/blog/multi-line-tooltips/#comments</comments> <pubDate>Thu, 03 Nov 2011 02:05:36 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[Tooltip]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2518</guid> <description><![CDATA[Multi-line tooltips are very easy to set up, once you know your way around a few undocumented hiccups.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li><li><a
href='http://undocumentedmatlab.com/blog/multi-column-grid-legend/' rel='bookmark' title='Multi-column (grid) legend'>Multi-column (grid) legend</a> <small>This article explains how to use undocumented axes listeners for implementing multi-column plot legends...</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>I often use tooltips in my Matlab GUIs. They are a fantastically intuitive and unobtrusive visual helper for users to understand the GUI. In this blog, I have already posted several articles about how to tweak tooltip contents (<a
target="_blank" href="http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/">here</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/">here</a>, and <a
target="_blank" href="http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/">here</a>). Today I would like to expand on one of the aspects that were already covered, namely that of multi-line tooltips.</p><h3 id="Basic">Basic multi-line tooltips</h3><p>The basic multi-line tooltip consists of a simple string that includes the newline character. for example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'this is line 1'</span> <span style="color: #33f;">10</span> <span style="color:#A020F0;">'and this is line 2'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p>Or better (platform independent):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'this is line 1\nand this is line 2'</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: 210px"><img
alt="A simple multi-line tooltip string" src="http://UndocumentedMatlab.com/images/tooltip_multi-line1.png" title="A simple multi-line tooltip string" width="120" /><p
class="wp-caption-text">A simple multi-line tooltip string</p></div></center></p><p>As can be seen, this is very simple to set up. Unfortunately, the font cannot be customized. Which leads us to:</p><h3 id="HTML">HTML-rendered multi-line tooltips</h3><p>Tooltips, like most other Matlab controls, <a
target="_blank" href="http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/">supports HTML rendering</a>. This is a natural corollary of the fact that Java Swing, on which all Matlab controls are based, <a
target="_blank" rel="nofollow" href="http://download.oracle.com/javase/tutorial/uiswing/components/html.html">automatically supports HTML</a>. All we need to do is to add &#8216;<code>&lt;HTML&gt;</code>&#8216; at the very beginning of our tooltip string, and then we can embed HTML tags within the rest of the string:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'tooltipString'</span>, <span style="color:#A020F0;">'&lt;html&gt;&lt;b&gt;line #1&lt;/b&gt;&lt;br /&gt;&lt;i&gt;&lt;font color=&quot;red&quot;&gt;line#2&lt;/font&gt;&lt;/i&gt;&lt;/html&gt;'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Multi-line HTML'ed tooltip" src="http://undocumentedmatlab.com/blog/wp-content/uploads/2009/03/html2.png" title="Multi-line HTML'ed tooltip" width="54" /><p
class="wp-caption-text">Multi-line HTML'ed tooltip</p></div></center></p><p>And a more sophisticated example, used within my <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14225-java-based-data-table">createTable utility</a> on the File Exchange:</p><p><center><div
class="wp-caption aligncenter" style="width: 607px"><img
alt="A more complex multi-line HTML-based tooltip" src="http://UndocumentedMatlab.com/images/table.png" title="A more complex multi-line HTML-based tooltip" width="597" /><p
class="wp-caption-text">A more complex multi-line HTML-based tooltip</p></div></center></p><p>Note that there is no need to close the final HTML tags in the tooltip string, although it&#8217;s not an error to do so (as I have done above).</p><h3 id="Problem">The problem with formatted multi-line tooltip</h3><p>Unfortunately, none of these two methods work when we wish to display formatted multi-line tooltips. For example, suppose that we wish to display struct data in a tooltip:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myData = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'a'</span>,<span style="color: #0000FF;">pi</span>, <span style="color:#A020F0;">'b'</span>,-<span style="color: #33f;">4</span>, <span style="color:#A020F0;">'very_long_field_name'</span>,<span style="color:#A020F0;">'short txt'</span><span style="color: #080;">&#41;</span>
myData = 
                       a<span style="color: #F0F;">:</span> <span style="color: #33f;">3.14159265358979</span>
                       b<span style="color: #F0F;">:</span> -<span style="color: #33f;">4</span>
    very_long_field_name<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'short txt'</span>
&nbsp;
&gt;&gt; myDataStr = <span style="color: #0000FF;">evalc</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'disp(myData)'</span><span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, myDataStr<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Badly-formatted multi-line tooltip" src="http://UndocumentedMatlab.com/images/tooltip_multi-line2.png" title="Badly-formatted multi-line tooltip" width="200" /><p
class="wp-caption-text">Badly-formatted multi-line tooltip</p></div></center></p><p>If we try to use HTML, the result looks even worse, because if the HTML-renderer detects a newline character embedded in the string, it simply uses the regular (non-HTML) renderer:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;'</span> myDataStr <span style="color:#A020F0;">'&lt;/html&gt;'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Failure to parse a string using HTML" src="http://UndocumentedMatlab.com/images/tooltip_multi-line3.png" title="Failure to parse a string using HTML" width="200" /><p
class="wp-caption-text">Failure to parse a string using HTML</p></div></center></p><p>Even if we fix this by replacing all line-separators with &#8216;<code>&lt;br/&gt;</code>&#8216;, we still get a badly-formatted tooltip, because HTML ignores all white spaces:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myDataStr2 = <span style="color: #0000FF;">strrep</span><span style="color: #080;">&#40;</span>myDataStr, <span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'\n'</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'&lt;br /&gt;'</span><span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;'</span> myDataStr2 <span style="color:#A020F0;">'&lt;/html&gt;'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Badly-formatted multi-line tooltip" src="http://UndocumentedMatlab.com/images/tooltip_multi-line4.png" title="Badly-formatted multi-line tooltip" width="200" /><p
class="wp-caption-text">Badly-formatted multi-line tooltip</p></div></center></p><p>(you can see that it&#8217;s HTML-formatted by the fact that the tooltip contents do not have internal margins like in the non-HTML tooltip above)</p><h3 id="Fixed-width">Fixed-width font multi-line tooltips</h3><p>We now recall the HTML <code>&lt;pre&gt;</code> tag, which tells HTML not to ignore white-spaces. In most web browsers, it also defaults to using a fixed-width font. Unfortunately, this is not the case here:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;&lt;pre&gt;'</span> myDataStr2<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 250px"><img
alt="Still not quite right..." src="http://UndocumentedMatlab.com/images/tooltip_multi-line5.png" title="Still not quite right..." width="240" /><p
class="wp-caption-text">Still not quite right...</p></div></center></p><p>Which brings us to the final customization of the day, namely explicitly forcing the tooltip to use a fixed-width font:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;&lt;pre&gt;&lt;font face=&quot;courier new&quot;&gt;'</span> myDataStr2 <span style="color:#A020F0;">'&lt;/font&gt;'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 330px"><img
alt="Finally - a well-formatted multi-line tooltip" src="http://UndocumentedMatlab.com/images/tooltip_multi-line6.png" title="Finally - a well-formatted multi-line tooltip" width="320" /><p
class="wp-caption-text">Finally - a well-formatted multi-line tooltip</p></div></center></p><p>Even more powerful customizations can be achieved using CSS rather than pure HTML. This will be discussed a future article.</html></pre><p></html></pre><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li><li><a
href='http://undocumentedmatlab.com/blog/multi-column-grid-legend/' rel='bookmark' title='Multi-column (grid) legend'>Multi-column (grid) legend</a> <small>This article explains how to use undocumented axes listeners for implementing multi-column plot legends...</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/multi-line-tooltips/feed/</wfw:commentRss> <slash:comments>0</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> <item><title>Controlling callback re-entrancy</title><link>http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy/</link> <comments>http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy/#comments</comments> <pubDate>Wed, 10 Aug 2011 18:00:57 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[COM]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Malcolm Lidierth]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2403</guid> <description><![CDATA[Callback reentrancy is a major problem for frequently-fired events. Luckily, it can easily be solved.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/continuous-slider-callback/' rel='bookmark' title='Continuous slider callback'>Continuous slider callback</a> <small>Matlab slider uicontrols do not enable a continuous-motion callback by default. This article explains how this can be achieved using undocumented features....</small></li><li><a
href='http://undocumentedmatlab.com/blog/controlling-plot-data-tips/' rel='bookmark' title='Controlling plot data-tips'>Controlling plot data-tips</a> <small>Data-tips are an extremely useful plotting tool that can easily be controlled programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/' rel='bookmark' title='Matlab-Java interface using a static control'>Matlab-Java interface using a static control</a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>I&#8217;d like to welcome guest blogger <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/authors/23816">Malcolm Lidierth</a> of King&#8217;s College London. Malcolm is well known in the Matlab-Java community for his <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/15580-using-java-swing-components-in-matlab">jcontrol utility</a>. Some months ago, I mentioned his work on another File Exchange contribution, <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/28326-mutilities">MUtilities</a> when I discussed <a
target="_blank" href="http://undocumentedmatlab.com/blog/transparent-matlab-figure-window/">transparent Matlab figure windows</a>. Today, Malcolm discusses one of his lesser-known but extremely important <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/26027-ismultiplecall">isMultipleCall utility</a>.</i></p><p>Every now and again, a very simple bit of code turns out to be more useful than the author initially imagined. Something I have repeatedly used is the <i><b>isMultipleCall</b></i> function which I <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/26027-ismultiplecall">posted</a> to MATLAB Central&#8217;s File Exchange a year or so ago.</p><p>The <i><b>isMultipleCall</b></i> function uses fully-documented pure-MATLAB to extend the control that can be achieved over callbacks.</p><p>Here was the problem: I had a modular system built in MATLAB which allowed third-party developers to add their own plugins. I wanted a mechanism to force the dismissal (&#8220;bail-out&#8221;) of a callback even when the <b>Interruptible</b> property of the parent object was set to &#8216;on&#8217;. Such callback re-entrancy issues are common for rapidly-firing events, and debugging and fixing them is usually <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/answers/5809-how-to-implement-a-speed-limit-for-gui-button-press">not easy</a>.</p><p>The callback&#8217;s dismissal code would need to be fast because it might be called many dozens of times, e.g. in a <b>WindowButtonMotion</b> callback. An obvious approach was to check the function call stack using MATLAB&#8217;s <i><b>dbstack</b></i> function. Although, at first, this seemed likely to be too slow, profiling showed it was not – taking &lt; 40&micro;sec per call &#8211; and within a <b>WindowButtonMotion</b> callback in a real GUI, I could not perceive any slowing of the code.</p><p>Here is the function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> <span style="color: #0000FF;">flag</span>=isMultipleCall<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">flag</span> = <span style="color: #0000FF;">false</span>; 
  <span style="color: #228B22;">% Get the stack</span>
  s = <span style="color: #0000FF;">dbstack</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">if</span> numel<span style="color: #080;">&#40;</span>s<span style="color: #080;">&#41;</span>&lt; =<span style="color: #33f;">2</span>
    <span style="color: #228B22;">% Stack too short for a multiple call</span>
    <span style="color: #0000FF;">return</span>
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% How many calls to the calling function are in the stack?</span>
  names = <span style="color: #080;">&#123;</span>s<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#41;</span>.<span style="">name</span><span style="color: #080;">&#125;</span>;
  TF = <span style="color: #0000FF;">strcmp</span><span style="color: #080;">&#40;</span>s<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>.<span style="">name</span>,names<span style="color: #080;">&#41;</span>;
  count = <span style="color: #0000FF;">sum</span><span style="color: #080;">&#40;</span>TF<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">if</span> count&gt;<span style="color: #33f;">1</span>
    <span style="color: #228B22;">% More than 1</span>
    <span style="color: #0000FF;">flag</span> = <span style="color: #0000FF;">true</span>; 
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span></pre></div></div><p>With <i><b>isMultipleCall</b></i> invoked from another function (see note below), <i><b>dbstack</b></i> will return a structure with a minimum of 2 elements – the first relating to <i><b>isMultipleCall</b></i> itself and the second to the calling function. So with <i><b>numel</b></i>(s) &lt;= 2, there can be no multiple calls and we can return false immediately thus saving time in doing any further testing.  For <i><b>numel</b></i>(s) &gt; 2 we simply check to see whether the calling functions referenced in s(2) appears anywhere else on the stack. If it does, then we return true; otherwise false.</p><p>Then, in our callback code we simply use:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">if</span> isMultipleCall<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;  <span style="color: #0000FF;">return</span>;  <span style="color: #0000FF;">end</span></pre></div></div><p>If this line is placed first in the callback function code, it essentially mimics the behavior that you might expect after setting the <b>Interruptible</b> property of the event firing object to &#8216;off&#8217;. Adding a <i><b>drawnow()</b></i> at the end of the callback will ensure that any waiting callbacks in the queue are dismissed:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> MyCallback<span style="color: #080;">&#40;</span>hObj, EventData<span style="color: #080;">&#41;</span>
  <span style="color: #228B22;">% Quick bail-out if callback code is called before another has ended</span>
  <span style="color: #0000FF;">if</span> isMultipleCall<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;  <span style="color: #0000FF;">return</span>;  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #F0F;">...</span>  <span style="color: #228B22;">% do some actual callback work here</span>
  <span style="color: #0000FF;">drawnow</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>There are several ways in which <i><b>isMultipleCall</b></i> can extend the standard MALAB functionality. First, by moving <i><b>isMultipleCall</b></i> reference from the first line of the callback we can create both an interruptible and an uninteruptible code block, e.g.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> MyCallback<span style="color: #080;">&#40;</span>hObj, EventData<span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #228B22;">%Code Block 1</span>
  <span style="color: #F0F;">...</span>
&nbsp;
  <span style="color: #0000FF;">if</span> isMultipleCall<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;  <span style="color: #0000FF;">return</span>;  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">%Code Block 2</span>
  <span style="color: #F0F;">...</span>
&nbsp;
  <span style="color: #0000FF;">drawnow</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>Second, as <i><b>isMultipleCall</b></i> controls the callbacks  &#8211; not the objects that trigger them &#8211; we can individually control the callbacks of objects which fire multiple events. That is particularly useful with Java components, which gives a third extension – <i><b>isMultipleCall</b></i> can be used in any function: not just the callbacks of standard MATLAB components, but also of Java or COM components.<br
/>  <br
/> Finally, as the callback, not the object is being controlled, we can control a callback that may be shared between multiple objects e.g. a menu component and a toolbar button.</p><p>Not bad for 13 lines of code.</p><p>Note: <i><b>isMultipleCall</b></i> must be called from a function, not from a string in the callback property.</p><p>Do you have any other favorite mechanism for controlling callback re-entrancy? If so, please <a
href="http://UndocumentedMatlab.com/blog/controlling-callback-re-entrancy/#respond">post a comment</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/continuous-slider-callback/' rel='bookmark' title='Continuous slider callback'>Continuous slider callback</a> <small>Matlab slider uicontrols do not enable a continuous-motion callback by default. This article explains how this can be achieved using undocumented features....</small></li><li><a
href='http://undocumentedmatlab.com/blog/controlling-plot-data-tips/' rel='bookmark' title='Controlling plot data-tips'>Controlling plot data-tips</a> <small>Data-tips are an extremely useful plotting tool that can easily be controlled programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/' rel='bookmark' title='Matlab-Java interface using a static control'>Matlab-Java interface using a static control</a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/uicontrol-callbacks/' rel='bookmark' title='Uicontrol callbacks'>Uicontrol callbacks</a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy/feed/</wfw:commentRss> <slash:comments>11</slash:comments> </item> <item><title>Uitable customization report</title><link>http://undocumentedmatlab.com/blog/uitable-customization-report/</link> <comments>http://undocumentedmatlab.com/blog/uitable-customization-report/#comments</comments> <pubDate>Wed, 03 Aug 2011 18:00:25 +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[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JIDE]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[uitable]]></category> <category><![CDATA[uitable report]]></category> <category><![CDATA[uitools]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2396</guid> <description><![CDATA[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 contents of this report. This is a reasonable request, and [...]<pre> </pre>Related posts:<ol><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/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/guide-customization/' rel='bookmark' title='GUIDE customization'>GUIDE customization</a> <small>Matlab's GUI Design Editor (GUIDE) has several interesting undocumented features. This post describes how to customize GUIDE rulers....</small></li><li><a
href='http://undocumentedmatlab.com/blog/button-customization/' rel='bookmark' title='Button customization'>Button customization</a> <small>Matlab's button uicontrols (pushbutton and togglebutton) are basically wrappers for a Java Swing JButton object. This post describes how the buttons can be customized using this Java object in ways...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>In last week&#8217;s report about <a
target="_blank" href="http://undocumentedmatlab.com/blog/uitable-sorting/"><i><b>uitable</b></i> sorting</a>, I offered a report that I have written which covers <i><b>uitable</b></i> customization in detail. Several people have asked for more details about the contents of this report. This is a reasonable request, and so in today&#8217;s post I will discuss in a bit more detail the highlights of what can be achieved to customize Matlab <i><b>uitable</b></i>s. For the fine details, well, <a
target="_blank" rel="nofollow" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&#038;business=ymasoftware@gmail.com&#038;currency_code=USD&#038;amount=35&#038;return=&#038;item_name=Matlab-uitable-report">get the report</a>&#8230;</p><h3 id="Introduction">1. Introduction</h3><p>Here I discuss the evolution of the <i><b>uitable</b></i> control over the past decade, from its initially semi-documented status to today. I explain the similarities and differences between the control&#8217;s versions and explain how they can both be accessed today.</p><p>I also provide references to online resources for both Matlab&#8217;s <i><b>uitable</b></i>&#8216;s underlying Java components, as well as multiple alternatives using different technologies, that have been used and reported over the years.</p><h3 id="Customization">2. Customizing uitable</h3><p>In this section I explore the sub-component hierarchy of the <i><b>uitable</b></i> control (both new and old). I show how the scrollbar sub-components can be accessed (this will be used in section 4 below), as well as the table header and data grid.<br
/><center><div
class="wp-caption aligncenter" style="width: 322px"><img
alt="annotated uitable sub-components" src="http://undocumentedmatlab.com/images/uitable_new2b.png" title="annotated uitable sub-components" width="312" /><p
class="wp-caption-text">annotated <i><b>uitable</b></i> sub-components</p></div></center></p><p>I explain how individual cells can be modified without requiring the entire data set to be updated. This is very important in large data sets, to prevent flicker and improve performance.</p><p>I show how HTML can be used to format data cell contents (even images) and tooltips:<br
/><center><div
class="wp-caption aligncenter" style="width: 259px"><img
alt="uitable with HTML cell contents and tooltip" src="http://undocumentedmatlab.com/images/uitable2.png" title="uitable with HTML cell contents and tooltip" width="249" /><p
class="wp-caption-text"><i><b>uitable</b></i> with HTML cell contents and tooltip</p></div></center></p><p>I then explain the role of the cell-renderer in the visual appearance of the cell, and of cell-editors in the way that cells interact with the user for data modification. I explain such customizations from the simple (setting a column&#8217;s background color) to the complex (cell-specific tooltips and colors; color-selection cell-editor):<br
/><center><div
class="wp-caption aligncenter" style="width: 590px"><img
alt="uitable with a non-standard cell-renderer" src="http://undocumentedmatlab.com/images/uitable5b.png" title="uitable with a non-standard cell-renderer" width="288" /><img
alt="uitable with a non-standard cell-editor" src="http://undocumentedmatlab.com/images/uitable_lookup.png" title="uitable with a non-standard cell-editor" width="280" /><p
class="wp-caption-text"><i><b>uitable</b></i> with a non-standard cell-renderer (left) and cell-editor (right)</p></div><br
/><div
class="wp-caption aligncenter" style="width: 318px"><img
alt="uitable with custom CellRenderer and CellEditor" src="http://UndocumentedMatlab.com/images/ColorCellEditor_Renderer.png" title="uitable with custom CellRenderer and CellEditor" width="308" /><p
class="wp-caption-text"><i><b>uitable</b></i> with custom CellRenderer and CellEditor</p></div></center></p><h3 id="Callbacks">3. Table callbacks</h3><p>This section presents the different callback properties that are settable in the old and new <i><b>uitable</b></i>, for events such as cell selection, data modification, key press, and mouse click. The discussion includes a working code example for validating user input and reverting invalid edits. The section also includes a discussion of how to avoid and overcome problems that may occur with the callback execution.</p><h3 id="Scrollbars">4. Customizing scrollbars, column widths and selection behavior</h3><p>This section explains how to control the scrollbars behavior. For example, hiding the horizontal (bottom) scrollbar, automatically displaying it when the data width is larger than the table width. I also show how to control the column widths.</p><p>I then show how to customize the data selection policy: Can multiple cells be selected? perhaps only one cell at a time? or maybe a single large interval of cells? &#8211; this is all customizable. I then explain how the selection can be done and accessed programmatically. Ever wanted to set the cursor on cell A1 after some system event has occurred? &#8211; this will show you how to do it. Ever wanted to use non-standard selection colors (background/foreground)? &#8211; this can also be done.</p><h3 id="Sorting">5. Data sorting</h3><p>Table sorting was discussed in last week&#8217;s <a
target="_blank" href="http://undocumentedmatlab.com/blog/uitable-sorting/">article</a>. This section expands on that article, and explains how the sorting can be customized, controlled, and accessed programmatically, and how sorted rows can be retrieved by the user.<br
/><center><div
class="wp-caption aligncenter" style="width: 270px"><img
alt="Multi-column sorting with blue sort-order numbers" src="http://UndocumentedMatlab.com/images/JIDE_Table_sort3a.png" title="Multi-column sorting with blue sort-order numbers" width="260" /><p
class="wp-caption-text">Multi-column sorting with blue sort-order numbers</p></div></center></p><h3 id="Filtering">6. Data filtering</h3><p>Data filtering is the ability to filter only a specified sub-set of rows for display (just like in Excel). This section explains how to do it (it&#8217;s so easy!).<br
/><center><div
class="wp-caption aligncenter" style="width: 418px"><img
alt="uitable data filtering" src="http://UndocumentedMatlab.com/images/004x013b.png" title="uitable data filtering" width="408" /><p
class="wp-caption-text"><i><b>uitable</b></i> data filtering</p></div></center></p><h3 id="JIDE">7. JIDE customizations</h3><p>The new <i><b>uitable</b></i> is based on an underlying JIDE table. This sectino explains how we can use this to our advantage for some simple and useful. Customization.</p><p>For example: have you wondered some time why is it that columns can only be resized by dragging the tiny divider in the table header? Why can&#8217;t the columns and rows be resized by dragging the grid lines? Well, it turns out that they can, with just a tiny bit of JIDE magic powder, explained here:<br
/><center><div
class="wp-caption aligncenter" style="width: 270px"><img
alt="Resizing columns" src="http://UndocumentedMatlab.com/images/004x014.png" title="Resizing columns" width="260" /><p
class="wp-caption-text">Resizing columns</p></div></center></p><p>Similarly, this section explains how we can use JIDE to merge together adjacent cells:<br
/><center><div
class="wp-caption aligncenter" style="width: 270px"><img
alt="Example of two table cell-spans (1x2 and 2x2)" src="http://UndocumentedMatlab.com/images/004x016.png" title="Example of two table cell-spans (1x2 and 2x2)" width="260" /><p
class="wp-caption-text">Example of two table cell-spans (1x2 and 2x2)</p></div></center></p><h3 id="Structure">8. Controlling the table structure (adding/removing rows)</h3><p>This section discusses the matter of dynamically adding and removing table rows. While this is easy to do in the old <i><b>uitable</b></i>, this is unfortunately not the case in the new <i><b>uitable</b></i>.</p><h3 id="Final">9. Final remarks</h3><p>Here I present a workaround for a long-time table bug. Also, I present my <i><b>createTable</b></i> utility that wraps table creation in Matlab:<br
/><center><div
class="wp-caption aligncenter" style="width: 607px"><img
alt="createTable utility screenshot (note the action buttons, sortable columns, and customized CellEditor)" src="http://UndocumentedMatlab.com/images/table.png" title="createTable utility screenshot (note the action buttons, sortable columns, and customized CellEditor)" width="597" /><p
class="wp-caption-text"><i><b>createTable</b></i> utility screenshot (note the action buttons, sortable columns, and customized CellEditor)</p></div></center></p><h3 id="JIDE-Grids">Appendix – JIDE Grids</h3><p>Finally, this appendix presents an overview of the wide array of components provided by JIDE and available in Matlab. <i><b>uitable</b></i> uses only one of these components (the <code>SortableTable</code>). In fact, there are many more such controls that we can use in our GUIs.</p><p>These include a wide selection of combo-box (drop-down) controls &#8211; calculator, file/folder selection, date selection, color selection, multi-elements selection etc. In addition, a very wide selection of lists, trees and table types is available.</p><p>Also included is a set of specialized editbox controls for accepting IP addresses and credit card numbers:<br
/><center><div
class="wp-caption aligncenter" style="width: 360px"><img
alt="IP address entry box" src="http://UndocumentedMatlab.com/images/JIDE_IPTextField.png" title="IP address entry box" width="100" /><img
alt="credit-card entry box" src="http://UndocumentedMatlab.com/images/JIDE_CreditCard_invalid.png" title="credit-card entry box" width="100" /><img
alt="credit-card entry box" src="http://UndocumentedMatlab.com/images/JIDE_CreditCard_Visa.png" title="credit-card entry box" width="100" /><p
class="wp-caption-text">IP address and credit-card entry boxes</p></div></center></p><p>While not explaining all these controls in detail (this could take hundreds of pages), this section does say a few words on each of them, and includes links to online resources for further exploration.</p><p><pre> </pre>Related posts:<ol><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/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/guide-customization/' rel='bookmark' title='GUIDE customization'>GUIDE customization</a> <small>Matlab's GUI Design Editor (GUIDE) has several interesting undocumented features. This post describes how to customize GUIDE rulers....</small></li><li><a
href='http://undocumentedmatlab.com/blog/button-customization/' rel='bookmark' title='Button customization'>Button customization</a> <small>Matlab's button uicontrols (pushbutton and togglebutton) are basically wrappers for a Java Swing JButton object. This post describes how the buttons can be customized using this Java object in ways...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/uitable-customization-report/feed/</wfw:commentRss> <slash:comments>9</slash:comments> </item> <item><title>Uitable sorting</title><link>http://undocumentedmatlab.com/blog/uitable-sorting/</link> <comments>http://undocumentedmatlab.com/blog/uitable-sorting/#comments</comments> <pubDate>Tue, 26 Jul 2011 18:00:01 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[JIDE]]></category> <category><![CDATA[uitable]]></category> <category><![CDATA[uitools]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2391</guid> <description><![CDATA[Matlab's uitables can be sortable using simple undocumented features<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitable-customization-report/' rel='bookmark' title='Uitable customization report'>Uitable customization report</a> <small>In last week&#8217;s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives/' rel='bookmark' title='Tab panels &#8211; uitab and relatives'>Tab panels &#8211; uitab and relatives</a> <small>This article describes several undocumented Matlab functions that support tab-panels...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/ref/uitable.html"><i><b>uitable</b></i></a> is probably the most complex basic GUI controls available in Matlab. It displays data in a table within a figure, with settable properties as with any other Matlab Handle-Graphics (HG) control. After many years in which the <i><b>uitable</b></i> was available but <a
target="_blank" href="http://undocumentedmatlab.com/blog/legend-semi-documented-feature/#Semi-documented">semi-documented</a> and not officially supported in Matlab, it finally became fully documented and supported in R2008a (aka Matlab 7.6). At that time its internal implementation has changed from a MathWorks-developed Java table to a <a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/JIDE/">JIDE</a>-based Java table (another JIDE-derived table was described <a
target="_blank" href="http://undocumentedmatlab.com/blog/jide-property-grids/">here</a> last year). Since R2008a, both versions of <i><b>uitable</b></i> are available &#8211; the old version is available by adding the &#8216;v0&#8242; input arg.</p><p>Matlab&#8217;s <i><b>uitable</b></i> exposes only a very limited subset of functionalities and properties to the user. Numerous other functionalities are available by accessing the underlying Java table and hidden Matlab properties. Today I will describe a very <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/answers/1880-uitable-can-the-headers-be-made-clickable">common need</a> in GUI tables, that for some unknown reason is missing in Matlab&#8217;s <i><b>uitable</b></i>: Sorting table data columns.</p><p>Last week I <a
target="_blank" href="http://undocumentedmatlab.com/blog/running-vb-code-in-matlab/">explained</a> how we can modify table headers of an ActiveX table control to display sorting icons. In that case, sorting was built-in the control, and the question was just how to display the sorting arrow icon. Unfortunately, Matlab&#8217;s <i><b>uitable</b></i> does not have sorting built-in, although it&#8217;s quite easy to add it, as I shall now show.</p><h3 id="Old">Old uitable sorting</h3><p>The old <i><b>uitable</b></i> is the default control used until R2007b, or that can be selected with the &#8216;v0&#8242; input arg since R2008a. It was based on an internal MathWorks extension of the standard Java Swing <a
target="_blank" rel="nofollow" href="http://download.oracle.com/javase/tutorial/uiswing/components/table.html">JTable</a> &#8211; a class called <code>com.mathworks.widgets.spreadsheet.SpreadsheetTable</code>.</p><p>Users will normally try to sort columns by clicking the header. This has been a deficiency of JTable for ages. To solve this for the old (pre-R2008a) <i><b>uitable</b></i>, download one of several available JTable sorter classes, or my TableSorter class (available <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14225-java-based-data-table">here</a>).  Add the TableSorter.jar file to your static java classpath (via <code>edit('classpath.txt')</code>) or your dynamic classpath (<code>javaaddpath('TableSorter.jar')</code>).</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display the uitable and get its underlying Java object handle</span>
<span style="color: #080;">&#91;</span>mtable,hcontainer<span style="color: #080;">&#93;</span> = uitable<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'v0'</span>, <span style="color: #0000FF;">gcf</span>, <span style="color: #0000FF;">magic</span><span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>, <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'A'</span>, <span style="color:#A020F0;">'B'</span>, <span style="color:#A020F0;">'C'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;   <span style="color: #228B22;">% discard the 'v0' in R2007b and earlier</span>
jtable = mtable.<span style="">getTable</span>;   <span style="color: #228B22;">% or: get(mtable,'table');</span>
&nbsp;
<span style="color: #228B22;">% We want to use sorter, not data model...</span>
<span style="color: #228B22;">% Unfortunately, UitablePeer expects DefaultTableModel not TableSorter so we need a modified UitablePeer class</span>
<span style="color: #228B22;">% But UitablePeer is a Matlab class, so use a modified TableSorter &amp; attach it to the Model</span>
<span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">which</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'TableSorter'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
   <span style="color: #228B22;">% Add TableSorter as TableModel listener</span>
   sorter = TableSorter<span style="color: #080;">&#40;</span>jtable.<span style="">getModel</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
   jtable.<span style="">setModel</span><span style="color: #080;">&#40;</span>sorter<span style="color: #080;">&#41;</span>;
   sorter.<span style="">setTableHeader</span><span style="color: #080;">&#40;</span>jtable.<span style="">getTableHeader</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
   <span style="color: #228B22;">% Set the header tooltip (with sorting instructions)</span>
   jtable.<span style="">getTableHeader</span>.<span style="">setToolTipText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'&lt;html&gt;&amp;nbsp;&lt;b&gt;Click&lt;/b&gt; to sort up; &lt;b&gt;Shift-click&lt;/b&gt; to sort down&lt;br /&gt;&amp;nbsp;...&lt;/html&gt;'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">else</span>
   <span style="color: #228B22;">% Set the header tooltip (no sorting instructions...)</span>
   jtable.<span style="">getTableHeader</span>.<span style="">setToolTipText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'&lt;html&gt;&amp;nbsp;&lt;b&gt;Click&lt;/b&gt; to select entire column&lt;br /&gt;&amp;nbsp;&lt;b&gt;Ctrl-click&lt;/b&gt; (or &lt;b&gt;Shift-click&lt;/b&gt;) to select multiple columns&amp;nbsp;&lt;/html&gt;'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 607px"><img
alt="Sorted uitable - old version" src="http://www.mathworks.com/matlabcentral/fx_files/14225/1/table.png" title="Sorted uitable - old version" width="597" /><p
class="wp-caption-text">Sorted <i><b>uitable</b></i> - old version</p></div></center></p><h3 id="New">New uitable sorting</h3><p>The new <i><b>uitable</b></i> is based on JIDE&#8217;s <code>com.jidesoft.grid.SortableTable</code> and so has built-in sorting support – all you need to do is to turn it on. First get the underlying Java object using my <a
target="_blank" href="http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/">FindJObj utility</a>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display the uitable and get its underlying Java object handle</span>
mtable = uitable<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>, <span style="color:#A020F0;">'Data'</span>,<span style="color: #0000FF;">magic</span><span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'ColumnName'</span>,<span style="color: #080;">&#123;</span><span style="color:#A020F0;">'A'</span>, <span style="color:#A020F0;">'B'</span>, <span style="color:#A020F0;">'C'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
jscrollpane = findjobj<span style="color: #080;">&#40;</span>mtable<span style="color: #080;">&#41;</span>;
jtable = jscrollpane.<span style="">getViewport</span>.<span style="">getView</span>;
&nbsp;
<span style="color: #228B22;">% Now turn the JIDE sorting on</span>
jtable.<span style="">setSortable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;		<span style="color: #228B22;">% or: set(jtable,'Sortable','on');</span>
jtable.<span style="">setAutoResort</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
jtable.<span style="">setMultiColumnSortable</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;
jtable.<span style="">setPreserveSelectionsAfterSorting</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">true</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Note: the Matlab <code>mtable</code> handle has a hidden <b>Sortable</b> property, but it has no effect – use the Java property mentioned above instead. I assume that the hidden <b>Sortable</b> property was meant to implement the sorting behavior in R2008a, but MathWorks never got around to actually implement it, and so it remains this way to this day.</p><h3 id="Report">A more detailed report</h3><p>I have prepared a 30-page report about using and customizing Matlab&#8217;s <i><b>uitable</b></i>, which greatly expands on the above. This report is available for a small fee <a
target="_blank" rel="nofollow" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&#038;business=ymasoftware@gmail.com&#038;currency_code=USD&#038;amount=35&#038;return=&#038;item_name=Matlab-uitable-report">here</a> (please allow up to 48 hours for email delivery). The report includes the following:</p><ul><li>comparison of the old vs. the new <i><b>uitable</b></i> implementations</li><li>description of the <i><b>uitable</b></i> properties and callbacks</li><li>alternatives to <i><b>uitable</b></i> using a variety of technologies</li><li>updating a specific cell&#8217;s value</li><li>setting background and foreground colors for a cell or column</li><li>using dedicated cell renderer and editor components</li><li>HTML processing</li><li>setting dynamic cell-specific tooltip</li><li>setting dynamic cell-specific drop-down selection options</li><li>using a color-selection drop-down for cells</li><li>customizing scrollbars</li><li>customizing column widths and resizing</li><li>customizing selection behavior</li><li>data sorting (expansion of today&#8217;s article)</li><li>data filtering (similar to Excel&#8217;s data filtering control)</li><li>merging table cells</li><li>programmatically adding/removing rows</li><li>numerous links to online resources</li><li>overview of the JIDE grids package, which contains numerous extremely useful GUI controls and components</li></ul><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uitable-customization-report/' rel='bookmark' title='Uitable customization report'>Uitable customization report</a> <small>In last week&#8217;s report about uitable sorting, I offered a report that I have written which covers uitable customization in detail. Several people have asked for more details about the...</small></li><li><a
href='http://undocumentedmatlab.com/blog/uitable-cell-colors/' rel='bookmark' title='Uitable cell colors'>Uitable cell colors</a> <small>A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/' rel='bookmark' title='Matlab and the Event Dispatch Thread (EDT)'>Matlab and the Event Dispatch Thread (EDT)</a> <small>The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives/' rel='bookmark' title='Tab panels &#8211; uitab and relatives'>Tab panels &#8211; uitab and relatives</a> <small>This article describes several undocumented Matlab functions that support tab-panels...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/uitable-sorting/feed/</wfw:commentRss> <slash:comments>16</slash:comments> </item> <item><title>Running VB code in Matlab</title><link>http://undocumentedmatlab.com/blog/running-vb-code-in-matlab/</link> <comments>http://undocumentedmatlab.com/blog/running-vb-code-in-matlab/#comments</comments> <pubDate>Wed, 20 Jul 2011 19:36:05 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[ActiveX]]></category> <category><![CDATA[COM]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2174</guid> <description><![CDATA[Matlab does not natively enable running VB code, but a nice trick enables us to do just that<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/com-activex-tips/' rel='bookmark' title='COM/ActiveX tips &amp; tricks'>COM/ActiveX tips &#038; tricks</a> <small>This article describes several little-known tips useful for COM / ActiveX programming in Matlab...</small></li><li><a
href='http://undocumentedmatlab.com/blog/solving-a-matlab-bug-by-subclassing/' rel='bookmark' title='Solving a MATLAB bug by subclassing'>Solving a MATLAB bug by subclassing</a> <small>Matlab's Image Processing Toolbox's impoint function contains an annoying bug that can be fixed using some undocumented properties....</small></li><li><a
href='http://undocumentedmatlab.com/blog/docs-of-old-matlab-releases/' rel='bookmark' title='Docs of old Matlab releases'>Docs of old Matlab releases</a> <small>MathWorks recently posted archived documentation for many previous Matlab releases...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-installation-woes/' rel='bookmark' title='Matlab installation woes'>Matlab installation woes</a> <small>Matlab has some issues when installing a new version. This post discusses some of them and how to overcome them....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Matlab has a <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/matlab_external/bp_kqh7.html">variety of interfaces</a> to external programming languages. We can easily access ActiveX and COM-enabled applications from Matlab. Matlab also provides easy access to most COM/ActiveX functionalities, with a few exceptions and limitations.</p><p>Unfortunately, Microsoft&#8217;s VBScript is not one of the supported interface languages in Matlab. Therefore, if we need to use VBScript to customize ActiveX or COM functionality or appearance, we have a problem. In some cases, the ActiveX/COM application provides internal methods or properties that enables us to bypass the need for direct VBScript. But in other cases, VBScript is unavoidable.</p><p>Today I&#8217;ll show one such example, and how we can run the required VBScript code in Matlab. Note that the entire discussion is Microsoft oriented, and therefore only applicable to Microsoft Windows. And yes &#8211; not a single word today about Java&#8230;</p><h3 id="need">The need</h3><p>One of the standard controls in Microsoft Windows is ListViewCtrl (more precisely, <code>MSComctlLib.ListViewCtrl.2</code>), which provides a simple sortable table ActiveX. ListViewCtrl should be available on practically all Windows installations, in \Windows\System32\MSCOMCTL.OCX. It is an ancient control (over a decade old), yet still widely used in the ActiveX world.</p><p>While Matlab includes a built-in <i><b>uitable</b></i> function, it is sometimes beneficial to use ListViewCtrl instead. For example, ListViewCtrl enables easy setting of cell-specific tool-tips (ok, yes, this can also be done in <i><b>uitable</b></i>s but it&#8217;s not as easy as in ListViewCtrl). ListViewCtrl also enables easy setting of row color and <a
target="_blank" rel="nofollow" href="http://www.foxite.com/articles/read.aspx?id=39&#038;document=taming-the-microsoft-listview-control">other goodies</a>.</p><p>So ok &#8211; all of these can be overcome in <i><b>uitable</b></i> which is cross-platform (not just Windows-specific) and has some very important features missing from ListViewCtrl (perhaps most importantly, <i><b>uitable</b></i> doesn&#8217;t <b>look</b> a decade old, as ListViewCtrl does). Still, when maintaining legacy code you sometimes don&#8217;t have the luxury of using modern controls.</p><p>Indeed, I once needed to use ListViewCtrl, when I ran into today&#8217;s issue. Here&#8217;s a simple usage example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Set the control's position to be just inside</span>
<span style="color: #228B22;">% the container's pixel position</span>
initPos = getpixelposition<span style="color: #080;">&#40;</span>hParent<span style="color: #080;">&#41;</span> + <span style="color: #080;">&#91;</span><span style="color: #33f;">2</span>,<span style="color: #33f;">3</span>,-<span style="color: #33f;">5</span>,-<span style="color: #33f;">5</span><span style="color: #080;">&#93;</span>;
<span style="color: #080;">&#91;</span>hActiveX,hContainer<span style="color: #080;">&#93;</span> = actxcontrol<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'MSComctlLib.ListViewCtrl.2'</span>, <span style="color: #F0F;">...</span>
                                    <span style="">initPos</span>, <span style="color: #0000FF;">gcf</span>, <span style="color: #F0F;">...</span>
                                    @lbEventHandler<span style="color: #080;">&#41;</span>;
bgColor = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hContainer,<span style="color:#A020F0;">'backgroundColor'</span><span style="color: #080;">&#41;</span>;
bgColorDec = <span style="color: #0000FF;">sum</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">floor</span><span style="color: #080;">&#40;</span>bgColor*<span style="color: #33f;">256</span><span style="color: #080;">&#41;</span>.*<span style="color: #080;">&#40;</span><span style="color: #33f;">256</span>.^<span style="color: #080;">&#91;</span><span style="color: #33f;">0</span><span style="color: #F0F;">:</span><span style="color: #33f;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">%#ok</span>
&nbsp;
<span style="color: #228B22;">% Customize the control's appearance</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hActiveX, <span style="color:#A020F0;">'view'</span>,<span style="color:#A020F0;">'lvwReport'</span>, <span style="color:#A020F0;">'BorderStyle'</span>,<span style="color:#A020F0;">'ccNone'</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'AllowColumnReorder'</span>,<span style="color: #33f;">0</span>, <span style="color:#A020F0;">'Appearance'</span>,<span style="color:#A020F0;">'ccFlat'</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'Sorted'</span>,<span style="color: #33f;">0</span>, <span style="color:#A020F0;">'Checkboxes'</span>,<span style="color: #33f;">1</span>, <span style="color:#A020F0;">'FullRowSelect'</span>,<span style="color: #33f;">1</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'HotTracking'</span>,<span style="color: #33f;">1</span>, <span style="color:#A020F0;">'HoverSelection'</span>,<span style="color: #33f;">0</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'HideColumnHeaders'</span>,<span style="color: #33f;">0</span>, <span style="color:#A020F0;">'BackColor'</span>,bgColorDec<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Set the column headers</span>
panelPos = hgconvertunits<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>, <span style="color: #F0F;">...</span>
                          <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hContainer,<span style="color:#A020F0;">'Position'</span><span style="color: #080;">&#41;</span>, <span style="color: #F0F;">...</span>
                          <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hContainer,<span style="color:#A020F0;">'Units'</span><span style="color: #080;">&#41;</span>, <span style="color: #F0F;">...</span>
                          <span style="color:#A020F0;">'centimeters'</span>, hParent<span style="color: #080;">&#41;</span>;
panelWidth = panelPos<span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>*<span style="color: #33f;">955</span>;  <span style="color: #228B22;">% [Microsoft units...]</span>
colWidths = panelWidth * <span style="color: #080;">&#91;</span><span style="color: #33f;">0.2</span>,<span style="color: #33f;">0.2</span>,<span style="color: #33f;">0.6</span><span style="color: #080;">&#93;</span>;
hColHeaders = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hActiveX,<span style="color:#A020F0;">'ColumnHeaders'</span><span style="color: #080;">&#41;</span>;
hColHeaders.<span style="">Add</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color:#A020F0;">'Name'</span>,<span style="color:#A020F0;">'Name'</span>,colWidths<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
hColHeaders.<span style="">Add</span><span style="color: #080;">&#40;</span><span style="color: #33f;">2</span>,<span style="color:#A020F0;">'Type'</span>,<span style="color:#A020F0;">'Type'</span>,colWidths<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
hColHeaders.<span style="">Add</span><span style="color: #080;">&#40;</span><span style="color: #33f;">3</span>,<span style="color:#A020F0;">'Comment'</span>,<span style="color:#A020F0;">'Comment'</span>,colWidths<span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Add some data</span>
blue = <span style="color: #0000FF;">hex2dec</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'FF0000'</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% reverse HTML hex color: BBGGRR</span>
data = <span style="color: #080;">&#123;</span> <span style="color:#A020F0;">'Orange'</span>, <span style="color:#A020F0;">'Fruit'</span>,     <span style="color:#A020F0;">'My favorite fruit'</span>; <span style="color: #F0F;">...</span>
         <span style="color:#A020F0;">'Onion'</span>,  <span style="color:#A020F0;">'Vegetable'</span>, <span style="color:#A020F0;">'An onion a day keeps everybody else away'</span>; <span style="color: #F0F;">...</span>
         <span style="color:#A020F0;">'Apple'</span>,  <span style="color:#A020F0;">'Fruit'</span>,     <span style="color:#A020F0;">'An apple a day keeps the doctor away'</span>; <span style="color: #F0F;">...</span>
         <span style="color:#A020F0;">'Tomato'</span>, <span style="color:#A020F0;">'Vegetable'</span>, <span style="color:#A020F0;">'You say tomato, I say tomato, let'</span><span style="color:#A020F0;">'s call the whole thing off'</span><span style="color: #080;">&#125;</span>;
<span style="color: #0000FF;">for</span> rowData = data'
    row = hActiveX.<span style="">ListItems</span>.<span style="">Add</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,rowData<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span>,rowData<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
    row.<span style="">ListSubItems</span>.<span style="">Add</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,rowData<span style="color: #080;">&#123;</span><span style="color: #33f;">2</span><span style="color: #080;">&#125;</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,rowData<span style="color: #080;">&#123;</span><span style="color: #33f;">2</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
    row.<span style="">ListSubItems</span>.<span style="">Add</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,rowData<span style="color: #080;">&#123;</span><span style="color: #33f;">3</span><span style="color: #080;">&#125;</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,rowData<span style="color: #080;">&#123;</span><span style="color: #33f;">3</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
    row.<span style="">ListSubItems</span>.<span style="">Item</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>.<span style="">ForeColor</span> = blue;
<span style="color: #0000FF;">end</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 455px"><img
alt="Unsorted ListViewCtrl" src="http://UndocumentedMatlab.com/images/ListViewCtrl_unsorted.png" title="Unsorted ListViewCtrl" width="445" height="172" /><p
class="wp-caption-text">Unsorted ListViewCtrl</p></div></center></p><p>While most of ListViewCtrl&#8217;s features are accessible as internal properties and methods, one particular aspect requires VBScript. Which finally brings us to today&#8217;s topic:</p><p>Apparently, ListViewCtrl supports data sorting by simply clicking the column headers. Unfortunately, the sort icons (up/down arrows) have to be specified using an <a
target="_blank" rel="nofollow" href="http://support.microsoft.com/kb/170146">ImageListCtrl</a> (<code>MSComctlLib.ImageListCtrl.2</code>) that holds the images. ImageListCtrl in turn <a
target="_blank" rel="nofollow" href="http://support.microsoft.com/kb/166937">requires</a> using the internal built-in VB function <i>LoadPicture()</i> to load the images.</p><p>Unfortunately, Matlab does not enable direct invocation of VB code. So unless we find a way of calling VB&#8217;s internal <i>LoadPicture()</i> directly from Matlab, we cannot use sorting icons in our ListViewCtrl.</p><h3 id="ugly">An ugly workaround</h3><p>An ugly workaround for this problem is to modify the column name in run-time, so that clicking a column header (e.g., &#8220;Name&#8221;) will sort its data and also modify the column name to &#8220;Name ^&#8221; or &#8220;Name v&#8221;, depending on the sorting order:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Event handler for ListBox item click</span>
<span style="color: #228B22;">% Note: see documentation within %matlabroot%\toolbox\matlab\winfun\private\comeventcallback.m</span>
<span style="color: #0000FF;">function</span> lbEventHandler<span style="color: #080;">&#40;</span>obj,eventId,item,eventArgs,eventName,<span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">try</span>
    <span style="color: #0000FF;">if</span> <span style="color: #080;">&#40;</span>nargin<span style="color: #F0F;">&amp;</span>lt;<span style="color: #33f;">5</span><span style="color: #080;">&#41;</span> <span style="color: #F0F;">||</span> ~<span style="color: #0000FF;">ischar</span><span style="color: #080;">&#40;</span>eventName<span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">return</span>;   <span style="color: #228B22;">% can't tell which event this is</span>
    <span style="color: #0000FF;">end</span>
&nbsp;
    <span style="color: #0000FF;">switch</span> eventName
      <span style="color: #0000FF;">case</span> <span style="color:#A020F0;">'ItemClick'</span>,
        item.<span style="">Checked</span> = <span style="color: #33f;">1</span> - item.<span style="">Checked</span>;
        doSomethingUseful<span style="color: #080;">&#40;</span>item<span style="color: #080;">&#41;</span>;
&nbsp;
      <span style="color: #0000FF;">case</span> <span style="color:#A020F0;">'ItemCheck'</span>,
        <span style="color: #228B22;">% do nothing</span>
&nbsp;
      <span style="color: #0000FF;">case</span> <span style="color:#A020F0;">'ColumnClick'</span>,
        <span style="color: #228B22;">% Update the sorting order for the new (clicked) column</span>
        obj.<span style="">Sorted</span> = <span style="color: #33f;">1</span>;
        newSortKey = item.<span style="">Index</span> - <span style="color: #33f;">1</span>;
        <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">strcmpi</span><span style="color: #080;">&#40;</span>obj.<span style="">SortOrder</span>,<span style="color:#A020F0;">'lvwAscending'</span><span style="color: #080;">&#41;</span> <span style="color: #F0F;">&amp;&amp;</span> <span style="color: #080;">&#40;</span>newSortKey == obj.<span style="">SortKey</span><span style="color: #080;">&#41;</span>
          obj.<span style="">SortOrder</span> = <span style="color:#A020F0;">'lvwDescending'</span>;
          sortStr = <span style="color:#A020F0;">' v'</span>;
        <span style="color: #0000FF;">else</span>
          obj.<span style="">SortOrder</span> = <span style="color:#A020F0;">'lvwAscending'</span>;
          sortStr = <span style="color:#A020F0;">' ^'</span>;
        <span style="color: #0000FF;">end</span>
&nbsp;
        <span style="color: #228B22;">% Update the column header text with the sort &quot;icon&quot; string</span>
        <span style="color: #0000FF;">if</span> <span style="color: #080;">&#40;</span>obj.<span style="">SortKey</span> ~= newSortKey<span style="color: #080;">&#41;</span>
          oldColHeader = obj.<span style="">ColumnHeaders</span>.<span style="">Item</span><span style="color: #080;">&#40;</span>obj.<span style="">SortKey</span> + <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
          oldColHeader.<span style="">Text</span> = <span style="color: #0000FF;">strrep</span><span style="color: #080;">&#40;</span>oldColHeader.<span style="">Text</span>,<span style="color:#A020F0;">' v'</span>,<span style="color:#A020F0;">''</span><span style="color: #080;">&#41;</span>;
          oldColHeader.<span style="">Text</span> = <span style="color: #0000FF;">strrep</span><span style="color: #080;">&#40;</span>oldColHeader.<span style="">Text</span>,<span style="color:#A020F0;">' ^'</span>,<span style="color:#A020F0;">''</span><span style="color: #080;">&#41;</span>;
          newColHeader = obj.<span style="">ColumnHeaders</span>.<span style="">Item</span><span style="color: #080;">&#40;</span>newSortKey + <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
          newColHeader.<span style="">Text</span> = <span style="color: #080;">&#91;</span>newColHeader.<span style="">Text</span> sortStr<span style="color: #080;">&#93;</span>;
        <span style="color: #0000FF;">end</span>
        obj.<span style="">SortKey</span> = newSortKey;
&nbsp;
      <span style="color: #0000FF;">otherwise</span>,
        <span style="color: #228B22;">% do nothing</span>
    <span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% switch eventName</span>
  <span style="color: #0000FF;">catch</span>
    handleError;
  <span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% lbEventHandler</span></pre></div></div><p>As can be seen, the result does not look very professional:</p><p><center><div
class="wp-caption aligncenter" style="width: 455px"><img
alt="Sorted ListViewCtrl (ugly)" src="http://UndocumentedMatlab.com/images/ListViewCtrl_sorted1.png" title="Sorted ListViewCtrl (ugly)" width="445" height="175" /><p
class="wp-caption-text">Sorted ListViewCtrl (ugly)</p></div></center></p><h3 id="solution">The solution</h3><p>The solution to the problem is to use a not-well-known COM server component called <a
target="_blank" rel="nofollow" href="http://support.microsoft.com/kb/184740">ScriptControl</a> (<code>MSScriptControl.ScriptControl</code>). By using ScriptControl&#8217;s <i>eval()</i> function we can call the built-in <loadpicture
()</i> function and everybody&#8217;s happy:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Load the images using ScriptControl</span>
hScriptControl = actxserver<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'MSScriptControl.ScriptControl'</span><span style="color: #080;">&#41;</span>;
hScriptControl.<span style="">Language</span> = <span style="color:#A020F0;">'VBScript'</span>;
picUp   = hScriptControl.<span style="color: #0000FF;">Eval</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'LoadPicture(&quot;..\Icons\UpIcon.gif&quot;)'</span><span style="color: #080;">&#41;</span>;
picDown = hScriptControl.<span style="color: #0000FF;">Eval</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'LoadPicture(&quot;..\Icons\DownIcon.gif&quot;)'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Now prepare an ImageList with these images</span>
<span style="color: #080;">&#91;</span>hImageListActiveX, hContainer2<span style="color: #080;">&#93;</span> = actxcontrol<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'MSComctlLib.ImageListCtrl.2'</span>,initPos<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hContainer2,<span style="color:#A020F0;">'Pos'</span>,<span style="color: #080;">&#91;</span>initPos<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>,<span style="color: #33f;">1</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% hide the control</span>
hImageListActiveX.<span style="">ListImages</span>.<span style="">Add</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,picUp<span style="color: #080;">&#41;</span>;
hImageListActiveX.<span style="">ListImages</span>.<span style="">Add</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,picDown<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Finally, attach the images to the ListViewCtrl headers</span>
hActiveX.<span style="">ColumnHeaderIcons</span> = hImageListActiveX;</pre></div></div><p>And now modify the sorting icon whenever a column header is clicked:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Event handler for ListBox item click</span>
<span style="color: #228B22;">% Note: see documentation within %ML\toolboc\matlab\winfun\private\comeventcallback.m</span>
<span style="color: #0000FF;">function</span> lbEventHandler<span style="color: #080;">&#40;</span>obj,eventId,item,eventArgs,eventName,<span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">try</span>
    <span style="color: #0000FF;">if</span> <span style="color: #080;">&#40;</span>nargin<span style="color: #F0F;">&amp;</span>lt;<span style="color: #33f;">5</span><span style="color: #080;">&#41;</span> <span style="color: #F0F;">||</span> ~<span style="color: #0000FF;">ischar</span><span style="color: #080;">&#40;</span>eventName<span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">return</span>;   <span style="color: #228B22;">% can't tell which event this is</span>
    <span style="color: #0000FF;">end</span>
&nbsp;
    <span style="color: #0000FF;">switch</span> eventName
      <span style="color: #0000FF;">case</span> <span style="color:#A020F0;">'ItemClick'</span>,
        item.<span style="">Checked</span> = <span style="color: #33f;">1</span> - item.<span style="">Checked</span>;
        doSomethingUseful<span style="color: #080;">&#40;</span>item<span style="color: #080;">&#41;</span>;
&nbsp;
      <span style="color: #0000FF;">case</span> <span style="color:#A020F0;">'ItemCheck'</span>,
        <span style="color: #228B22;">% do nothing</span>
&nbsp;
      <span style="color: #0000FF;">case</span> <span style="color:#A020F0;">'ColumnClick'</span>,
        <span style="color: #228B22;">% Update the sorting order for the new (clicked) column</span>
        obj.<span style="">Sorted</span> = <span style="color: #33f;">1</span>;
        newSortKey = item.<span style="">Index</span> - <span style="color: #33f;">1</span>;
        <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">strcmpi</span><span style="color: #080;">&#40;</span>obj.<span style="">SortOrder</span>,<span style="color:#A020F0;">'lvwAscending'</span><span style="color: #080;">&#41;</span> <span style="color: #F0F;">&amp;&amp;</span> <span style="color: #080;">&#40;</span>newSortKey == obj.<span style="">SortKey</span><span style="color: #080;">&#41;</span>
          obj.<span style="">SortOrder</span> = <span style="color:#A020F0;">'lvwDescending'</span>;
          sortIconIndex = <span style="color: #33f;">1</span>;
        <span style="color: #0000FF;">else</span>
          obj.<span style="">SortOrder</span> = <span style="color:#A020F0;">'lvwAscending'</span>;
          sortIconIndex = <span style="color: #33f;">0</span>;
        <span style="color: #0000FF;">end</span>
&nbsp;
        <span style="color: #228B22;">% Remove sort icon from previous column</span>
        setSortIcon<span style="color: #080;">&#40;</span>obj.<span style="">hWnd</span>,obj.<span style="">SortKey</span>,-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
&nbsp;
        <span style="color: #228B22;">% Add the sort icon to the clicked column header</span>
        setSortIcon<span style="color: #080;">&#40;</span>obj.<span style="">hWnd</span>,newSortKey,sortIconIndex<span style="color: #080;">&#41;</span>;
&nbsp;
        <span style="color: #228B22;">% Update the previous column header text, to remove '...' due to the sort icon</span>
        <span style="color: #0000FF;">if</span> <span style="color: #080;">&#40;</span>obj.<span style="">SortKey</span> ~= newSortKey<span style="color: #080;">&#41;</span>
          oldColHeader = obj.<span style="">ColumnHeaders</span>.<span style="">Item</span><span style="color: #080;">&#40;</span>obj.<span style="">SortKey</span> + <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
          oldColHeader.<span style="">Text</span> = oldColHeader.<span style="">Text</span>;
        <span style="color: #0000FF;">end</span>
        obj.<span style="">SortKey</span> = newSortKey;
&nbsp;
      <span style="color: #0000FF;">otherwise</span>,
        <span style="color: #228B22;">% do nothing</span>
    <span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% switch eventName</span>
  <span style="color: #0000FF;">catch</span>
    handleError;
  <span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% lbEventHandler</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 455px"><img
alt="Sorted ListViewCtrl (much better)" src="http://UndocumentedMatlab.com/images/ListViewCtrl_sorted2.png" title="Sorted ListViewCtrl (much better)" width="445" height="174" /><p
class="wp-caption-text">Sorted ListViewCtrl (much better)</p></div></center></p><p>This code uses the MEX utility <a
target="_blank" href="http://UndocumentedMatlab.com/files/setSortIcon.zip">setSortIcon</a>, which was developed (not by me, I can&#8217;t remember by whom) based on <a
target="_blank" rel="nofollow" href="http://vbnet.mvps.org/index.html?code/comctl/lvheaderimage.htm">this</a>. Simply unzip the setSortIcon.zip file into your favorite folder on the Matlab path. The zip file contains the c source code, that you can modify and mex-recompile if you wish.</p><h3 id="Macros">Running VBA Macros</h3><p>A related issue is running VBA macros from Matlab. This was very recently asked and <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/243292#845753">answered</a> on the CSSM forum, so I&#8217;ll just provide Peter Lorin Rasmussen&#8217;s code (read higher up in that CSSM thread for more details):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Initiate active X component</span>
powerPoint = actxserver<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'PowerPoint.Application'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Visible</span>
powerPoint.<span style="">Visible</span>=<span style="color: #33f;">1</span>;
&nbsp;
<span style="color: #228B22;">% Minimize powerpoint</span>
powerPoint.<span style="">WindowState</span>=<span style="color: #33f;">2</span>;
&nbsp;
<span style="color: #228B22;">% Load presentations object</span>
presentations = powerPoint.<span style="">presentations</span>;
&nbsp;
<span style="color: #228B22;">% Open file</span>
presentation = invoke<span style="color: #080;">&#40;</span>presentations,<span style="color:#A020F0;">'open'</span>,fileName<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Load VBA project</span>
k = presentation.<span style="">VBProject</span>.<span style="">Collection</span>.<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Run Macro</span>
k = invoke<span style="color: #080;">&#40;</span>presentation.<span style="">application</span>,<span style="color:#A020F0;">'run'</span>,macroName,<span style="color: #0000FF;">varargin</span><span style="color: #080;">&#123;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Close all</span>
presentation.<span style="color: #0000FF;">Save</span>;
presentation.<span style="color: #0000FF;">Close</span>;
powerPoint.<span style="color: #0000FF;">Quit</span>;
powerPoint.<span style="color: #0000FF;">delete</span>;</pre></div></div><p>Do you have any favorite code in another programming language that you run from Matlab? Don&#8217;t be shy &#8211; please tell us all about it in a comment <a
href="http://UndocumentedMatlab.com/blog/running-vb-code-in-matlab/#respond">below</a>.</loadpicture></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/com-activex-tips/' rel='bookmark' title='COM/ActiveX tips &amp; tricks'>COM/ActiveX tips &#038; tricks</a> <small>This article describes several little-known tips useful for COM / ActiveX programming in Matlab...</small></li><li><a
href='http://undocumentedmatlab.com/blog/solving-a-matlab-bug-by-subclassing/' rel='bookmark' title='Solving a MATLAB bug by subclassing'>Solving a MATLAB bug by subclassing</a> <small>Matlab's Image Processing Toolbox's impoint function contains an annoying bug that can be fixed using some undocumented properties....</small></li><li><a
href='http://undocumentedmatlab.com/blog/docs-of-old-matlab-releases/' rel='bookmark' title='Docs of old Matlab releases'>Docs of old Matlab releases</a> <small>MathWorks recently posted archived documentation for many previous Matlab releases...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-installation-woes/' rel='bookmark' title='Matlab installation woes'>Matlab installation woes</a> <small>Matlab has some issues when installing a new version. This post discusses some of them and how to overcome them....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/running-vb-code-in-matlab/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           category
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-02-04 03:18:56 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/category/gui/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      1.972s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1328350734; expires=Sun, 03-Feb-2013 10:18:54 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Sat, 04 Feb 2012 10:18:56 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Sat, 04 Feb 2012 11:18:56 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               73b41074f489767cb929570b516e758a
Content-Encoding:   gzip
-->
