<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Undocumented Matlab &#187; Toolbox</title> <atom:link href="http://undocumentedmatlab.com/blog/tag/toolbox/feed/" rel="self" type="application/rss+xml" /><link>http://undocumentedmatlab.com</link> <description>Charting Matlab's unsupported hidden underbelly</description> <lastBuildDate>Thu, 17 May 2012 12:01:26 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.1</generator> <item><title>Solving a MATLAB bug by subclassing</title><link>http://undocumentedmatlab.com/blog/solving-a-matlab-bug-by-subclassing/</link> <comments>http://undocumentedmatlab.com/blog/solving-a-matlab-bug-by-subclassing/#comments</comments> <pubDate>Sun, 07 Feb 2010 23:57:25 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Toolbox]]></category> <category><![CDATA[Matt Whitaker]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[Undocumented property]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1110</guid> <description><![CDATA[Matlab's Image Processing Toolbox's impoint function contains an annoying bug that can be fixed using some undocumented properties.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/docs-of-old-matlab-releases/' rel='bookmark' title='Docs of old Matlab releases'>Docs of old Matlab releases</a> <small>MathWorks recently posted archived documentation for many previous Matlab releases...</small></li><li><a
href='http://undocumentedmatlab.com/blog/panel-level-uicontrols/' rel='bookmark' title='Panel-level uicontrols'>Panel-level uicontrols</a> <small>Matlab's uipanel contains a hidden handle to the title label, which can be modified into a checkbox or radio-button control...</small></li><li><a
href='http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/' rel='bookmark' title='Types of undocumented Matlab aspects'>Types of undocumented Matlab aspects</a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>I would like to welcome guest blogger <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/author/94021">Matthew Whitaker</a>. Many of Matt&#8217;s CSSM submissions offer important insight of internal Matlab functionality. As shall be seen by today&#8217;s article and some future submissions, Matt has plenty to share vis-a-vis Matlab&#8217;s undocumented functionality.</i></p><p>In my day-to-day work I make extensive use of MATLAB&#8217;s <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/products/image/">Image Processing Toolbox</a> (IPT). One area of the toolbox that has seen considerable change over the last few releases has been the development of a set of <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/toolbox/images/f17-6011.html">modular tools</a> to aid in GUI design for image processing applications. In this article, I examine a bug in one of those tools to illustrate how we can use the power of subclassing these objects (using an undocumented property) to design a simple and effective workaround.</p><h3>The problem</h3><p>The problem arose as I was refactoring some code that was written in R2006b to R2009b. The code in question uses the <b><i>impoint</i></b> tool on an image along with an associated <b><i>text</i></b> object that moves with the point to display information as it is dragged around the image. At the time of the R2006b release the <b><i>impoint</i></b> tool was written as an API. In R2006b the call to <b><i>impoint</i></b> returns a handle to an <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/hggroup.html"><b><i>hggroup</i></b></a> containing a structure of function handles in its application data under the tag &#8216;API&#8217;. This programming pattern was common before the advent of the new class syntax in MATLAB version 7.6 (R2008a).</p><p>Here is an example of how <b><i>impoint</i></b> would be used in R2006b:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> impointBehavior_R2006b
<span style="color: #228B22;">%IMPOINTBEHAVIOR_R2006B shows how impoint would be used in R2006b</span>
<span style="color: #228B22;">%Note: RUN UNDER R2006B (will run under R2009b but actually uses</span>
<span style="color: #228B22;">%classdef impoint so it will show the same issue)</span>
&nbsp;
  <span style="color: #228B22;">% Display the image in a figure window</span>
  <span style="color: #0000FF;">figure</span>;  imshow<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'rice.png'</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% In R2006b calling impoint returns the hggroup handle</span>
  h = impoint<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">200</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% In 2006b iptgetapi returns a structure of function handles</span>
  api = iptgetapi<span style="color: #080;">&#40;</span>h<span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% Add a new position callback to set the text string</span>
  api.<span style="">addNewPositionCallback</span><span style="color: #080;">&#40;</span>@newPos_Callback<span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% Construct boundary constraint function so we can't go outside the axes</span>
  fcn = makeConstrainToRectFcn<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'impoint'</span>,<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'XLim'</span><span style="color: #080;">&#41;</span>,<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'YLim'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
  api.<span style="">setDragConstraintFcn</span><span style="color: #080;">&#40;</span>fcn<span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% Fire callback so we get initial text</span>
  newPos_Callback<span style="color: #080;">&#40;</span>api.<span style="">getPosition</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #0000FF;">function</span> newPos_Callback<span style="color: #080;">&#40;</span>newPos<span style="color: #080;">&#41;</span>
    <span style="color: #228B22;">% Display the current point position in a text label</span>
    api.<span style="">setString</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'(%1.0f,%1.0f)'</span>,newPos<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>,newPos<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%newPos_Callback</span>
&nbsp;
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%impointBehavior_R2006b</span></pre></div></div><p>The code above, when run in R2006b, produces the desired behavior of displaying a text object containing the point coordinates that moves around with the point as it is dragged around the axes.</p><p>In R2009b, <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/toolbox/images/impoint.html"><b><i>impoint</i></b></a> is now a true MATLAB class using the new <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brqy3km-6.html"><b><i>classdef</i></b></a> syntax, so I wanted to update the existing code. Initially this appeared to be a straightforward translation of the code to make use of the new <b><i>impoint</i></b> class syntax. The first attempt to rewrite the code was:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> impointBehavior_R2009b
<span style="color: #228B22;">%IMPOINTBEHAVIOR_R2009B shows the undesirable behavior when</span>
<span style="color: #228B22;">%using the setString method in R2009b. </span>
&nbsp;
  <span style="color: #228B22;">% Display the image in a figure window</span>
  <span style="color: #0000FF;">figure</span>;  imshow<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'rice.png'</span><span style="color: #080;">&#41;</span>;
  h = impoint<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color: #33f;">100</span>,<span style="color: #33f;">200</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% Add a new position callback to set the text string</span>
  h.<span style="">addNewPositionCallback</span><span style="color: #080;">&#40;</span>@newPos_Callback<span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% Construct boundary constraint function so we can't go outside the axes</span>
  fcn = makeConstrainToRectFcn<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'impoint'</span>,<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'XLim'</span><span style="color: #080;">&#41;</span>,<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'YLim'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% Enforce boundary constraint function</span>
  h.<span style="">setPositionConstraintFcn</span><span style="color: #080;">&#40;</span>fcn<span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #228B22;">% Fire callback so we get initial text</span>
  newPos_Callback<span style="color: #080;">&#40;</span>h.<span style="">getPosition</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
&nbsp;
  <span style="color: #0000FF;">function</span> newPos_Callback<span style="color: #080;">&#40;</span>newPos<span style="color: #080;">&#41;</span>
    <span style="color: #228B22;">% Display the current point position in a text label</span>
    h.<span style="">setString</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'(%1.0f,%1.0f)'</span>,newPos<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>,newPos<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%newPos_Callback</span>
&nbsp;
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%impointBehavior_R2009b</span></pre></div></div><p>Unfortunately, when this code is run, dragging the mouse around the axes produces a trail of labels as shown below:<br
/><center><div
class="wp-caption aligncenter" style="width: 305px"><img
alt="Before - buggy behavior" src="http://UndocumentedMatlab.com/images/impoint_before.png" title="Before - buggy behavior" width="295" height="295" /><p
class="wp-caption-text">Before - buggy behavior</p></div></center></p><p>Opening up <b><i>impoint</i></b>.m in the editor and tracing the code revealed that <b><i>impoint</i></b>&#8216;s <i>setString</i> method creates a new <b><i>text</i></b> object each time it is used. I reported this to MATLAB and the bug is now documented on the MathWorks Support site (<a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/bugreports/574846">574846</a>).</p><h3>The solution</h3><p>So how do we work around this bug to get to the behavior we want? One solution would be to rewrite the offending MATLAB code but this is somewhat risky in terms of maintainability and compatibility.</p><p>A more elegant solution is to subclass the <b><i>impoint</i></b> class and substitute the <i>setString</i> behavior we want. Looking at the <b><i>impoint</i></b> code we find that <b><i>impoint</i></b> is a subclass of <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/toolbox/images/imroi.html"><b><i>imroi</i></b></a>. In the <b><i>imroi</i></b> property declarations we see a number of undocumented properties that are protected. We can access these properties in a subclass but not outside the class. One of these undocumented properties is h_group which is an <b><i>hggroup</i></b> that contains the handle graphic objects that make up the <b><i>impoint</i></b> on the screen. The label, when created, becomes part of this <b><i>hggroup</i></b> with its <b>Tag</b> property set to &#8216;label&#8217;. When performing the <i>setString</i> method the behavior we want to see is that if the <b><i>text</i></b> object exists we want to update its <b>String</b> property. If it does not exist we want it to perform its existing functionality:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">classdef</span> impointtextupdate &lt; impoint
<span style="color: #228B22;">%IMPOINTTEXTUPDATE subclasses impoint to override the setString</span>
<span style="color: #228B22;">%method of impoint so that it does not create a new text object</span>
<span style="color: #228B22;">%each time it is called.   </span>
&nbsp;
  <span style="color: #0000FF;">methods</span>
    <span style="color: #0000FF;">function</span> obj = impointtextupdate<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
      obj = obj@impoint<span style="color: #080;">&#40;</span><span style="color: #0000FF;">varargin</span><span style="color: #080;">&#123;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%impointtextupdate</span>
&nbsp;
    <span style="color: #0000FF;">function</span> setString<span style="color: #080;">&#40;</span>obj,str<span style="color: #080;">&#41;</span>
      <span style="color: #228B22;">%override impoint setString</span>
&nbsp;
      <span style="color: #228B22;">%check to see if there is already a text label</span>
      label = <span style="color: #0000FF;">findobj</span><span style="color: #080;">&#40;</span>obj.<span style="">h_group</span>,<span style="color:#A020F0;">'Type'</span>,<span style="color:#A020F0;">'text'</span>,<span style="color:#A020F0;">'Tag'</span>,<span style="color:#A020F0;">'label'</span><span style="color: #080;">&#41;</span>;
&nbsp;
      <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>label<span style="color: #080;">&#41;</span>
        <span style="color: #228B22;">%do the default behavior</span>
        setString@impoint<span style="color: #080;">&#40;</span>obj,str<span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">else</span>
        <span style="color: #228B22;">%update the existing tag</span>
        <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>label<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'String'</span>,str<span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%if</span>
    <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%setString</span>
&nbsp;
  <span style="color: #0000FF;">end</span> <span style="color: #228B22;">%methods</span>
<span style="color: #0000FF;">end</span> <span style="color: #228B22;">%impointtextupdate</span></pre></div></div><p>Substituting calls to <b><i>impoint</i></b> with the new <b><i>impointupdatetext</i></b> subclass now produces the desired effect as shown below:<br
/><center><div
class="wp-caption aligncenter" style="width: 305px"><img
alt="After - expected behavior" src="http://UndocumentedMatlab.com/images/impoint_after.png" title="After - expected behavior" width="295" height="295" /><p
class="wp-caption-text">After - expected behavior</p></div></center></p><h3>Conclusions</h3><p>This case illustrates a couple of points:</p><ul><li>Much of the existing code in the MATLAB toolboxes is being updated to the new object oriented syntax. This presents many opportunities to easily and elegantly modify the default behavior without modifying provided toolbox code In the example above we retain all the desirable behavior of <b><i>impoint</i></b> while overriding the undesirable behavior.</li><li>Many of the properties and methods in the provided toolbox objects are hidden or protected and are undocumented. It takes some simple detective work to find these out through examining the code. MATLAB is very generous in providing much of the existing code openly. Open the functions and classes you use in the editor to really find out how they work. Over the years I&#8217;ve learned and adopted a lot of useful MATLAB programming patterns by examining the code in the various toolboxes (there are a few coding horrors as well!).</li></ul><p>I hope to explore some other under-documented features of the IPT and other toolboxes in future posts.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/docs-of-old-matlab-releases/' rel='bookmark' title='Docs of old Matlab releases'>Docs of old Matlab releases</a> <small>MathWorks recently posted archived documentation for many previous Matlab releases...</small></li><li><a
href='http://undocumentedmatlab.com/blog/panel-level-uicontrols/' rel='bookmark' title='Panel-level uicontrols'>Panel-level uicontrols</a> <small>Matlab's uipanel contains a hidden handle to the title label, which can be modified into a checkbox or radio-button control...</small></li><li><a
href='http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/' rel='bookmark' title='Types of undocumented Matlab aspects'>Types of undocumented Matlab aspects</a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/solving-a-matlab-bug-by-subclassing/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           tag
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-05-21 20:32:27 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/tag/toolbox/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      0.580s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1337657547; expires=Wed, 22-May-2013 03:32:27 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Tue, 22 May 2012 03:32:27 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Tue, 22 May 2012 04:32:27 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               c6ed40e03950ad7d4d858de9127cd324
Content-Encoding:   gzip
-->
