<?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>Semi-documented function &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/semi-documented-function/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Thu, 28 Jun 2018 12:57:59 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>String/char compatibility</title>
		<link>https://undocumentedmatlab.com/articles/string-char-compatibility?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=string-char-compatibility</link>
					<comments>https://undocumentedmatlab.com/articles/string-char-compatibility#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 28 Jun 2018 12:57:59 +0000</pubDate>
				<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Semi-documented function]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7710</guid>

					<description><![CDATA[<p>Backward compatibility of strings in function inputs is a challenge, that can be assisted with an undocumented builtin  function. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/string-char-compatibility">String/char compatibility</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/sliders-in-matlab-gui-part-2" rel="bookmark" title="Sliders in Matlab GUI &#8211; part 2">Sliders in Matlab GUI &#8211; part 2 </a> <small>Matlab contains a variety of ways to define/display slider controls in GUI windows. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/converting-java-vectors-to-matlab-arrays" rel="bookmark" title="Converting Java vectors to Matlab arrays">Converting Java vectors to Matlab arrays </a> <small>Converting Java vectors to Matlab arrays is pretty simple - this article explains how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects" rel="bookmark" title="Types of undocumented Matlab aspects">Types of undocumented Matlab aspects </a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/ismembc-undocumented-helper-function" rel="bookmark" title="ismembc &#8211; undocumented helper function">ismembc &#8211; undocumented helper function </a> <small>Matlab has several undocumented internal helper functions that can be useful on their own in some cases. This post presents the ismembc function....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>In numerous functions that I wrote over the years, some input arguments were expected to be strings in the old sense, i.e. char arrays for example, <code>'on'</code> or <code>'off'</code>. Matlab release R2016b introduced the concept of <a href="https://www.mathworks.com/help/matlab/characters-and-strings.html" rel="nofollow" target="_blank">string objects</a>, which can be created using the <i><b>string</b></i> function or [starting in R2017a] double quotes (<code>"on"</code>).<br />
The problem is that I have numerous functions that supported the old char-based strings but not the new string objects. If someone tries to enter a string object (<code>"on"</code>) as input to a function that expects a char-array (<code>'on'</code>), in many cases Matlab will error. This by itself is very unfortunate &#8211; I would have liked everything to be fully backward-compatible. But unfortunately this is not the case: MathWorks did invest effort in making the new strings backward-compatible to some degree (for example, graphic object property names/values and many internal functions that now accept either form as input). However, backward compatibility of strings is not 100% perfect.<br />
In such cases, the only solution is to make the function accept both forms (char-arrays and string objects), for example, by type-casting all such inputs as char-arrays using the builtin <i><b>char</b></i> function. If we do this at the top of our function, then the rest of the function can remain unchanged. For example:<br />
<span id="more-7710"></span></p>
<pre lang="matlab" highlight="2-4">
function test(stage)
   if isa(stage,'string')
      stage = char(stage);
   end
   % from this point onward, we don't need to worry about string inputs - any such strings will become plain-ol' char-arrays
   switch stage
      case 'stage 1', ...
      case 'stage 2', ...
      ...
   end
end
</pre>
<p>That was simple enough. But what if our function expects complex inputs (cell-arrays, structs etc.) that may contain strings in only some of their cells/fields?<br />
Luckily, Matlab contains an internal utility function that can help us: <i><b>controllib.internal.util.hString2Char</b></i>. This function, whose Matlab source-code is available (<i>%matlabroot%/toolbox/shared/controllib/general/+controllib/+internal/+util/hString2Char.m</i>) recursively scans the input value and converts any string object into the corresponding char-array, leaving all other data-types unchanged. For example:</p>
<pre lang="matlab">
>> controllib.internal.util.hString2Char({123, 'char-array', "a string"})
ans =
  1×3 cell array
    {[123]}    {'char-array'}    {'a string'}
>> controllib.internal.util.hString2Char(struct('a',"another string", 'b',pi))
ans =
  struct with fields:
    a: 'another string'
    b: 3.14159265358979
</pre>
<p>In order to keep our code working not just on recent releases (that support strings and <i><b>controllib.internal.util.hString2Char</b></i>) but also on older Matlab releases (where they did not exist), we simply wrap the call to <i><b>hString2Char</b></i> within a <i><b>try</b></i>&#8211;<i><b>catch</b></i> block. The adaptation of our function might then look as follows:</p>
<pre lang="matlab" highlight="2">
function test(varargin)
   try varargin = controllib.internal.util.hString2Char(varargin); catch, end
   % from this point onward, we don't need to worry about string inputs - any such strings will become plain-ol' char-arrays
   ...
end
</pre>
<p>Note that <i><b>controllib.internal.util.hString2Char</b></i> is a semi-documented function: it contains a readable internal help section (accessible via <code>help controllib.internal.util.hString2Char</code>), but not a doc-page. Nor is this function mentioned anywhere in Matlab&#8217;s official documentation. I think that this is a pity, because it&#8217;s such a useful little helper function.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/string-char-compatibility">String/char compatibility</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/sliders-in-matlab-gui-part-2" rel="bookmark" title="Sliders in Matlab GUI &#8211; part 2">Sliders in Matlab GUI &#8211; part 2 </a> <small>Matlab contains a variety of ways to define/display slider controls in GUI windows. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/converting-java-vectors-to-matlab-arrays" rel="bookmark" title="Converting Java vectors to Matlab arrays">Converting Java vectors to Matlab arrays </a> <small>Converting Java vectors to Matlab arrays is pretty simple - this article explains how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects" rel="bookmark" title="Types of undocumented Matlab aspects">Types of undocumented Matlab aspects </a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/ismembc-undocumented-helper-function" rel="bookmark" title="ismembc &#8211; undocumented helper function">ismembc &#8211; undocumented helper function </a> <small>Matlab has several undocumented internal helper functions that can be useful on their own in some cases. This post presents the ismembc function....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/string-char-compatibility/feed</wfw:commentRss>
			<slash:comments>18</slash:comments>
		
		
			</item>
		<item>
		<title>Password &#038; spinner controls in Matlab GUI</title>
		<link>https://undocumentedmatlab.com/articles/password-and-spinner-controls-in-matlab-gui?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=password-and-spinner-controls-in-matlab-gui</link>
					<comments>https://undocumentedmatlab.com/articles/password-and-spinner-controls-in-matlab-gui#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 14 Dec 2016 17:28:09 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Semi-documented function]]></category>
		<category><![CDATA[AppDesigner]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6775</guid>

					<description><![CDATA[<p>Password fields and spinner controls can easily be embedded in Matlab GUIs. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/password-and-spinner-controls-in-matlab-gui">Password &amp; spinner controls in Matlab GUI</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-7-selection-controls" rel="bookmark" title="Matlab toolstrip – part 7 (selection controls)">Matlab toolstrip – part 7 (selection controls) </a> <small>Matlab toolstrips can contain a wide variety of selection controls: popups, combo-boxes, and galleries. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-6-complex-controls" rel="bookmark" title="Matlab toolstrip – part 6 (complex controls)">Matlab toolstrip – part 6 (complex controls) </a> <small>Multiple types of customizable controls can be added to Matlab toolstrips...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-spinners-in-matlab-gui" rel="bookmark" title="Using spinners in Matlab GUI">Using spinners in Matlab GUI </a> <small>Spinner controls can easily be added to Matlab GUI. This article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/reverting-axes-controls-in-figure-toolbar" rel="bookmark" title="Reverting axes controls in figure toolbar">Reverting axes controls in figure toolbar </a> <small>In R2018b the axes controls were removed from the figure toolbar, but this can be reverted. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>I often include configuration panels in my programs, to enable the user to configure various program aspects, such as which emails should automatically be sent by the program to alert when certain conditions occur. Last week <a href="/articles/sending-email-text-messages-from-matlab" target="_blank">I presented</a> such a configuration panel, which is mainly composed of standard documented Matlab controls (sub-panels, uitables and uicontrols). As promised, today&#8217;s post will discuss two undocumented controls that are often useful in similar configuration panels (not necessarily for emails): password fields and spinners.<br />
<center><a href="/images/email_config_panel.png" target="_blank"><img decoding="async" alt="Matlab GUI configuration panel including password and spinner controls (click to zoom-in)" src="https://undocumentedmatlab.com/images/email_config_panel.png" title="Matlab GUI configuration panel including password and spinner controls (click to zoom-in)" width="75%" style="max-width:842px;"></a><br />
Matlab GUI configuration panel including password and spinner controls (click to zoom-in)</center><br />
Password fields are basically editboxes that hide the typed text with some generic echo character (such as * or a bullet); spinners are editboxes that only enable typing certain preconfigured values (e.g., numbers in a certain range). Both controls are part of the standard Java Swing package, on which the current (non-web-based) Matlab GUIs relies. In both cases, we can use the <a href="/articles/javacomponent" target="_blank"><i><b>javacomponent</b></i> function</a> to place the built-in Swing component in our Matlab GUI.<br />
<span id="more-6775"></span></p>
<h3 id="password">Password field</h3>
<p>The relevant Java Swing control for password fields is <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/components/passwordfield.html"><code>javax.swing.JPasswordField</code></a>. <code>JPasswordField</code> is basically an editbox that hides any typed key with a * or bullet character.<br />
Here&#8217;s a basic code snippet showing how to display a simple password field:</p>
<pre lang='matlab'>
jPasswordField = javax.swing.JPasswordField('defaultPassword');  % default password arg is optional
jPasswordField = javaObjectEDT(jPasswordField);  % javaObjectEDT is optional but recommended to avoid timing-related GUI issues
jhPasswordField = javacomponent(jPasswordField, [10,10,70,20], gcf);
</pre>
<p><center><figure style="width: 105px" class="wp-caption aligncenter"><img decoding="async" alt="Password control" src="https://undocumentedmatlab.com/images/JPasswordField.gif" title="Password control" width="105" height="30" /><figcaption class="wp-caption-text">Password control</figcaption></figure></center><br />
We can set/get the password string programmatically via the <b>Text</b> property; the displayed (echo) character can be set/get using the <b>EchoChar</b> property.<br />
To attach a data-change callback, set jhPasswordField&#8217;s <b>ActionPerformedCallback</b> property.</p>
<h3 id="spinner">Spinner control</h3>
<p><a href="/articles/using-spinners-in-matlab-gui" target="_blank">detailed post on using spinners in Matlab GUI</a><br />
The relevant Java Swing control for spinners is <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html"><code>javax.swing.JSpinner</code></a>. <code>JSpinner</code> is basically an editbox with two tiny adjacent up/down buttons that visually emulate a small round spinning knob. 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).<br />
<code>JSpinner</code> uses an internal data model. The default model is <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/7/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/7/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/7/docs/api/javax/swing/SpinnerDateModel.html"><code>SpinnerDateModel</code></a> (which defines a date range and step unit).<br />
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 0.1:</p>
<pre lang='matlab'>
jModel = javax.swing.SpinnerNumberModel(24,20,35,0.1);
jSpinner = javax.swing.JSpinner(jModel);
jSpinner = javaObjectEDT(jSpinner);  % javaObjectEDT is optional but recommended to avoid timing-related GUI issues
jhSpinner = javacomponent(jSpinner, [10,10,70,20], gcf);
</pre>
<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 various models have specific properties. For example, <code>SpinnerNumberModel</code> has the settable <b>Maximum</b>, <b>Minimum</b> and <b>StepSize</b> properties.<br />
To attach a data-change callback, set jhSpinner&#8217;s <b>StateChangedCallback</b> property.<br />
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 Java documentation. Readers are welcome to download this demo from the Matlab File Exchange and reuse its source code.<br />
<center><figure style="width: 188px" class="wp-caption aligncenter"><img decoding="async" alt="Matlab SpinnerDemo" src="https://undocumentedmatlab.com/images/SpinnerDemoMatlab.png" title="Matlab SpinnerDemo" width="188" height="184"/><figcaption class="wp-caption-text">Matlab SpinnerDemo</figcaption></figure></center><br />
The nice thing about spinners is that you can set a custom display format without affecting the underlying data model. For example, the following code snippet update the spinner&#8217;s display format without affecting its underlying numeric data model:</p>
<pre lang="matlab">
formatStr = '$ #,##0.0 Bn';
jEditor = javaObject('javax.swing.JSpinner$NumberEditor', jhSpinner, formatStr);
jhSpinner.setEditor(jEditor);
</pre>
<p><center><figure style="width: 105px" class="wp-caption aligncenter"><img decoding="async" alt="Formatted spinner control" src="https://undocumentedmatlab.com/images/JSpinner.gif" title="Formatted spinner control" width="105" height="30" /><figcaption class="wp-caption-text">Formatted spinner control</figcaption></figure></center><br />
For more information, refer to my <a href="/articles/using-spinners-in-matlab-gui" target="_blank">detailed post on using spinners in Matlab GUI</a>.</p>
<h3 id="warning">Caveat emptor</h3>
<p>MathWorks&#8217; new web-based GUI paradigm will most probably not directly support the Java components presented in today&#8217;s post, or more specifically the <i><b>javacomponent</b></i> function that enables placing them in Matlab GUIs. The new web-based GUI-building application (<a href="https://www.mathworks.com/products/matlab/app-designer.html" rel="nofollow" target="_blank">AppDesigner</a>, aka AD) does contain a spinner, although it is [currently] limited to displaying numeric values (not dates/lists as in my SpinnerDemo). Password fields are not currently supported by AppDesigner at all, and it is unknown whether they will ever be.<br />
All this means that users of Java controls who wish to transition to the new web-based GUIs will need to develop <a href="/articles/editbox-data-input-validation" rel="nofollow" target="_blank">programmatic workarounds</a>, that would presumably appear and behave less professional. It&#8217;s a tradeoff: AppDesigner does include features that improve GUI usability, not to mention the presumed future ability to post Matlab GUIs online (hopefully without requiring a monstrous Matlab Production Server license/installation).<br />
In the past, MathWorks has posted a <a href="http://www.mathworks.com/support/contact_us/dev/javaframe.html" rel="nofollow" target="_blank">dedicated webpage</a> to solicit user feedback on how they are using the figure&#8217;s <b>JavaFrame</b> property. MathWorks will presumably prepare a similar webpage to solicit user feedback on uses of the <i><b>javacomponent</b></i> function, so they could add the top items to AppDesigner, making the transition to web-based GUIs less painful. When such a survey page becomes live, I will post about it on this website so that you could tell MathWorks about your specific use-cases and help them prioritize their R&#038;D efforts.<br />
In any case, regardless of whether the functionality eventually makes it into AppDesigner, my hope is that when the time comes MathWorks will not pull the plug from non-web GUIs, and will still enable running them on desktops for backward compatibility (&#8220;legacy mode&#8221;). Users of existing GUIs will then not need to choose between upgrading their Matlab (and redeveloping their GUI as a web-based app) and running their existing programs. Instead, users will face the much less painful choice between keeping the existing Java-based programs and developing a web-based variant at some later time, separate from the choice of whether or not to upgrade Matlab. The increased revenue from license upgrades and SMS (maintenance plan) renewals might well offset the R&#038;D effort that would be needed to keep supporting the old Java-based figures. The traumatic<sup>*</sup> release of HG2 in R2014b, where a less-than-perfect version was released with no legacy mode, resulting in significant user backlash/disappointment, is hopefully still fresh in the memory of decision makers and would hopefully not be repeated.<br />
<sup>*</sup><i>well, traumatic for some at least. I really don&#8217;t wish to make this a debate on HG2&#8217;s release; I&#8217;d rather focus on making the transition to web-based GUIs as seamless as possible.</i></p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/password-and-spinner-controls-in-matlab-gui">Password &amp; spinner controls in Matlab GUI</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-7-selection-controls" rel="bookmark" title="Matlab toolstrip – part 7 (selection controls)">Matlab toolstrip – part 7 (selection controls) </a> <small>Matlab toolstrips can contain a wide variety of selection controls: popups, combo-boxes, and galleries. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-6-complex-controls" rel="bookmark" title="Matlab toolstrip – part 6 (complex controls)">Matlab toolstrip – part 6 (complex controls) </a> <small>Multiple types of customizable controls can be added to Matlab toolstrips...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-spinners-in-matlab-gui" rel="bookmark" title="Using spinners in Matlab GUI">Using spinners in Matlab GUI </a> <small>Spinner controls can easily be added to Matlab GUI. This article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/reverting-axes-controls-in-figure-toolbar" rel="bookmark" title="Reverting axes controls in figure toolbar">Reverting axes controls in figure toolbar </a> <small>In R2018b the axes controls were removed from the figure toolbar, but this can be reverted. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/password-and-spinner-controls-in-matlab-gui/feed</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>Editable combo-box</title>
		<link>https://undocumentedmatlab.com/articles/editable-combo-box?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=editable-combo-box</link>
					<comments>https://undocumentedmatlab.com/articles/editable-combo-box#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 09 Oct 2013 15:38:55 +0000</pubDate>
				<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[UI controls]]></category>
		<category><![CDATA[uicontrol]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4246</guid>

					<description><![CDATA[<p>Matlab's popup menu (combo-box) control is quite limited in its abilities. This article explains how we can work around these limitations. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/editable-combo-box">Editable combo-box</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/using-jide-combo-boxes" rel="bookmark" title="Using JIDE combo-boxes">Using JIDE combo-boxes </a> <small>Matlab includes many custom JIDE combo-box controls that can be used in Matlab GUIs out of the box. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/additional-uicontrol-tooltip-hacks" rel="bookmark" title="Additional uicontrol tooltip hacks">Additional uicontrol tooltip hacks </a> <small>Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/auto-completion-widget" rel="bookmark" title="Auto-completion widget">Auto-completion widget </a> <small>Matlab includes a variety of undocumented internal controls that can be used for an auto-completion component. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>In previous articles, I explained how we can use <a target="_blank" href="/articles/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i></a> to customize a Matlab uicontrol&#8217;s underlying Java control, thereby improving its appearance and functionality. My two previous <a target="_blank" href="/articles/customizing-editboxes/">articles</a> on the Matlab editbox were a classic example of this mechanism. Unfortunately, this technique does not always work well, and an alternative mechanism needs to be used. One such case in point is the subject of today&#8217;s article.<br />
Matlab combo-box (a.k.a. &#8220;popup-menu&#8221; or &#8220;drop-down&#8221;) controls are very simple controls that accept a list of strings from which a user can select. This is a very simple control that answers a wide range of use-cases. Unfortunately, it is quite limited. Among other limitations, we cannot modify the popup-panel&#8217;s appearance/functionality; we cannot select multiple items; and we cannot type-in a value in the control&#8217;s editbox. Today&#8217;s article will focus on the latter limitation, namely how to use an editable combo-box.</p>
<h3 id="standard">Trying to make Matlab&#8217;s standard combobox editable</h3>
<p>We can indeed use <i><b>findjobj</b></i> to get a Matlab combo-box&#8217;s underlying Java control. This turns out to be a <code>com.mathworks.hg.peer.ComboboxPeer$MLComboBox</code> object, which extends the standard Java Swing <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html">JComboBox</a>:</p>
<pre lang='matlab'>
>> hCombo = uicontrol('Style','popup', 'String',{'a','b','c'});
>> jCombo = findjobj(hCombo)
jCombo =
	javahandle_withcallbacks.com.mathworks.hg.peer.ComboboxPeer$MLComboBox
</pre>
<p>This <code>jCombo</code> control has the <b>Editable</b> property that we can then try to override (the default value is <code>false</code>):</p>
<pre lang='matlab'>
>> jCombo.setEditable(true);
>> jCombo.Editable = true;          % equivalent alternative
>> set(jCombo,'setEditable',true);  % equivalent alternative
</pre>
<p><figure style="width: 98px" class="wp-caption alignleft"><img loading="lazy" decoding="async" alt="Editable combo-box control" src="https://undocumentedmatlab.com/images/combobox_editable.png" title="Editable combo-box control" width="98" height="75" /><figcaption class="wp-caption-text">Editable combo-box control</figcaption></figure><br />
The bad news is that the moment we enter some value (as in the screenshot here) that is not already a member of the pop-up panel (i.e., the cell-array in the <b>String</b> property), then the control disappears and a warning message is issued to the Matlab console:</p>
<pre lang='matlab'>
Warning: popupmenu control requires that Value be an integer within String range
Control will not be rendered until all of its parameter values are valid
(Type "warning off MATLAB:hg:uicontrol:ParameterValuesMustBeValid" to suppress this warning.)
</pre>
<p><span id="more-4246"></span><br />
The reason for this behavior is that when the combo-box object detects that the text field&#8217;s content match none of the popup list items, it automatically sets <code>jCombo</code>&#8216;s <b>SelectedIndex</b> to -1 and Matlab&#8217;s corresponding HG <b>Value</b> property to 0. At this point the Matlab implementation kicks in, hiding the uicontrol since it considers 0 an invalid value for the <b>Value</b> property. This is similar to the check being done to test for an empty HG String value (=no items):</p>
<pre lang='matlab'>
>> set(hCombo, 'string', [])
popupmenu control requires a non-empty String
Control will not be rendered until all of its parameter values are valid.
</pre>
<p>We can clear these particular warnings via:</p>
<pre lang='matlab'>warning('off','MATLAB:hg:uicontrol:ParameterValuesMustBeValid')</pre>
<p>but this does not prevent the control from being hidden – it just prevents the warning from showing on the Command Window.<br />
We can dive deeper and try to modify the underlying data model and disassociate the Java control from HG, but let&#8217;s stop at this point since there is really no point considering the fact that there is a much simpler alternative.<br />
Note: if you do decide to make the standard Matlab uicontrol editable, read <a target="_blank" href="/articles/additional-uicontrol-tooltip-hacks/#truncated_text">here</a> for a related customization that I posted several years ago.</p>
<h3 id="Java">The pure-Java alternative</h3>
<p>Rather than customizing Matlab&#8217;s uicontrol, we can easily create an identical-looking control using the standard Java Swing JComboBox, which has none of these problems/limitations. We can create and display the component in one go using the semi-documented <a target="_blank" href="/articles/javacomponent/"><i><b>javacomponent</b></i></a> function:</p>
<pre lang='matlab' highlight='5,6'>
position = [10,100,90,20];  % pixels
hContainer = gcf;  % can be any uipanel or figure handle
options = {'a','b','c'};
model = javax.swing.DefaultComboBoxModel(options);
jCombo = javacomponent('javax.swing.JComboBox', position, hContainer);
jCombo.setModel(model);
jCombo.setEditable(true);
</pre>
<p>We can use a variant of <i><b>javacomponent</b></i> to create the component with the model in one go, replacing the highlighted lines above:</p>
<pre lang='matlab'>jCombo = javacomponent({'javax.swing.JComboBox',model}, position, hContainer);</pre>
<p>Once we have the control ready, all we need to do is set its <b>ActionPerformedCallback</b> property to our Matlab callback function and we&#8217;re pretty-much set. We can get the currently-selected text using <code>char(jCombo.getSelectedItem)</code> and the selected list index (0-based) using <code>jCombo.getSelectedIndex</code> (which returns -1 if we entered a non-listed value, 0 for the first list item, 1 for the second etc.). More information can be found <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html">here</a>.</p>
<h3 id="conclusion">Lesson learnt</h3>
<p>In the past 20+ years of my professional life as engineer and development manager, my number one rule <a target="_blank" href="/articles/xlsread-functionality-change-in-r2012a/">has always been</a>:<br />
<center><font size=+3 color="red"><i><b>DON&#8217;T FIX IT, IF IT AIN&#8217;T BROKE!</b></i></font></center><br />
But sometimes, we should also remember an alternative version:<br />
<center><font size=+3 color="red"><i><b>DON&#8217;T FIX IT, IF IT&#8217;S EASIER TO REPLACE!</b></i></font></center></p>
<h3 id="report">Available report &#8211; &#8220;Advanced Customizations of Matlab Uicontrols&#8221;</h3>
<p>Interested readers can find more information about these and other possible customizations in my report on &#8220;<i>Advanced Customizations of Matlab Uicontrols</i>&#8220;. This 90-page PDF report can be purchased <a target="_blank" rel="nofollow" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&#038;currency_code=USD&#038;business=altmany@gmail.com&#038;amount=29&#038;item_name=Matlab+uicontrols+customization+report">here</a> ($29, please allow 24 hours for delivery by email). The report explains how to customize Matlab&#8217;s uicontrols in ways that are simply not possible using documented Matlab properties. This includes treatment of push buttons, toggle buttons, radio buttons, checkboxes, editboxes, listboxes, popup menus (aka combo-boxes/drop-downs), sliders, labels, and tooltips. Much of the information in the report is also available in hard-copy format in chapter 6 of my <a target="_blank" href="/matlab-java-book/">Matlab-Java programming book</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/editable-combo-box">Editable combo-box</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/using-jide-combo-boxes" rel="bookmark" title="Using JIDE combo-boxes">Using JIDE combo-boxes </a> <small>Matlab includes many custom JIDE combo-box controls that can be used in Matlab GUIs out of the box. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/additional-uicontrol-tooltip-hacks" rel="bookmark" title="Additional uicontrol tooltip hacks">Additional uicontrol tooltip hacks </a> <small>Matlab's uicontrol tooltips have several limitations that can be overcome using the control's underlying Java object....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/auto-completion-widget" rel="bookmark" title="Auto-completion widget">Auto-completion widget </a> <small>Matlab includes a variety of undocumented internal controls that can be used for an auto-completion component. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/editable-combo-box/feed</wfw:commentRss>
			<slash:comments>20</slash:comments>
		
		
			</item>
		<item>
		<title>Function definition meta-info</title>
		<link>https://undocumentedmatlab.com/articles/function-definition-meta-info?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=function-definition-meta-info</link>
					<comments>https://undocumentedmatlab.com/articles/function-definition-meta-info#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 04 Sep 2013 13:38:42 +0000</pubDate>
				<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Semi-documented function]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4141</guid>

					<description><![CDATA[<p>There are multiple ways to retrieve meta-info about functions in Matlab. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/function-definition-meta-info">Function definition meta-info</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/function-call-timeline-profiling" rel="bookmark" title="Function call timeline profiling">Function call timeline profiling </a> <small>A new utility enables to interactively explore Matlab function call timeline profiling. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/sparse-data-math-info" rel="bookmark" title="Sparse data math info">Sparse data math info </a> <small>Matlab contains multiple libraries for handling sparse data. These can report very detailed internal info. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/math-libraries-version-info-upgrade" rel="bookmark" title="Math libraries version info &amp; upgrade">Math libraries version info &amp; upgrade </a> <small>Matlab exposes undocumented info about its internal math libraries; these libraries can be upgraded manually. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/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>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Last week, Loren Shure <a target="_blank" rel="nofollow" href="http://blogs.mathworks.com/loren/2013/08/26/what-kind-of-matlab-file-is-this/">posted an article</a> explaining some documented ways to retrieve information about the type of Matlab functions. Loren basically showed how we can use a combination of the built-in <i><b>nargin</b></i> and <i><b>exist</b></i> functions to check whether a specified function-name is a regular m-file, a script filename, a class name or something else.<br />
Today I will discuss several additional alternatives for retrieving information about a specified function:</p>
<ul>
<li><a href="/articles/function-definition-meta-info/#mtree"><i><b>mtree</b></i></a></li>
<li><a href="/articles/function-definition-meta-info/#getcallinfo"><i><b>getcallinfo</b></i></a></li>
<li><a href="/articles/function-definition-meta-info/#mlint"><i><b>mlint</b></i></a></li>
<li><a href="/articles/function-definition-meta-info/#which"><i><b>which</b></i></a></li>
<li><a href="/articles/function-definition-meta-info/#functions"><i><b>functions</b></i></a></li>
</ul>
<h3 id="mtree">The <i>mtree()</i> alternative</h3>
<p>Reader Mark Brown has <a target="_blank" rel="nofollow" href="http://blogs.mathworks.com/loren/2013/08/26/what-kind-of-matlab-file-is-this/#comment-38909">commented</a> about an alternative, using the semi-documented built-in function <i><b>mtree</b></i>:</p>
<pre lang='matlab'>
>> t = mtree('mtree.m','-file'); t.select(1).kind  % m-class file
ans =
CLASSDEF
>> t = mtree('profile.m','-file'); t.select(1).kind  % regular m-file function
ans =
FUNCTION
>> t = mtree('test.m','-file'); t.select(1).kind  % script file
ans =
EXPR
</pre>
<p><i><b>mtree</b></i> contains numerous other goodies in its reported parse-tree, including information about who-calls-what-where, set/used info about variables and other information used in the lexical parsing of the m-file. <i><b>mtree</b></i> is a very sophisticated m-file function (or rather, a class), but is very heavily documented internally, and so it can definitely be read and followed. Unfortunately, much of the internal processing is carried out by opaque internal c-functions (<i>mtreemex</i>), but there is still plenty of processing left for the 3K+ lines of m-code. I plan to write an extensive article about this function one day.<br />
Unlike many other internal m-files, which are much simpler, less well written and sparsely documented (if at all), but for which personal credit is included in a comment, <i><b>mtree</b></i> includes no personal credit although it appears to be very well written and documented. I have to congratulate the unknown author(s) for both their proficiency and humility. <b><u>Addendum</u></b>: This unknown author is now confirmed to have been <a target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Stephen_C._Johnson">Steve Johnson</a>, author of the <a target="_blank" href="/articles/parsing-mlint-code-analyzer-output/">mlint</a> set of tools and functionality.<br />
For people worried about future compatibility, note that <i><b>mtree</b></i> has existed on the Matlab path (%matlabroot%\toolbox\matlab\codetools\@mtree\mtree.m) since 2006 (I think R2006a, but I&#8217;m not certain; it already had version 1.3 by R2007b; the latest version [R2013b] is 2.5). Considering the large investment in its code thus far, and the significant effort that it would take to create a replacement, I don&#8217;t see this function going away in the near future. Then again, there is no certainty about this, and it might well disappear without notice in some future Matlab release.</p>
<h3 id="getcallinfo">The <i>getcallinfo()</i> alternative</h3>
<p><i><b>getcallinfo</b></i> is another semi-documented internal function, which uses <i><b>mtree</b></i> to construct an m-file&#8217;s call-tree and reports the results in either a tabulated manner (if no output arg is request), or a struct array (as output arg):<br />
<span id="more-4141"></span></p>
<pre lang='java'>
>> getcallinfo('profile.m');
Name           Type             Starts Ends Length Full Name
----           ----             ------ ---- ------ ---------
profile        function             1   242    242 profile
ParseInputs    subfunction        247   354     82 profile>ParseInputs
ParseOption    nested-function    262   287     26 profile>ParseInputs/ParseOption
notifyUI       subfunction        356   370     15 profile>notifyUI
>> s = getcallinfo('profile.m')
s =
1x4 struct array with fields:
    type
    name
    fullname
    functionPrefix
    calls
    firstline
    lastline
    linemask
>> s(4).calls
ans =
      fcnCalls: [1x1 struct]
    innerCalls: [1x1 struct]
      dotCalls: [1x1 struct]
       atCalls: [1x1 struct]
>> s(4).calls.fcnCalls
ans =
    names: {'usejava'}
    lines: 359
>> s(4).calls.innerCalls
ans =
    names: {1x0 cell}
    lines: [1x0 double]
>> s(4).calls.dotCalls
ans =
    names: {'com.mathworks.mde.profiler.Profiler.start'  [1x40 char]  [1x41 char]}
    lines: [363 365 367]
</pre>
<p>Note: In order to use <i><b>getcallinfo</b></i>, we first need to fix a small internal bug in <i>%matlabroot%/toolbox/matlab/codetools/getcallinfo.m</i>, specifically within the <code>displayStructure()</code> sub-function. Modifying this file may require administrator privileges, and can be done in any text editor. The bug is that the keyword <code>length</code> is used as a variable in this function (line #136 in R2013b), and so cannot be used to reference the built-in function <i><b>length()</b></i>. We can either rename the variable (lines 136, 139, 145) or the function. It&#8217;s easiest to rename <i><b>length()</b></i> to <i><b>numel()</b></i> in line #147 (highlighted below):</p>
<pre lang='matlab' highlight='3,11'>
130:   function displayStructure(strc)
           ...
136:       length = getString(message('MATLAB:codetools:reports:RptLength'));
137:       fullName = getString(message('MATLAB:codetools:reports:RptFullName'));
138:
139:       fprintf('%-20s %-20s %-4s %-4s %-6s %-20s\n',name,type,starts,ends,length,fullName);
140:       fprintf('%-20s %-20s %-4s %-4s %-6s %-20s\n', ...
               ...
145:           getDashesForString(length), ...
146:           getDashesForString(fullName));
147:       for n = 1:numel(strc)   % original code: n = 1:length(strc)
               ...
152:       end
153:   end
</pre>
<h3 id="mlint">The <i>mlint()</i> alternative</h3>
<p>A few months ago, I <a target="_blank" href="/articles/parsing-mlint-code-analyzer-output/">wrote</a> about <i><b>mlint</b></i>&#8216;s undocumented interface and ability to report much internal information about the analyzed file. It is no surprise that one of the undocumented <i><b>mlint</b></i> options is <code>-calls</code>, which lists the calling-tree of an m-file. For example:</p>
<pre lang='matlab'>
>> mlint profile.m -calls
M0 1 14 profile
E0 242 3 profile
U1 122 15 callstats
U1 131 11 ParseInputs
U1 133 10 MException
U1 134 5 throw
U1 161 8 lower
U1 164 9 notifyUI
U1 165 23 true
U1 169 23 false
U1 180 9 profreport
U1 183 13 usejava
U1 184 13 error
U1 184 19 message
U1 188 21 profile
U1 189 16 isempty
U1 192 17 profview
U1 236 9 warning
S0 248 7 ParseInputs
E0 354 3 ParseInputs
U1 260 1 error
U1 260 7 nargchk
U1 260 17 Inf
U1 260 21 nargin
N1 262 23 ParseOption
E1 287 7 ParseOption
U2 263 12 strcmp
...
</pre>
<p>In this report, the first character represents the function type:</p>
<ul>
<li>M = main (top-level) function</li>
<li>S = sub-function</li>
<li>N = nested function</li>
<li>U = out-of-scope (external/built-in) function</li>
<li>A = anonymous function</li>
<li>E = end-of-function indication</li>
</ul>
<p>The following numbers indicate the nesting level, line #, column # and function identifier (name). In essence, it&#8217;s the same information presented by <i><b>getcallinfo</b></i>, with two distinctions: <i><b>getcallinfo</b></i>&#8216;s report is much more nicely formatted, and on the other hand <i><b>getcallinfo</b></i> does not include internal function-calls (maybe there&#8217;s an undocumented switch that I haven&#8217;t found for this).<br />
In this regard, I wish to once again praise Urs Schwartz&#8217;s excellent <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/15924-farg-a-pedestrian-m-file-parser-showing-all-used-functions-syntax"><i><b>farg</b></i></a> and <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/17291-fdep-a-pedestrian-function-dependencies-finder"><i><b>fdep</b></i></a> utilities, which use this undocumented <i><b>mlint</b></i> syntax. They seem to out-perform and out-class the built-in <i><b>depends</b></i> function by a wide margin&#8230;</p>
<h3 id="which">The <i>which()</i> alternative</h3>
<p>The built-in <i><b>which</b></i> function can be used to report the file-path of m-file functions, or an indicate that the function is built-in (i.e., coded as a C/C++ function in one of the Matlab libraries):</p>
<pre lang='matlab'>
>> str = which('perfTest')
str =
C:\Yair\Books\MATLAB Performance Tuning\Code\perfTest.m
>> str = which('matlab.io.MatFile')
str =
C:\Program Files\Matlab\R2013b\toolbox\matlab\iofun\+matlab\+io\MatFile.m
>> str = which('sin')
str =
built-in (C:\Program Files\Matlab\R2013b\toolbox\matlab\elfun\@double\sin)
>> str = which('noSuchFunction')
str =
     ''
</pre>
<p>Note: a little-known option enables specifying sub-functions (although this does not work for nested functions for some unknown reason [bug? oversight?]):</p>
<pre lang='matlab'>
>> str = which('nestedFuncName','in','MFileName');
</pre>
<h3 id="functions">The <i>functions()</i> alternative</h3>
<p>Similar functionality can be achieved via the built-in <i><b>functions</b></i>, using function handles rather than function names:</p>
<pre lang='matlab'>
>> functions(@perfTest)
ans =
    function: 'perfTest'
        type: 'simple'
        file: 'C:\Yair\Books\MATLAB Performance Tuning\Code\perfTest.m'
>> functions(@matfile)
ans =
    function: 'matfile'
        type: 'simple'
        file: 'C:\Program Files\Matlab\R2013b\toolbox\matlab\iofun\matfile.m'
>> fType = functions(@(a)a+1)
fType =
     function: '@(a)a+1'
         type: 'anonymous'
         file: ''
    workspace: {[1x1 struct]}
>> functions(@transpose)
ans =
    function: 'transpose'
        type: 'simple'
        file: ''
</pre>
<p>Unlike <i><b>which</b></i>, <i><b>functions</b></i> can also be used for both sub- <i>and</i> nested-functions:</p>
<pre lang='matlab'>
% The following was called within the confines of a specific m-file function:
K>> fType = functions(@MFileName)
fType =
    function: 'MFileName'
        type: 'simple'
        file: 'C:\Yair\MFileName.m'
K>> fType = functions(@subFunc)
fType =
     function: 'subFunc'
         type: 'scopedfunction'
         file: 'C:\Yair\MFileName.m'
    parentage: {'subFunc'  'MFileName'}
K>> fType = functions(@nestedFunc)
fType =
     function: 'MFileName/nestedFunc'
         type: 'nested'
         file: 'C:\Yair\MFileName.m'
    workspace: {[1x1 struct]}
</pre>
<p>Note that <code>parentage</code> and <code>workspace</code> are undocumented sub-fields of the returned struct: they are mentioned in the official <a target="_blank" rel="nofollow" href="http://www.mathworks.com/help/matlab/ref/functions.html">doc page</a>, but only in passing, without a format explanation. Also note that <code>workspace</code> is a cell array of a single element (contrary to the official doc &#8211; this is probably an internal bug), containing the actual workspace as a struct (fields = workspace variables). So it should be accessed as <code>fType.workspace{1}.varName</code>. Note that <code>parentage</code> and <code>workspace</code> are present only for certain types of function types.<br />
Have you found any other nifty ways of retrieving function meta-info in run-time? Are you using such meta-info in an interesting manner? If so, please post a short comment below.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/function-definition-meta-info">Function definition meta-info</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/function-call-timeline-profiling" rel="bookmark" title="Function call timeline profiling">Function call timeline profiling </a> <small>A new utility enables to interactively explore Matlab function call timeline profiling. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/sparse-data-math-info" rel="bookmark" title="Sparse data math info">Sparse data math info </a> <small>Matlab contains multiple libraries for handling sparse data. These can report very detailed internal info. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/math-libraries-version-info-upgrade" rel="bookmark" title="Math libraries version info &amp; upgrade">Math libraries version info &amp; upgrade </a> <small>Matlab exposes undocumented info about its internal math libraries; these libraries can be upgraded manually. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/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>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/function-definition-meta-info/feed</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>Math libraries version info &#038; upgrade</title>
		<link>https://undocumentedmatlab.com/articles/math-libraries-version-info-upgrade?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=math-libraries-version-info-upgrade</link>
					<comments>https://undocumentedmatlab.com/articles/math-libraries-version-info-upgrade#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 17 Jul 2013 18:00:36 +0000</pubDate>
				<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Semi-documented function]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3998</guid>

					<description><![CDATA[<p>Matlab exposes undocumented info about its internal math libraries; these libraries can be upgraded manually. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/math-libraries-version-info-upgrade">Math libraries version info &amp; upgrade</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/sparse-data-math-info" rel="bookmark" title="Sparse data math info">Sparse data math info </a> <small>Matlab contains multiple libraries for handling sparse data. These can report very detailed internal info. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/function-definition-meta-info" rel="bookmark" title="Function definition meta-info">Function definition meta-info </a> <small>There are multiple ways to retrieve meta-info about functions in Matlab. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-infiniband-with-matlab-parallel-computing-toolbox" rel="bookmark" title="Using Infiniband with Matlab Parallel Computing Toolbox">Using Infiniband with Matlab Parallel Computing Toolbox </a> <small>Infiniband networking can significantly improve PCT performance in Matlab parallelization and distributed computing. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/ip-address-input-control" rel="bookmark" title="IP address input control">IP address input control </a> <small>A built-in JIDE control can be used in Matlab GUI for IP-address entry/display. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><i><b>version</b></i> is a well-known Matlab function, which displays and returns information about the installed release:</p>
<pre lang='matlab'>
>> [v,d] = version
v =
8.1.0.604 (R2013a)
d =
February 15, 2013
>> version('-java')
ans =
Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
</pre>
<p>This is pretty boring stuff. But it starts to get interesting when we learn the undocumented fact that <i><b>version</b></i> can also report version information of the installed math libraries, starting in R2009b (Matlab 7.9):<br />
<span class="alignright"><img loading="lazy" decoding="async" alt="FFTW logo" src="https://undocumentedmatlab.com/images/fftw-logo-med.png" title="FFTW logo" width="165" height="54" /><br /><img loading="lazy" decoding="async" alt="LAPACK logo" src="https://undocumentedmatlab.com/images/Lapack-logo.png" title="LAPACK logo" width="165" height="140" /></span></p>
<pre lang='matlab'>
% R2013a (Matlab 8.1) on Win7 64b
>> version('-fftw')
ans =
FFTW-3.3.1-sse2-avx
>> version('-blas')
ans =
Intel(R) Math Kernel Library Version 10.3.11 Product Build 20120606 for Intel(R) 64 architecture applications
>> version('-lapack')
ans =
Intel(R) Math Kernel Library Version 10.3.11 Product Build 20120606 for Intel(R) 64 architecture applications
Linear Algebra PACKage Version 3.4.1
</pre>
<p>This information is sometimes useful, and is <a target="_blank" rel="nofollow" href="https://www.mathworks.com/matlabcentral/newsreader/view_thread/327906">sometimes asked</a> by users. Specific versions of <a target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms">BLAS</a>, <a target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/LAPACK">LAPACK</a> and <a target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/FFTW">FFTW</a>, which Matlab uses under its hood for all linear algebra and FFW computations, may exhibit idiosyncrasies that may be important in certain cases.<br />
<span id="more-3998"></span><br />
In versions up to R2013a, this information could also be retrieved using the <i><b>internal.matlab.language.versionPlugins.*</b></i> mex functions (e.g., <i><b>internal.matlab.language.versionPlugins.blas</b></i>, which runs the mex function <i>%matlabroot%\toolbox\matlab\matfun\+internal\+matlab\+language\+versionPlugins\blas.mexw64</i>). I&#8217;ll have a separate post (or series of posts) on Matlab&#8217;s internal.* functions, but at least with regards to version information they should NOT be relied upon. These interfaces (and their mex files) have changed their name and availability in some Matlab releases. Luckily, the <i><b>version</b></i> format that I showed above seems pretty stable and can be used as-is across multiple Matlab releases.<br />
Here is a summary of the math libraries version in recent Matlab releases (Windows only):</p>
<table border="0">
<thead>
<tr>
<th>Matlab release</th>
<th>FFTW</th>
<th>MKL (BLAS)</th>
<th>LAPACK</th>
</tr>
</thead>
<tr>
<td>R2009b SP1 (7.9.1)</td>
<td>3.2.0-sse2</td>
<td>10.2.2 (2009-08-14)</td>
<td>3.1.1</td>
</tr>
<tr>
<td>R2010a (7.10)</td>
<td>3.2.2-sse2</td>
<td>10.2.2 (2009-08-14)</td>
<td>3.2.1</td>
</tr>
<tr>
<td>R2010b (7.11)</td>
<td>3.2.2-sse2</td>
<td>10.2.3 (2009-11-30)</td>
<td>3.2.1</td>
</tr>
<tr>
<td>R2011a (7.12)</td>
<td>3.2.2-sse2</td>
<td>10.2.6 (2010-07-28)</td>
<td>3.2.2</td>
</tr>
<tr>
<td>R2011b (7.13)</td>
<td>3.2.2-sse2</td>
<td>10.3.2 (2011-01-17)</td>
<td>3.2.2</td>
</tr>
<tr>
<td>R2012a (7.14)</td>
<td>3.2.2-sse2</td>
<td>10.3.5 (2011-07-20)</td>
<td>3.3.1</td>
</tr>
<tr>
<td>R2012b (8.0)</td>
<td>3.3.1-sse2</td>
<td>10.3.9 (2012-01-31)</td>
<td>3.3.1</td>
</tr>
<tr>
<td>R2013a (8.1)</td>
<td>3.3.1-sse2-avx</td>
<td>10.3.11 (2012-06-06)</td>
<td>3.4.1</td>
</tr>
</table>
<p>Note that the LAPACK version is not always updated together with BLAS, although they are both part of Intel&#8217;s MKL (more on MKL below).<br />
<b><u>Addendum 2013-10-9</u></b>: The undocumented <code>-modules</code> option of the <i><b>version</b></i> command provides the list of currently installed libraries:</p>
<pre lang='matlab'>
>> version -modules
C:\Program Files\Matlab\R2013b\bin\win64\libut.dll Version < unknown >
C:\Program Files\Matlab\R2013b\bin\win64\libmwfl.dll Version < unknown >
C:\Program Files\Matlab\R2013b\bin\win64\libmx.dll Version 8.2.0.627
C:\Program Files\Matlab\R2013b\bin\win64\zlib1.dll Version < unknown >
...
</pre>
<p>[/Addendum]<br />
Do you know of any other hidden version info anywhere in Matlab? If so, please post a comment below.</p>
<h3 id="Upgrading">Upgrading library versions</h3>
<p>It may well be possible for users to upgrade the internal libraries that Matlab uses to the latest version, assuming that backward compatibility is preserved in these libraries (which I suppose is the case). Naturally, you can only upgrade if you have a license for the upgraded library. For those interested, the libraries are located in <i>%matlabroot%/bin/%arch%</i>. For example: <i>C:\Program Files\Matlab\R2013b\bin\win64\mkl.dll</i> is Intel&#8217;s <a target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Math_Kernel_Library">MKL</a> (Math Kernel Library), which contains BLAS and LAPACK. Using this example, the <a target="_blank" rel="nofollow" href="http://software.intel.com/en-us/intel-mkl/">latest official version</a> of MKL is 11.0, which <a target="_blank" rel="nofollow" href="http://software.intel.com/en-us/articles/intel-mkl-110-release-notes">promises</a> several important improvements over R2013a&#8217;s bundled version (10.3.11):</p>
<ul>
<li>A <a target="_blank" rel="nofollow" href="http://software.intel.com/en-us/articles/svd-multithreading-bug-in-mkl">fix for a bug</a> in the <i><b>svd</b></i> routines</li>
<li>Improved performance</li>
<li>Improved support for Intel&#8217;s <a target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Advanced_Vector_Extensions">AVX2</a> instruction set (see related <a target="_blank" rel="nofollow" href="https://www.mathworks.com/matlabcentral/newsreader/view_thread/328084#901644">comment</a> by Eric Sampson recently)</li>
<li>Improved functionality of MKL&#8217;s FFT implementation &#8212; Matlab&#8217;s <i><b>*fft</b></i> functions use FFTW, but <a target="_blank" rel="nofollow" href="http://www.fftw.org/speed/">in some cases</a> MKL&#8217;s FFT implementation outperforms FFTW&#8217;s. You may wish to use MKL&#8217;s <a target="_blank" rel="nofollow" href="http://software.intel.com/sites/products/documentation/hpc/mkl/mklman/GUID-A23603DA-B6D1-48AA-90D2-A415302F27D9.htm">FFTW wrapper</a> in such cases rather than calling MKL&#8217;s FFT routines directly.</li>
</ul>
<p>Similarly, FFTW&#8217;s latest stable release is 3.3.3, <a target="_blank" rel="nofollow" href="http://www.fftw.org/release-notes.html">offers advantages</a> compared to R2013a&#8217;s bundled 3.3.1 version. Unfortunately, unlike MKL, FFTW is not bundled as a replaceable library file. I believe that FFTW is directly integrated in Matlab&#8217;s code. However, nothing prevents us from <a target="_blank" rel="nofollow" href="http://www.fftw.org/download.html">downloading</a> the latest library and using it directly.<br />
<span class="alignleft"><img loading="lazy" decoding="async" alt="Warning!" src="https://undocumentedmatlab.com/images/warning.gif" title="Warning!" width="48" height="48" /></span> Needless to say, upgrading specific DLLs in Matlab can be extremely dangerous, and is entirely unsupported by MathWorks. Don&#8217;t come crying if Matlab starts to crash, or even worse &#8211; to return incorrect numerical results. If you design a car with upgraded libs, just let me know please, so that I&#8217;d know not to buy that model&#8230; I don&#8217;t often place such warnings on this blog. After all, this is a blog about undocumented / unsupported stuff. So when I do explicitly warn like this, you should take extra note. Upgrading Matlab&#8217;s math libs is purely an exercise for adventurous engineers who like to blow things up (oh, the sheer pleasure of charting undocumented waters!).<br />
<b><u>Addendum 2013-10-9</u></b>: Amro&#8217;s <a href="/articles/math-libraries-version-info-upgrade/#comment-226776">comment</a> below provides links to several articles that show how we can redirect Matlab to use a different BLAS/LAPACK library than the default (in MKL/ACML), by either setting the <code>BLAS_VERSION</code> environment variable or by updating the <i>blas.spec</i> and <i>lapack.spec</i> files in the libs folder. One user has <a target="_blank" rel="nofollow" href="https://www.mathworks.com/matlabcentral/newsreader/view_thread/129077">reported</a> that in his specific case this achieved a 30% speedup with no change to the code. Note that both of these options only apply to MKL/ACML, and not to other libraries &#8211; in those cases, AFAIK, only a direct replacement of the physical file will work. [/Addendum]</p>
<h3 id="IPP">IPP library</h3>
<p>If you have the Image Processing Toolbox (IPT), then you will also find Intel&#8217;s <a target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Integrated_Performance_Primitives">IPP</a> (Integrated Performance Primitives) library (<i>ippl.dll</i>) in the same folder as <i>mkl.dll</i>. IPP is used by IPT for running some highly-optimized image-processing functions.<br />
<span class="alignright"><img loading="lazy" decoding="async" alt="Intel logo" src="https://undocumentedmatlab.com/images/Intel-logo.png" title="Intel logo" width="200" height="132" /></span> Unfortunately, I could not find a parameter of <i><b>version</b></i> that returns the IPP version used by Matlab. However, we can get IPP&#8217;s version info using the <i><b>ippl</b></i> function, which is a semi-documented function in IPT (i.e., it has detailed help, but for some reason not an official doc page):</p>
<pre lang='matlab'>
% R2013a (Matlab 8.1) on Win7 64b
>> [isIpplEnabled, ipplVersion] = ippl
isIpplEnabled =
     1                     % == true
ipplVersion =
    'ippie9_t.lib 7.0 build 205.58 7.0.205.1054 e9   Apr 18 2011'
</pre>
<p>A few IPP-related aspects that you might find interesting in this regard:</p>
<ul>
<li>Matlab currently (R2013a) uses IPPL 7.0 dated April 2011 &#8211; the <a target="_blank" rel="nofollow" href="http://software.intel.com/en-us/intel-ipp">latest version</a> is 8.0 dated June 26, 2013 (only 3 weeks ago) <a target="_blank" rel="nofollow" href="http://software.intel.com/en-us/articles/intel-ipp-80-library-release-notes">includes</a> significant performance boosts. Note that between 7.0 and 8.0 there was a 7.1 release that <a target="_blank" rel="nofollow" href="http://software.intel.com/en-us/articles/intel-ipp-71-library-whats-new">also had</a> significant improvements, especially for modern CPUs. The 8.0 release changed some function names, so for compatibility with Matlab you might discover that you need to settle for the 7.1 DLL. This could still bring significant benefits.</li>
<li>Matlab currently limits IPP usage to uints, floats and (since R2012b) doubles, although (AFAIK) IPP also supports signed ints. For signed ints Matlab reverts to much slower functional processing, which seems a pity. I&#8217;m not 100% certain that these are indeed supported by IPP for the specific cases in which Matlab currently blocks them, but I think it&#8217;s worth a check &#8211; the performance boost could be worth the effort.</li>
<li>Matlab currently uses IPP only for some of the image-processing functions. It would make sense to use IPP much more widely, e.g. math, stats, convolution, FFT/FIR (I&#8217;m not sure it&#8217;s faster than FFTW, but it may be worth a check) and many others &#8211; IPP is much more versatile than an image processing toolkit (<a target="_blank" rel="nofollow" href="http://software.intel.com/en-us/intel-ipp">read here</a>).</li>
</ul>
<p>Even if we don&#8217;t upgrade the <i>ippl.dll</i> or <i>mkl.dll</i> version, we may still benefit by accessing them directly in Matlab and/or MEX code. They can be used in Matlab just like any other DLL. There are plenty of online resources that can help us program using the IPP/MKL functionalities, even on the <a target="_blank" rel="nofollow" href="https://www.mathworks.com/matlabcentral/newsreader/search_results?dur=all&#038;search_string=tag%3Amkl">Matlab CSSM newsgroup</a>. It seems that quite a few Matlab users try to work directly with these libraries, sometimes perhaps not realizing that they are already pre-bundled within Matlab.<br />
If you have successfully used one of these libraries directly in Matlab, and/or successfully upgraded the libs for some use, please share your experience by posting a comment below.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/math-libraries-version-info-upgrade">Math libraries version info &amp; upgrade</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/sparse-data-math-info" rel="bookmark" title="Sparse data math info">Sparse data math info </a> <small>Matlab contains multiple libraries for handling sparse data. These can report very detailed internal info. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/function-definition-meta-info" rel="bookmark" title="Function definition meta-info">Function definition meta-info </a> <small>There are multiple ways to retrieve meta-info about functions in Matlab. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-infiniband-with-matlab-parallel-computing-toolbox" rel="bookmark" title="Using Infiniband with Matlab Parallel Computing Toolbox">Using Infiniband with Matlab Parallel Computing Toolbox </a> <small>Infiniband networking can significantly improve PCT performance in Matlab parallelization and distributed computing. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/ip-address-input-control" rel="bookmark" title="IP address input control">IP address input control </a> <small>A built-in JIDE control can be used in Matlab GUI for IP-address entry/display. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/math-libraries-version-info-upgrade/feed</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
			</item>
		<item>
		<title>Handle Graphics Behavior</title>
		<link>https://undocumentedmatlab.com/articles/handle-graphics-behavior?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=handle-graphics-behavior</link>
					<comments>https://undocumentedmatlab.com/articles/handle-graphics-behavior#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 06 Mar 2013 18:00:19 +0000</pubDate>
				<category><![CDATA[Handle graphics]]></category>
		<category><![CDATA[Hidden property]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Semi-documented function]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<category><![CDATA[schema]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3665</guid>

					<description><![CDATA[<p>HG behaviors are an important aspect of Matlab graphics that enable custom control of handle functionality. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/handle-graphics-behavior">Handle Graphics Behavior</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/undocumented-scatter-plot-behavior" rel="bookmark" title="Undocumented scatter plot behavior">Undocumented scatter plot behavior </a> <small>The scatter plot function has an undocumented behavior when plotting more than 100 points: it returns a single unified patch object handle, rather than a patch handle for each specific point as it returns with 100 or less points....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/performance-accessing-handle-properties" rel="bookmark" title="Performance: accessing handle properties">Performance: accessing handle properties </a> <small>Handle object property access (get/set) performance can be significantly improved using dot-notation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/handle-object-as-default-class-property-value" rel="bookmark" title="Handle object as default class property value">Handle object as default class property value </a> <small>MCOS property initialization has a documented but unexpected behavior that could cause many bugs in user code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/copyobj-behavior-change-in-hg2" rel="bookmark" title="copyobj behavior change in HG2">copyobj behavior change in HG2 </a> <small>the behavior of Matlab's copyobj function changed in R2014b (HG2), and callbacks are no longer copied. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Matlab&#8217;s Handle Graphics (HG) have been around for ages. Still, to this day it contains many hidden gems. Today I discuss HG&#8217;s <b>Behavior</b> property, which is a standard <a target="_blank" href="/articles/getundoc-get-undocumented-object-properties/">undocumented hidden property</a> of all HG objects.<br />
<b>Behavior</b> is not normally updated directly (although there is absolutely nothing to prevent this), but rather via the <a target="_blank" href="/articles/legend-semi-documented-feature/#Semi-documented">semi-documented</a> built-in accessor functions <i><b>hggetbehavior</b></i> and <i><b>hgaddbehavior</b></i>. This manner of accessing <b>Behavior</b> is similar to its undocumented sibling property <b>ApplicationData</b>, which is accessed by the corresponding <i><b>getappdata</b></i> and <i><b>setappdata</b></i> functions.</p>
<h3 id="usage">Using HB behaviors</h3>
<p><i><b>hggetbehavior</b></i> with no input args displays a list of all pre-installed HG behaviors:</p>
<pre lang='matlab'>
>> hggetbehavior
   Behavior Object Name         Target Handle
   --------------------------------------------
   'Plotedit'...................Any Graphics Object
   'Print'......................Any Graphics Object
   'Zoom'.......................Axes
   'Pan'........................Axes
   'Rotate3d'...................Axes
   'DataCursor'.................Axes and Axes Children
   'MCodeGeneration'............Axes and Axes Children
   'DataDescriptor'.............Axes and Axes Children
   'PlotTools'..................Any graphics object
   'Linked'.....................Any graphics object
   'Brush'......................Any graphics object
</pre>
<p><span id="more-3665"></span><br />
<i><b>hggetbehavior</b></i> can be passed a specific behavior name (or cell array of names), in which case it returns the relevant behavior object handle(s):</p>
<pre lang='matlab'>
>> hBehavior = hggetbehavior(gca, 'Zoom')
hBehavior =
	graphics.zoombehavior
>> hBehavior = hggetbehavior(gca, {'Zoom', 'Pan'})
hBehavior =
	handle: 1-by-2
</pre>
<p>As the name indicates, the behavior object handle controls the behavior of the relevant action. For example, the behavior object for Zoom contains the following properties:</p>
<pre lang='matlab'>
>> hBehavior = hggetbehavior(gca, 'Zoom');
>> get(hBehavior)
       Enable: 1        % settable: true/false
    Serialize: 1        % settable: true/false
         Name: 'Zoom'   % read-only
        Style: 'both'   % settable: 'horizontal', 'vertical' or 'both'
</pre>
<p>By setting the behavior&#8217;s properties, we can control whether the axes will have horizontal, vertical, 2D or no zooming enabled, regardless of whether or not the toolbar/menu-bar zoom item is selected:</p>
<pre lang='matlab'>
hBehavior.Enable = false;         % or: set(hBehavior,'Enable',false)
hBehavior.Style  = 'horizontal';  % or: set(hBehavior,'Style','horizontal')
</pre>
<p>This mechanism is used internally by Matlab to disable zoom/pan/rotate3d (see <i>%matlabroot%/toolbox/matlab/graphics/@graphics/@zoom/setAllowAxesZoom.m</i> and similarly <i>setAllowAxesPan, setAllowAxesRotate</i>).<br />
At this point, some readers may jump saying that we can already do this via the zoom object handle that is returned by the <i><b>zoom</b></i> function (where the <b>Style</b> property was renamed <b>Motion</b>, but never mind). However, I am just trying to show the general usage. Not all behaviors have similar documented customizable mechanisms. In fact, using behaviors we can control specific behaviors for separate HG handles in the same figure/axes.<br />
For example, we can set a different callback function to different HG handles for displaying a plot data-tip (a.k.a. data cursor). I have explained in the past how to <a target="_blank" href="/articles/controlling-plot-data-tips/">programmatically control data-tips</a>, but doing so relies on the figure <a target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/ref/datacursormode.html"><i><b>datacursormode</b></i></a>, which is figure-wide. If we want to display different data-tips for different plot handles, we would need to add logic into our custom update function that would change the returned string based on the clicked handle. Using HG behavior we can achieve the same goal much easier:</p>
<pre lang='matlab'>
% Use dataCursorLineFcn() for the line data-tip
bh = hggetbehavior(hLine,'DataCursor');
set(bh,'UpdateFcn',@dataCursorLineFcn);
% Use dataCursorAnnotationFcn() for the annotation data-tip
bh = hggetbehavior(hAnnotation,'DataCursor');
set(bh,'UpdateFcn',{@dataCursorAnnotationFcn,extraData});
</pre>
<p>Note: there is also the related semi-documented function <i><b>hgbehaviorfactory</b></i>, which is used internally by <i><b>hggetbehavior</b></i> and <i><b>hgaddbehavior</b></i>. I do not see any need for using <i><b>hgbehaviorfactory</b></i> directly, only <i><b>hggetbehavior</b></i> and <i><b>hgaddbehavior</b></i>.</p>
<h3 id="custom">Custom behaviors</h3>
<p>The standard behavior objects are UDD schema objects (i.e., the old object-oriented mechanism in MATLAB). They are generally located in a separate folder beneath <i>%matlabroot%/toolbox/matlab/graphics/@graphics/</i>. For example, the Zoom behavior object is located in <i>%matlabroot%/toolbox/matlab/graphics/@graphics/@zoombehavior/</i>. These behavior object folders generally contain a <i>schema.m</i> file (that defines the behavior object class and its properties), and a <i>dosupport.m</i> function that returns a logical flag indicating whether or not the behavior is supported for the specified handle. These are pretty standard functions, here is an example:</p>
<pre lang='matlab'>
% Zoom behavior's schema.m:
function schema
% Copyright 2003-2006 The MathWorks, Inc.
pk = findpackage('graphics');
cls = schema.class(pk,'zoombehavior');
p = schema.prop(cls,'Enable','bool');
p.FactoryValue = true;
p = schema.prop(cls,'Serialize','MATLAB array');
p.FactoryValue = true;
p.AccessFlags.Serialize = 'off';
p = schema.prop(cls,'Name','string');
p.AccessFlags.PublicSet = 'off';
p.AccessFlags.PublicGet = 'on';
p.FactoryValue = 'Zoom';
p.AccessFlags.Serialize = 'off';
% Enumeration Style Type
if (isempty(findtype('StyleChoice')))
    schema.EnumType('StyleChoice',{'horizontal','vertical','both'});
end
p = schema.prop(cls,'Style','StyleChoice');
p.FactoryValue = 'both';
</pre>
<pre lang='matlab'>
% Zoom behavior's dosupport.m:
function [ret] = dosupport(~,hTarget)
% Copyright 2003-2009 The MathWorks, Inc.
% axes
ret = ishghandle(hTarget,'axes');
</pre>
<p>All behaviors must define the <b>Name</b> property, and most behaviors also define the <b>Serialize</b> and <b>Enable</b> properties. In addition, different behaviors define other properties. For example, the DataCursor behavior defines the <b>CreateNewDatatip</b> flag and no less than 7 callbacks:</p>
<pre lang='matlab'>
function schema
% Copyright 2003-2008 The MathWorks, Inc.
pk = findpackage('graphics');
cls = schema.class(pk,'datacursorbehavior');
p = schema.prop(cls,'Name','string');
p.AccessFlags.PublicSet = 'off';
p.AccessFlags.PublicGet = 'on';
p.FactoryValue = 'DataCursor';
schema.prop(cls,'StartDragFcn','MATLAB callback');
schema.prop(cls,'EndDragFcn','MATLAB callback');
schema.prop(cls,'UpdateFcn','MATLAB callback');
schema.prop(cls,'CreateFcn','MATLAB callback');
schema.prop(cls,'StartCreateFcn','MATLAB callback');
schema.prop(cls,'UpdateDataCursorFcn','MATLAB callback');
schema.prop(cls,'MoveDataCursorFcn','MATLAB callback');
p = schema.prop(cls,'CreateNewDatatip','bool');
p.FactoryValue = false;
p.Description = 'True will create a new datatip for every mouse click';
p = schema.prop(cls,'Enable','bool');
p.FactoryValue = true;
p = schema.prop(cls,'Serialize','MATLAB array');
p.FactoryValue = true;
p.AccessFlags.Serialize = 'off';
</pre>
<p>Why am I telling you all this? Because in addition to the standard behavior objects we can also specify custom behaviors to HG handles. All we need to do is mimic one of the standard behavior object classes in a user-defined class, and then use <i><b>hgaddbehavior</b></i> to add the behavior to an HG handle. Behaviors are differentiated by their <b>Name</b> property, so we can either use a new name for the new behavior, or override a standard behavior by reusing its name.</p>
<pre lang='matlab'>hgaddbehavior(hLine,myNewBehaviorObject)</pre>
<p>If you wish the behavior to be serialized (saved) to disk when saving the figure, you should add the <b>Serialize</b> property to the class and set it to <code>true</code>, then use <i><b>hgaddbehavior</b></i> to add the behavior to the relevant HG handle. The <b>Serialize</b> property is searched-for by the <i><b>hgsaveStructDbl</b></i> function when saving figures (I described <i><b>hgsaveStructDbl</b></i> <a target="_blank" href="/articles/handle2struct-struct2handle-and-matlab-8/">here</a>). All the standard behaviors except DataDescriptor have the <b>Serialize</b> property (I don&#8217;t know why DataDescriptor doesn&#8217;t).<br />
Just for the record, you can also use MCOS (not just UDD) class objects for the custom behavior, as mentioned by the internal comment within the <i><b>hgbehaviorfactory</b></i> function. Most standard behaviors use UDD schema classes; an example of an MCOS behavior is PlotEdit that is found at <i>%matlabroot%/toolbox/matlab/graphics/+graphics/+internal/@PlotEditBehavor/PlotEditBehavor.m</i>.</p>
<h3 id="ishghandle"><i>ishghandle</i>&#8216;s undocumented type input arg</h3>
<p>Note that the Zoom behavior&#8217;s <i>dosupport</i> function uses an undocumented format of the built-in <i><b>ishghandle</b></i> function, namely accepting a second parameter that specifies a specific handle type, which presumably needs to correspond to the handle&#8217;s <b>Type</b> property:</p>
<pre lang='matlab'>ret = ishghandle(hTarget,'axes');</pre>
<h3 id="hasbehavior">The <i>hasbehavior</i> function</h3>
<p>Another semi-documented built-in function called <i><b>hasbehavior</b></i> is located right next to <i><b>hggetbehavior</b></i> and <i><b>hgaddbehavior</b></i> in the <i>%matlabroot%/toolbox/matlab/graphics/</i> folder.<br />
Despite its name, and the internal comments that specifically mention HG behaviors, this function is entirely independent of the HG behavior mechanism described above, and in fact makes use of the <b>ApplicationData</b> property rather than <b>Behavior</b>. I have no idea why this is so. It may be a design oversight or some half-baked attempt by a Mathworker apprentice to emulate the behavior mechanism. Even the function name is misleading: in fact, <i><b>hasbehavior</b></i> not only checks whether a handle has some &#8220;behavior&#8221; (in the 2-input args format) but also sets this flag (in the 3-input args format).<br />
<i><b>hasbehavior</b></i> is used internally by the legend mechanism, to determine whether or not an HG object (line, scatter group, patch, annotation etc.) should be added to the legend. This can be very important for <a target="_blank" href="/articles/plot-performance/#Legend">plot performance</a>, since the legend would not need to be updated whenever these objects are modified in some manner:</p>
<pre lang='matlab'>
hLines = plot(rand(3,3));
hasbehavior(hLines(1), 'legend', false);   % line will not be in legend
hasbehavior(hLines(2), 'legend', true);    % line will be in legend
</pre>
<p>(for anyone interested, the relevant code that checks this flag is located in <i>%matlabroot%/toolbox/matlab/scribe/private/islegendable.m</i>)<br />
<i><b>hasbehavior</b></i> works by using a dedicated field in the handle&#8217;s <b>ApplicationData</b> struct with a logical flag value (<code>true/false</code>). The relevant field is called <code>[name,'_hgbehavior']</code>, where <code>name</code> is the name of the so-called &#8220;behavior&#8221;. In the example above, it creates a field called &#8220;legend_hgbehavior&#8221;.<br />
Do you know of any neat uses for HG behaviors? If so, please post a comment <a href="/articles/handle-graphics-behavior/#respond">below</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/handle-graphics-behavior">Handle Graphics Behavior</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/undocumented-scatter-plot-behavior" rel="bookmark" title="Undocumented scatter plot behavior">Undocumented scatter plot behavior </a> <small>The scatter plot function has an undocumented behavior when plotting more than 100 points: it returns a single unified patch object handle, rather than a patch handle for each specific point as it returns with 100 or less points....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/performance-accessing-handle-properties" rel="bookmark" title="Performance: accessing handle properties">Performance: accessing handle properties </a> <small>Handle object property access (get/set) performance can be significantly improved using dot-notation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/handle-object-as-default-class-property-value" rel="bookmark" title="Handle object as default class property value">Handle object as default class property value </a> <small>MCOS property initialization has a documented but unexpected behavior that could cause many bugs in user code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/copyobj-behavior-change-in-hg2" rel="bookmark" title="copyobj behavior change in HG2">copyobj behavior change in HG2 </a> <small>the behavior of Matlab's copyobj function changed in R2014b (HG2), and callbacks are no longer copied. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/handle-graphics-behavior/feed</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>Customizing menu items part 1</title>
		<link>https://undocumentedmatlab.com/articles/customizing-menu-items-part-1?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=customizing-menu-items-part-1</link>
					<comments>https://undocumentedmatlab.com/articles/customizing-menu-items-part-1#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 25 Apr 2012 18:14:08 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Semi-documented function]]></category>
		<category><![CDATA[Callbacks]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Menubar]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2897</guid>

					<description><![CDATA[<p>Matlab menus can be customized in a variety of undocumented manners - first article of a series. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-menu-items-part-1">Customizing menu items part 1</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/customizing-menu-items-part-2" rel="bookmark" title="Customizing menu items part 2">Customizing menu items part 2 </a> <small>Matlab menu items can be customized in a variety of useful ways using their underlying Java object. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-menu-items-part-3" rel="bookmark" title="Customizing menu items part 3">Customizing menu items part 3 </a> <small>Matlab menu items can easily display custom icons, using just a tiny bit of Java magic powder. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-listbox-combobox-items" rel="bookmark" title="Customizing listbox/combobox items">Customizing listbox/combobox items </a> <small>Matlab listboxes can be customized using custom Java cell-renderers. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uitree-nodes" rel="bookmark" title="Customizing uitree nodes &#8211; part 1">Customizing uitree nodes &#8211; part 1 </a> <small>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Over the past years, I have not posted articles dealing with menu items. I have shown how to <a target="_blank" href="/articles/modifying-default-toolbar-menubar-actions/">directly access menu items&#8217; hidden handles</a>, but not much more than that. A year ago I <a target="_blank" href="/articles/2010-perspective/">promised</a> a mini-series on menu customizations, and it&#8217;s time to keep my promise. In today&#8217;s article, the first in the mini-series, I will present several undocumented menu customization topics that rely on pure-Matlab (i.e, no Java today). The next article in this series will focus on Java-based customizations.</p>
<h3 id="Invoking">Invoking menu item callbacks</h3>
<p>As noted above, a figure window&#8217;s menu items can be directly accessed. Once we access a menu item&#8217;s handle, we can extract its <b>Callback</b> property and directly invoke it (for example, using the semi-documented <a target="_blank" href="/articles/hgfeval/"><i><b>hgfeval</b></i> function</a>). Note that menu callbacks are kept in <b>Callback</b>, while toolbar callbacks are kept in <b>ClickedCallback</b>.<br />
Menu callbacks generally use internal <a target="_blank" href="/articles/legend-semi-documented-feature/#Semi-documented">semi-documented</a> functions (i.e., having a readable help section but no doc, online help, or official support), which are part of Matlab&#8217;s uitools folder. These functions are specific to each top-level menu tree: <i><b>filemenufcn, editmenufcn, viewmenufcn, insertmenufcn, toolsmenufcn, desktopmenufcn, winmenu</b></i>, and <i><b>helpmenufcn</b></i> implement the figure&#8217;s eight respective top-level menu trees&#8217; callbacks. These functions accept an optional figure handle (otherwise, <i><b>gcbf</b></i> is assumed), followed by a string specifying the specific menu item whose action needs to be run. <i><b>webmenufcn</b></i> implements the Help menu&#8217;s Web Resources sub-menu callbacks in a similar manner.<br />
Use of these functions makes it easy to invoke a menu action directly from our Matlab code: instead of accessing the relevant menu item and invoking its <b>Callback</b>, we simply find out the menu item string in advance and use it directly. For example,</p>
<pre lang='matlab'>
filemenufcn FileClose;
editmenufcn(hFig,'EditPaste');
</pre>
<p><i><b>uimenufcn</b></i> is a related fully-undocumented (built-in) function, available since Matlab R11 (late 1990s). It accepts a figure handle (or the zero [0] handle to indicate the desktop) and action name. For example, the fully-documented <i><b>commandwindow</b></i> function uses the following code to bring the Command Window into focus:</p>
<pre lang='matlab'>uimenufcn(0, 'WindowCommandWindow');</pre>
<h3 id="Uitools">Customizing menus via uitools</h3>
<p><i><b>makemenu</b></i> is another semi-documented uitool function that enables easy creation of hierarchical menu trees with separators and accelerators. It is a simple and effective wrapper for <i><b>uimenu</b></i>. <i><b>makemenu</b></i> is a useful function that has been made obsolete (grandfathered) without any known replacement.<br />
<i><b>makemenu</b></i> accepts four parameters: a figure handle, a char matrix of labels (&#8216;&gt;&#8217; indicating sub item, &#8216;&gt;&gt;&#8217; indicating sub-sub items etc.; &#8216;&amp;&#8217; indicating keyboard shortcut; &#8216;^x&#8217; indicating an accelerator key; &#8216;-&#8216; indicating a separator line), a char matrix of callbacks, and an optional char matrix of tags (empty by default). <i><b>makemenu</b></i> makes use of another semi-documented grandfathered function, <i><b>menulabel</b></i>, to parse the specified label components. <i><b>makemenu</b></i> returns an array of handles of the created <i><b>uimenu</b></i> items:</p>
<pre lang='matlab'>
labels = str2mat('&File', ...    % File top menu
           '>&New^n', ...           % File=>New
           '>&Open', ...            % File=>Open
           '>>Open &document^d', ...    % File=>Open=>doc
           '>>Open &graph^g', ...       % File=>Open=>graph
           '>-------', ...          % File=>separator line
           '>&Save^s', ...          % File=>Save
           '&Edit', ...		% Edit top menu
           '&View', ...		% View top menu
           '>&Axis^a', ...          % View=>Axis
           '>&Selection region^r'); % View=>Selection
calls = str2mat('', ...		% no action: File top menu
           'disp(''New'')', ...
           '', ...			% no action: Open sub-menu
           'disp(''Open doc'')', ...
           'disp(''Open graph'')', ...
           '', ...			% no action: Separator
           'disp(''Save'')', ...
           '', ...			% no action: Edit top menu
           '', ...			% no action: View top menu
           'disp(''View axis'')', ...
           'disp(''View selection region'')');
handles = makemenu(hFig, labels, calls);
set(hFig,'menuBar','none');
</pre>
<p><center><figure style="width: 267px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="A simple figure menu" src="https://undocumentedmatlab.com/images/makemenu.png" title="A simple figure menu" width="267" height="148"/><figcaption class="wp-caption-text">A simple figure menu</figcaption></figure></center></p>
<h3 id="HTML">Customizing menus via HTML</h3>
<p>Since menu items share the same <a target="_blank" href="/articles/html-support-in-matlab-uicomponents/">HTML/CSS support feature</a> as all Java Swing labels, we can specify font size/face/color, bold, italic, underline, superscript/subscript, and practically any HTML formatting.<br />
Note that some features, such as the font or foreground/background colors, have specific properties that we can set using the Java handle, instead of using HTML. The benefit of using HTML is that it enables setting all the formatting in a single property. HTML does not require using Java – just pure Matlab (see the following example).<br />
Multi-line menu items can easily be done with HTML: simply include a <code>&lt;br&gt;</code> element in the label – the menu item will split into two lines and automatically resize vertically when displayed:</p>
<pre lang='matlab'>
txt1 = '<html><b><u><i>Save</i></u>';
txt2 = '<font color="red"><sup>this file</sup></font></b></html>';
txt3 = '<br />this file as...';
set(findall(hFig,'tag','figMenuFileSave'),   'Label',[txt1,txt2]);
set(findall(hFig,'tag','figMenuFileSaveAs'), 'Label',[txt1,txt3]);
</pre>
<p><center><figure style="width: 185px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="A multi-line HTML-rendered menu item" src="https://undocumentedmatlab.com/images/uimenu2.png" title="A multi-line HTML-rendered menu item" width="185" height="184"/><figcaption class="wp-caption-text">A multi-line HTML-rendered menu item</figcaption></figure></center></p>
<pre lang='matlab'>
set(hMenuItem, 'Label',['<html>&2: C:\My Documents\doc.txt<br />'
   '<font size="-1" face="Courier New" color="red">&nbsp;&nbsp; '
   'Date: 15-Jun-2011 13:23:45<br />&nbsp;&nbsp; Size: 123 KB</font></html>']);
</pre>
<p><center><figure style="width: 411px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="HTML-rendered menu items" src="https://undocumentedmatlab.com/images/uimenu2b.png" title="HTML-rendered menu items" width="411" height="246"/><figcaption class="wp-caption-text">HTML-rendered menu items</figcaption></figure></center><br />
Much more complex customizations can be achieved using Java. So stay tuned to part 2 of this mini-series&#8230;<br />
Note: Menu customization is explored in depth in section 4.6 of my <a target="_blank" href="/matlab-java-book/">book</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-menu-items-part-1">Customizing menu items part 1</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/customizing-menu-items-part-2" rel="bookmark" title="Customizing menu items part 2">Customizing menu items part 2 </a> <small>Matlab menu items can be customized in a variety of useful ways using their underlying Java object. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-menu-items-part-3" rel="bookmark" title="Customizing menu items part 3">Customizing menu items part 3 </a> <small>Matlab menu items can easily display custom icons, using just a tiny bit of Java magic powder. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-listbox-combobox-items" rel="bookmark" title="Customizing listbox/combobox items">Customizing listbox/combobox items </a> <small>Matlab listboxes can be customized using custom Java cell-renderers. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uitree-nodes" rel="bookmark" title="Customizing uitree nodes &#8211; part 1">Customizing uitree nodes &#8211; part 1 </a> <small>This article describes how to customize specific nodes of Matlab GUI tree controls created using the undocumented uitree function...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/customizing-menu-items-part-1/feed</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>Common javacomponent problems</title>
		<link>https://undocumentedmatlab.com/articles/common-javacomponent-problems?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=common-javacomponent-problems</link>
					<comments>https://undocumentedmatlab.com/articles/common-javacomponent-problems#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 07 Dec 2011 18:00:38 +0000</pubDate>
				<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[<p>The javacomponent function is very useful for placing Java components on-screen, but has a few quirks. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/common-javacomponent-problems">Common javacomponent problems</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/ip-address-input-control" rel="bookmark" title="IP address input control">IP address input control </a> <small>A built-in JIDE control can be used in Matlab GUI for IP-address entry/display. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/editable-combo-box" rel="bookmark" title="Editable combo-box">Editable combo-box </a> <small>Matlab's popup menu (combo-box) control is quite limited in its abilities. This article explains how we can work around these limitations. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>The <i><b>javacomponent</b></i> function, which I described <a target="_blank" href="/articles/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.<br />
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>.<br />
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>
<pre lang='matlab'>
jButton = javax.swing.JButton('click me!');
[jhButton, hContainer] = javacomponent(jButton, [100,100,60,30], hParent);
setappdata(hParent, 'linked_props__', linkprop([hParent,hContainer],'Visible'));
</pre>
<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;<br />
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="/articles/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="/articles/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.<br />
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.<br />
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>
<pre lang='matlab'>
jButton = javax.swing.JButton('click me!');
[jhButton, hContainer] = javacomponent(jButton, [100,100,60,30], gcf);
set(hContainer, 'Units','norm');
</pre>
<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="/articles/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.<br />
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.<br />
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="/articles/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="/articles/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.<br />
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.<br />
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>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/common-javacomponent-problems">Common javacomponent problems</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/ip-address-input-control" rel="bookmark" title="IP address input control">IP address input control </a> <small>A built-in JIDE control can be used in Matlab GUI for IP-address entry/display. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/editable-combo-box" rel="bookmark" title="Editable combo-box">Editable combo-box </a> <small>Matlab's popup menu (combo-box) control is quite limited in its abilities. This article explains how we can work around these limitations. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/common-javacomponent-problems/feed</wfw:commentRss>
			<slash:comments>29</slash:comments>
		
		
			</item>
		<item>
		<title>Types of undocumented Matlab aspects</title>
		<link>https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=types-of-undocumented-matlab-aspects</link>
					<comments>https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects#respond</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 24 Nov 2011 18:00:36 +0000</pubDate>
				<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Semi-documented feature]]></category>
		<category><![CDATA[Semi-documented function]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Undocumented function]]></category>
		<category><![CDATA[Hidden property]]></category>
		<category><![CDATA[Internal component]]></category>
		<category><![CDATA[JMI]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<category><![CDATA[Undocumented property]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2534</guid>

					<description><![CDATA[<p>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects">Types of undocumented Matlab aspects</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/reasons-for-undocumented-matlab-aspects" rel="bookmark" title="Reasons for undocumented Matlab aspects">Reasons for undocumented Matlab aspects </a> <small>There are many reasons for the numerous undocumented aspects in Matlab - this article explains them....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/undocumented-plot-marker-types" rel="bookmark" title="Undocumented plot marker types">Undocumented plot marker types </a> <small>Undocumented plot marker styles can easily be accesses using a hidden plot-line property. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/setting-class-property-types-2" rel="bookmark" title="Setting class property types &#8211; take 2">Setting class property types &#8211; take 2 </a> <small>R2016a saw the addition of class property types. However, a better alternative has existed for many years. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/setting-class-property-types" rel="bookmark" title="Setting class property types">Setting class property types </a> <small>Matlab's class properties have a simple and effective mechanism for setting their type....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Why are there so many undocumented aspects in Matlab?<br />
This is a great question, recently <a target="_blank" href="/articles/guide-customization/#comment-61578">asked</a> by a reader of this blog, so I wanted to expand on it in next week&#8217;s article. Before specifying the different reasons, let&#8217;s map the nature of undocumented aspects that we find in Matlab.<br />
The term <i>undocumented/unsupported</i> (as opposed to <i>mis-documentated</i> or <i>deprecated</i>) actually refers to quite a large number of different types.<br />
In the following list, the hyperlinks on the list-item titles lead to a list of corresponding articles on this website:</p>
<ul>
<li><b><a target="_blank" href="/articles/tag/undocumented-function/">Undocumented functions</a></b><br />
Matlab functions which appears nowhere in the documentation, are usually built-in functions (do not have an m-file) and can only be inferred from online CSSM posts or usage within one of the Matlab m-functions installed with Matlab (the latter being the usual case). None of these functions is officially supported by MathWorks. <a target="_blank" href="/articles/category/mex/">MEX</a> is an important source for such functions.
</li>
<li><b><a target="_blank" href="/articles/tag/semi-documented-function/">Semi-documented functions</a></b><br />
Matlab functionality which exists in Matlab m-functions installed with Matlab, but have their main comment separated from the H1 comment line, thereby hiding it from normal view (via Matlab&#8217;s <i><b>help</b></i> function). The H1 comment line itself is simply a standard warning that this function is not officially supported and may change in some future version. To see the actual help comment, simply edit the function (using Matlab&#8217;s <i><b>edit</b></i> function or any text editor) and place a comment sign (%) at the empty line between the H1 comment and the actual help section. The entire help section will then onward be visible via the <i><b>help</b></i> function:</p>
<pre lang='matlab'>
        function [tree, container] = uitree(varargin)
        % WARNING: This feature is not supported in MATLAB
        % and the API and functionality may change in a future release.
fix =>  %
        % UITREE creates a uitree component with hierarchical data in a figure window.
        %   UITREE creates an empty uitree object with default property values in
        %   a figure window.
        %...
</pre>
<p>These functions are not documented in the full documentation (via Matlab&#8217;s <i><b>doc</b></i> function, or online). The odd thing is that some of these functions may appear in the category help output (for example, <i><b>help</b>(&#8216;uitools&#8217;)</i>), and in some cases may even have a fully-visible help section (e.g., <i><b>help</b>(&#8216;setptr&#8217;)</i>), but do not have any online help documentation (<i><b>docsearch</b>(&#8216;setptr&#8217;)</i> fails, and <i><b>doc</b>(&#8216;setptr&#8217;)</i> simply displays the readable help text).<br />
All these functions are officially unsupported by MathWorks, even when having a readable help section. The rule of thumb appears to be that a Matlab function is supported only if it has online documentation. Note, however, that in some rare cases a documentation discrepancy may be due to a MathWorks documentation error, not to unsupportability&#8230;
</li>
<li><b><a target="_blank" href="/articles/tag/undocumented-function/">Helper functions</a></b><br />
Many fully-supported Matlab functions use helper functions that have a specific use in the main (documented) function(s).  Often, these helper functions are tightly-coupled to their documented parents and are useless as stand-alone functions. But quite a few of them have quite useful stand-alone use, as I&#8217;ve already shown in some past articles.
</li>
<li><b><a target="_blank" href="/articles/tag/undocumented-feature/">Undocumented features</a> and <a target="_blank" href="/articles/tag/undocumented-property/">properties</a></b><br />
Features of otherwise-documented Matlab functions, which appear nowhere in the official documentation. You guessed it – these are also not supported by MathWorks&#8230; Like undocumented functions, you can only infer such features by the occasional CSSM post or a reference somewhere in Matlab&#8217;s m-code.
</li>
<li><b><a target="_blank" href="/articles/tag/semi-documented-feature/">Semi-documented features</a></b><br />
Features of otherwise-documented Matlab functions, which are documented in a separate section beneath the main help section, and nowhere else (not in the full doc not the online documentation). If you did not know in advance that these features existed, you could only learn of them by manually looking at Matlab&#8217;s m-files (which is what I do in most cases&#8230;).
</li>
<li><b><a target="_blank" href="/articles/tag/undocumented-property/">Undocumented properties</a></b><br />
Many Matlab objects have internal properties, which can be retrieved (via Matlab&#8217;s <i><b>get</b></i> function) and/or set (via the <i><b>set</b></i> function) programmatically. All these properties are fully documented. Many objects also possess hidden properties, some of which are very interesting and useful, but which are undocumented and (oh yes) unsupported. Like undocumented features, they can only be inferred from CSSM or existing code. In a recent <a target="_blank" href="/articles/getundoc-get-undocumented-object-properties/">article</a> I described my <i><b>getundoc</b></i> utility, which lists these undocumented properties of specified objects.
</li>
<li><b><a target="_blank" href="/articles/tag/internal-component/">Internal Matlab classes</a></b><br />
Matlab uses a vast array of specialized Java classes to handle everything from algorithms to GUI. These classes are (of course) undocumented/unsupported. They can often be accessed directly from the Matlab Command Window or user m-files. GUI classes can be inferred by inspecting the figure frame&#8217;s Java components, and non-GUI classes can often be inferred from references in Matlab&#8217;s m-files.
</li>
<li><b><a target="_blank" href="/articles/tag/JMI">Matlab-Java integration</a></b><br />
Matlab&#8217;s GUI interface, as well as the Java-to-Matlab interface (JMI) is fully undocumented and unsupported. In addition to JMI, there are other mechanisms to run Matlab code from within Java (namely JMI, COM and DDE) &#8211; these are all unsupported and by-and-large undocumented.
</li>
<li><b><a target="_blank" href="/?s=UDD">Matlab&#8217;s UDD mechanism</a></b><br />
UDD (Unified Data Definition?) is used extensively in Matlab as the internal object-oriented mechanism for describing object properties and functionalities. We can use UDD for a wide variety of uses. UDD was described in a series of articles here in early 2011.
</li>
</ul>
<p>Next week I will list the reasons that cause MathWorks to decide whether a particular feature or property should be documented or not.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects">Types of undocumented Matlab aspects</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/reasons-for-undocumented-matlab-aspects" rel="bookmark" title="Reasons for undocumented Matlab aspects">Reasons for undocumented Matlab aspects </a> <small>There are many reasons for the numerous undocumented aspects in Matlab - this article explains them....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/undocumented-plot-marker-types" rel="bookmark" title="Undocumented plot marker types">Undocumented plot marker types </a> <small>Undocumented plot marker styles can easily be accesses using a hidden plot-line property. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/setting-class-property-types-2" rel="bookmark" title="Setting class property types &#8211; take 2">Setting class property types &#8211; take 2 </a> <small>R2016a saw the addition of class property types. However, a better alternative has existed for many years. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/setting-class-property-types" rel="bookmark" title="Setting class property types">Setting class property types </a> <small>Matlab's class properties have a simple and effective mechanism for setting their type....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Javacomponent background color</title>
		<link>https://undocumentedmatlab.com/articles/javacomponent-background-color?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=javacomponent-background-color</link>
					<comments>https://undocumentedmatlab.com/articles/javacomponent-background-color#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 12 Oct 2011 14:39:43 +0000</pubDate>
				<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[<p>This article explains how to align Java component background color with a Matlab color.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/javacomponent-background-color">Javacomponent background color</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background" rel="bookmark" title="Customizing figure toolbar background">Customizing figure toolbar background </a> <small>Setting the figure toolbar's background color can easily be done using just a tiny bit of Java magic powder. This article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/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>
</ol>
</div>
]]></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="/articles/javacomponent/">described here</a> last year).<br />
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>
<pre lang='matlab'>
hPanel = uipanel('units','pixe', 'pos',[30,30,200,100], 'title','Matlab uipanel');
jLabel = javax.swing.JLabel('Java Label');
[jhlabel,jContainer]=javacomponent(jLabel, [250,50,100,50], gcf);
</pre>
<p><center><figure style="width: 394px" class="wp-caption aligncenter"><img decoding="async" alt="Javacomponents use different default background color than figures" src="https://undocumentedmatlab.com/images/javacomponent_bgcolor.png" title="Javacomponents use different default background color than figures" width="394" /><figcaption class="wp-caption-text">Javacomponents use different default background color than figures</figcaption></figure></center><br />
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>
<pre lang='matlab'>
% Fix the Java component:
% Variant #1
bgcolor = get(gcf, 'Color');
jLabel.setBackground(java.awt.Color(bgcolor(1),bgcolor(2),bgcolor(3)));
% Variant #2
bgcolor = num2cell(get(gcf, 'Color'));
jLabel.setBackground(java.awt.Color(bgcolor{:}));
% Fix the Matlab panel uicontrol
set(hPanel, 'BackgroundColor', get(gcf,'Color'));
</pre>
<p><center><figure style="width: 394px" class="wp-caption aligncenter"><img decoding="async" alt="controls with fixed background colors" src="https://undocumentedmatlab.com/images/javacomponent_bgcolor2.png" title="controls with fixed background colors" width="394" /><figcaption class="wp-caption-text">controls with fixed background colors</figcaption></figure></center></p>
<h3 id="Option2">Changing the figure&#8217;s color to the <i><b>javacomponent</b></i>&#8216;s background color</h3>
<pre lang='matlab'>
bgcolor = jLabel.getBackground.getComponents([]);
set(gcf, 'Color', bgcolor(1:3));
</pre>
<p><center><figure style="width: 394px" class="wp-caption aligncenter"><img decoding="async" alt="figure with modified color to match the controls" src="https://undocumentedmatlab.com/images/javacomponent_bgcolor3.png" title="figure with modified color to match the controls" width="394" /><figcaption class="wp-caption-text">figure with modified color to match the controls</figcaption></figure></center><br />
Look at the list of related posts below for other articles related to colors in Matlab.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/javacomponent-background-color">Javacomponent background color</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background" rel="bookmark" title="Customizing figure toolbar background">Customizing figure toolbar background </a> <small>Setting the figure toolbar's background color can easily be done using just a tiny bit of Java magic powder. This article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/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>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/javacomponent-background-color/feed</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
