<?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; Stock Matlab function</title> <atom:link href="http://undocumentedmatlab.com/blog/category/stock-matlab-function/feed/" rel="self" type="application/rss+xml" /><link>http://undocumentedmatlab.com</link> <description>Charting Matlab's unsupported hidden underbelly</description> <lastBuildDate>Wed, 08 Feb 2012 18:40:25 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.1</generator> <item><title>Matlab mex in-place editing</title><link>http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing/</link> <comments>http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing/#comments</comments> <pubDate>Wed, 08 Feb 2012 17:00:25 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Memory]]></category> <category><![CDATA[Mex]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Performance]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2699</guid> <description><![CDATA[Editing Matlab arrays in-place can be an important technique for optimizing calculations. This article shows how to do it using Mex.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/undocumented-profiler-options/' rel='bookmark' title='Undocumented profiler options'>Undocumented profiler options</a> <small>The Matlab profiler has some undocumented options that facilitate debugging memory bottlenecks and JIT (Just-In-Time Java compilation) problems....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/' rel='bookmark' title='Matlab-Java memory leaks, performance'>Matlab-Java memory leaks, performance</a> <small>Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/jmi-java-to-matlab-interface/' rel='bookmark' title='JMI &#8211; Java-to-Matlab Interface'>JMI &#8211; Java-to-Matlab Interface</a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/' rel='bookmark' title='Matlab callbacks for Java events'>Matlab callbacks for Java events</a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>I would like to welcome Matlab Mex power-user <a
target="_blank" rel="nofollow" href="http://absurdlycertain.blogspot.com/">Peter Li</a> to a first in a short series of articles about undocumented aspects of Mex programing</i></p><p>Editing Matlab arrays in-place can be an important technique for optimizing calculations, especially when handling data that use large blocks of memory.  The Matlab language itself has some <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/loren/2007/03/22/in-place-operations-on-data/">limited support for in-place editing</a>, but when we are really concerned with speed we often turn to writing C/C++ extensions using the Mex interface.  Unfortunately, editing arrays in-place from Mex extensions is not officially supported in Matlab, and doing it incorrectly can cause data inconsistencies or can even cause Matlab to crash.  In this article, I will introduce the problem and show a simple solution that exhibit the basic implementation details of Matlab&#8217;s internal copy-on-write mechanism.</p><h3 id="Motivation">Why edit in-place?</h3><p>To demonstrate the techniques in this article, I use the <i>fast_median</i> function, which is part of <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/29453-nthelement">my nth_element package</a> on Matlab&#8217;s File Exchange.  You can download the package and play with the code if you want.  The examples are fairly self-explanatory, so if you do not want to try the code you should be okay just following along.</p><p>Let us try a few function calls to see how editing in-place can save time and memory:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; A = <span style="color: #0000FF;">rand</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100000000</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">tic</span>; <span style="color: #0000FF;">median</span><span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>    
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">4.122654</span> seconds.
&nbsp;
&gt;&gt; <span style="color: #0000FF;">tic</span>; fast_median<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">1.646448</span> seconds.
&nbsp;
&gt;&gt; <span style="color: #0000FF;">tic</span>; fast_median_ip<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.927898</span> seconds.</pre></div></div><p>If you try running this, be careful not to make A too large; tune the example according to the memory available on your system. In terms of the execution time for the different functions, your mileage may vary depending on factors such as: your system, what Matlab version you are running, and whether your test data is arranged in a single vector or a multicolumn array.</p><p>This example illustrates a few general points: first, <i>fast_median</i> is significantly faster than Matlab&#8217;s <i><b>native</b></i> median function. This is because <i>fast_median</i> uses a more efficient algorithm; see the nth_element page for more details.  Besides being a shameless plug, this demonstrates why we might want to write a Mex function in the first place: rewriting the median function in pure Matlab would be slow, but using C++ we can significantly improve on the status quo.</p><p>The second point is that the in-place version, <i>fast_median_ip</i>, yields an additional speed improvement.  What is the difference?  Let us look behind the scenes; here are the CPU and memory traces from my system monitor after running the above:</p><p><center><div
class="wp-caption alignleft" style="width: 387px"><img
alt="Memory and CPU usage for median() vs. fast_median_ip()" src="http://UndocumentedMatlab.com/images/median_vs_fast_median_ip.png" title="Memory and CPU usage for median() vs. fast_median_ip()" width="377" height="425"/><p
class="wp-caption-text">Memory and CPU usage for <i><b>median</b></i> vs. <i>fast_median_ip</i></p></div></center></p><p>You can see four spikes in CPU use, and some associated changes in memory allocation:</p><p>The first spike in CPU is when we created the test data vector; memory use also steps up at that time.</p><p>The second CPU spike is the largest; that is Matlab&#8217;s median function.  You can see that over that period memory use stepped up again, and then stepped back down; the median function makes a copy of the entire input data, and then throws its copy away when it is finished; this is expensive in terms of time and resources.</p><p>The <i>fast_median</i> function is the next CPU spike; it has a similar step up and down in memory use, but it is much faster.</p><p>Finally, in the case of <i>fast_median_ip</i> we see something different; there is a spike in CPU use, but memory use stays flat; the in-place version is faster and more memory efficient because it does not make a copy of the input data.</p><div
class="" style="width: 100%; overflow: auto;"></div><p>There is another important difference with the in-place version; it modifies its input array.  This can be demonstrated simply:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; A = randi<span style="color: #080;">&#40;</span><span style="color: #33f;">100</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">10</span> <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&gt;&gt; A'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">39</span>    <span style="color: #33f;">42</span>    <span style="color: #33f;">98</span>    <span style="color: #33f;">25</span>    <span style="color: #33f;">64</span>    <span style="color: #33f;">75</span>     <span style="color: #33f;">6</span>    <span style="color: #33f;">56</span>    <span style="color: #33f;">71</span>    <span style="color: #33f;">89</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">median</span><span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">60</span>
&nbsp;
&gt;&gt; fast_median<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">60</span>
&gt;&gt; A'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">39</span>    <span style="color: #33f;">42</span>    <span style="color: #33f;">98</span>    <span style="color: #33f;">25</span>    <span style="color: #33f;">64</span>    <span style="color: #33f;">75</span>     <span style="color: #33f;">6</span>    <span style="color: #33f;">56</span>    <span style="color: #33f;">71</span>    <span style="color: #33f;">89</span>
&nbsp;
&gt;&gt; fast_median_ip<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">60</span>
&gt;&gt; A'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">39</span>     <span style="color: #33f;">6</span>    <span style="color: #33f;">25</span>    <span style="color: #33f;">42</span>    <span style="color: #33f;">56</span>    <span style="color: #33f;">64</span>    <span style="color: #33f;">75</span>    <span style="color: #33f;">71</span>    <span style="color: #33f;">98</span>    <span style="color: #33f;">89</span></pre></div></div><p>As you can see, all three methods get the same answer, but <i><b>median</b></i> and <i>fast_median</i> do not modify A in the workspace, whereas after running <i>fast_median_ip</i>, input array A has changed.  This is how the in-place method is able to run without using new memory; it operates on the existing array rather than making a copy.</p><h3 id="Pitfalls">Pitfalls with in-place editing</h3><p>Modifying a function&#8217;s input is common in many languages, but in Matlab there are only a few special conditions under which this is officially sanctioned.  This is not necessarily a bad thing; many people feel that modifying input data is bad programming practice and makes code harder to maintain.  But as we have shown, it can be an important capability to have if speed and memory use are critical to an application.</p><p>Given that in-place editing is not officially supported in Matlab Mex extensions, what do we have to do to make it work?  Let us look at the normal, input-copying <i>fast_median</i> function as a starting point:</p><div
class="wp_syntax"><div
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> mexFunction<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> nlhs, mxArray <span style="color: #000040;">*</span>plhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> nrhs, <span style="color: #0000ff;">const</span> mxArray <span style="color: #000040;">*</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
   mxArray <span style="color: #000040;">*</span>incopy <span style="color: #000080;">=</span> mxDuplicateArray<span style="color: #008000;">&#40;</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   plhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> run_fast_median<span style="color: #008000;">&#40;</span>incopy<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>This is a pretty simple function (I have taken out a few lines of boiler plate input checking to keep things clean).  It relies on helper function <i>run_fast_median</i> to do the actual calculation, so the only real logic here is copying the input array <code>prhs[0]</code>.  Importantly, <i>run_fast_median</i> edits its inputs in-place, so the call to <i>mxDuplicateArray</i> ensures that the Mex function is overall well behaved, i.e. that it does not change its inputs.</p><p>Who wants to be well behaved though?  Can we save time and memory just by taking out the input duplication step?  Let us try it:</p><div
class="wp_syntax"><div
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> mexFunction<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> nlhs, mxArray <span style="color: #000040;">*</span>plhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> nrhs, <span style="color: #0000ff;">const</span> mxArray <span style="color: #000040;">*</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
   plhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> run_fast_median<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const_cast</span><span style="color: #000080;">&lt;</span>mxarray <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">// &lt;/mxarray&gt;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>Very bad behavior; note that we cast the original <code>const mxArray*</code> input to a <code>mxArray*</code> so that the compiler will let us mess with it; naughty.</p><p>But does this accomplish edit in-place for <i>fast_median</i>?  Be sure to save any work you have open and then try it:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; mex fast_median_tweaked.<span style="">cpp</span>
&gt;&gt; A = randi<span style="color: #080;">&#40;</span><span style="color: #33f;">100</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span> <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&gt;&gt; fast_median_tweaked<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">43</span></pre></div></div><p>Hmm, it looks like this worked fine.  But in fact there are subtle problems:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; A = randi<span style="color: #080;">&#40;</span><span style="color: #33f;">100</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span> <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&gt;&gt; A'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">65</span>    <span style="color: #33f;">92</span>    <span style="color: #33f;">14</span>    <span style="color: #33f;">26</span>    <span style="color: #33f;">41</span>     <span style="color: #33f;">2</span>    <span style="color: #33f;">45</span>    <span style="color: #33f;">85</span>    <span style="color: #33f;">53</span>     <span style="color: #33f;">2</span>
&gt;&gt; B = A;
&gt;&gt; B'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">65</span>    <span style="color: #33f;">92</span>    <span style="color: #33f;">14</span>    <span style="color: #33f;">26</span>    <span style="color: #33f;">41</span>     <span style="color: #33f;">2</span>    <span style="color: #33f;">45</span>    <span style="color: #33f;">85</span>    <span style="color: #33f;">53</span>     <span style="color: #33f;">2</span>
&nbsp;
&gt;&gt; fast_median_tweaked<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">43</span>
&gt;&gt; A'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">2</span>     <span style="color: #33f;">2</span>    <span style="color: #33f;">41</span>    <span style="color: #33f;">26</span>    <span style="color: #33f;">14</span>    <span style="color: #33f;">45</span>    <span style="color: #33f;">65</span>    <span style="color: #33f;">85</span>    <span style="color: #33f;">53</span>    <span style="color: #33f;">92</span>
&gt;&gt; B'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">2</span>     <span style="color: #33f;">2</span>    <span style="color: #33f;">41</span>    <span style="color: #33f;">26</span>    <span style="color: #33f;">14</span>    <span style="color: #33f;">45</span>    <span style="color: #33f;">65</span>    <span style="color: #33f;">85</span>    <span style="color: #33f;">53</span>    <span style="color: #33f;">92</span></pre></div></div><p>Uhoh, spooky; we expected that running <i>fast_median_tweaked</i> would change input A, but somehow it has also changed B, even though B is supposed to be an independent copy.  Not good.  In fact, under some conditions this kind of uncontrolled editing in-place can crash the entire Matlab environment with a segfault.  What is going on?</p><h3 id="COW">Matlab&#8217;s copy-on-write mechanism</h3><p>The answer is that our simple attempt to edit in-place conflicts with Matlab&#8217;s internal copy-on-write mechanism.  Copy-on-write is an optimization built into Matlab to help avoid expensive copying of variables in memory (actually similar to what we are trying to accomplish with edit in-place).  We can see copy-on-write in action with some simple tests:</p><div
class="wp-caption alignright" style="width: 403px"><img
alt="Matlab's Copy-on-Write memory and CPU usage" src="http://UndocumentedMatlab.com/images/copy-on-write.png" title="Matlab's Copy-on-Write memory and CPU usage" width="393" height="466"/><p
class="wp-caption-text">Matlab's Copy-on-Write memory and CPU usage</p></div><div><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Test #1: update, then copy</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; A = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100000000</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.588937</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000008</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; B = A; <span style="color: #0000FF;">toc</span>   
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000020</span> seconds.
&nbsp;
<span style="color: #228B22;">% Test #2: copy, then update</span>
&gt;&gt; <span style="color: #0000FF;">clear</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; A = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100000000</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.588937</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; B = A; <span style="color: #0000FF;">toc</span>   
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000020</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.678160</span> seconds.</pre></div></div></div><p>In the first set of operations, time and memory are used to create A, but updating A and &#8220;copying&#8221; A into B take no memory and essentially no time.  This may come as a surprise since supposedly we have made an independent copy of A in B; why does creating B take no time or memory when A is clearly a large, expensive block?</p><p>The second set of operations makes things more clear.  In this case, we again create A and then copy it to B; again this operation is fast and cheap.  But assigning into A at this point takes time and consumes a new block of memory, even though we are only assigning into a single index of A.  This is copy-on-write: Matlab tries to save you from copying large blocks of memory unless you need to.  So when you first assign B to equal A, nothing is copied; the variable B is simply set to point to the same memory location already used by A.  Only after you try to change A (or B), does Matlab decide that you really need to have two copies of the large array.</p><p>There are some additional tricks Matlab does with copy-on-write.  Here is another example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">clear</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span> = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100000000</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.573240</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">2</span><span style="color: #080;">&#125;</span> = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100000000</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.560369</span> seconds.
&nbsp;
&gt;&gt; <span style="color: #0000FF;">tic</span>; B = A; <span style="color: #0000FF;">toc</span>                     
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000016</span> seconds.
&nbsp;
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>               
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.690690</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">2</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.695758</span> seconds.
&nbsp;
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000011</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">2</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000004</span> seconds.</pre></div></div><p>This shows that for the purposes of copy-on-write, different elements of cell array A are treated independently.  When we assign B equal to A, nothing is copied.  Then when we change any part of A{1}, that whole element must be copied over.  When we subsequently change A{2}, that whole element must also be copied over; it was not copied earlier.  At this point, A and B are truly independent of each other, as both elements have experienced copy-on-write, so further assignments into either A or B are fast and require no additional memory.</p><p>Try playing with some struct arrays and you will find that copy-on-write also works independently for the elements of structs.</p><h3 id="mxUnshareArray">Reconciling edit in-place with copy-on-write: mxUnshareArray</h3><p>Now it is clear why we cannot simply edit arrays in-place from Mex functions; not only is it naughty, it fundamentally conflicts with copy-on-write.  Naively changing an array in-place can inadvertently change other variables that are still waiting for a copy-on-write, as we saw above when <i>fast_median_tweaked</i> inadvertently changed B in the workspace. This is, in the best case, an unmaintainable mess.  Under more aggressive in-place editing, it can cause Matlab to crash with a segfault.</p><p>Luckily, there is a simple solution, although it requires calling internal, undocumented Matlab functions.</p><p>Essentially what we need is a Mex function that can be run on a Matlab array that will do the following:</p><ol><li>Check whether the current array is sharing data with any other arrays that are waiting for copy-on-write.</li><li>If the array is shared, it must be unshared; the underlying memory must be copied and all the relevant pointers need to be fixed so that the array we want to work on is no longer accessible by anyone else.</li><li>If the array is not currently shared, simply proceed; the whole point is to avoid copying memory if we do not need to, so that we can benefit from the efficiencies of edit in-place.</li></ol><p>If you think about it, this is exactly the operation that Matlab needs to run internally when it is deciding whether an assignment operation requires a copy-on-write.  So it should come as no surprise that such a Mex function already exists in the form of a Matlab internal called <i>mxUnshareArray</i>.  Here is how you use it:</p><div
class="wp_syntax"><div
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">extern</span> <span style="color: #FF0000;">&quot;C&quot;</span> <span style="color: #0000ff;">bool</span> mxUnshareArray<span style="color: #008000;">&#40;</span>mxArray <span style="color: #000040;">*</span>array_ptr, <span style="color: #0000ff;">bool</span> noDeepCopy<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> mexFunction<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> nlhs, mxArray <span style="color: #000040;">*</span>plhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> nrhs, <span style="color: #0000ff;">const</span> mxArray <span style="color: #000040;">*</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
   mxUnshareArray<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const_cast</span><span style="color: #000080;">&lt;</span>mxarray <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">//&lt;/mxarray&gt;</span>
   plhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> run_fast_median<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const_cast</span><span style="color: #000080;">&lt;</span>mxarray <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">//&lt;/mxarray&gt;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>This is the method actually used by <i>fast_median_ip</i> to efficiently edit in-place without risking conflicts with copy-on-write.  Of course, if the array turns out to need to be unshared, then you do not get the benefit of edit in-place because the memory ends up getting copied.  But at least things are safe and you get the in-place benefit as long as the input array is not being shared.</p><h3 id="Extra">Further topics</h3><p>The method shown here should allow you to edit normal Matlab numerical or character arrays in-place from Mex functions safely.  For a Mex function in C rather than C++, omit the &#8220;C&#8221; in the <code>extern</code> declaration and of course you will have to use C-style casting rather than <code>const_cast</code>.  If you need to edit cell or struct arrays in-place, or especially if you need to edit subsections of shared cell or struct arrays safely and efficiently while leaving the rest of the data shared, then you will need a few more tricks.  A good place to get started is <a
target="_blank" rel="nofollow" href="http://www.mk.tu-berlin.de/Members/Benjamin/mex_sharedArrays">this article by Benjamin Schubert</a>.</p><p>Unfortunately, over the last few years Mathworks seems to have decided to make it more difficult for users to access these kinds of internal methods to make our code more efficient.  So be aware of the risk that in some future version of Matlab this method will no longer work in its current form.</p><p>Ultimately much of what is known about <i>mxUnshareArray</i> as well as the internal implementation details of how Matlab keeps track of which arrays are shared goes back to the work of Peter Boettcher, particularly his <a
target="_blank" rel="nofollow" href="http://groups.google.com/group/comp.soft-sys.matlab/browse_thread/thread/c241d8821fb90275/">headerdump.c utility</a>.  Unfortunately, it appears that HeaderDump fails with Matlab releases >=R2010a, as Mathworks have changed some of the internal memory formats &#8211; perhaps some smart reader can pick up the work and adapt HeaderDump to the new memory format.</p><p>In a future article, I hope to discuss headerdump.c and its relevance for copy-on-write and edit in-place, and some other related tools for the latest Matlab releases that do not support HeaderDump.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/undocumented-profiler-options/' rel='bookmark' title='Undocumented profiler options'>Undocumented profiler options</a> <small>The Matlab profiler has some undocumented options that facilitate debugging memory bottlenecks and JIT (Just-In-Time Java compilation) problems....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/' rel='bookmark' title='Matlab-Java memory leaks, performance'>Matlab-Java memory leaks, performance</a> <small>Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/jmi-java-to-matlab-interface/' rel='bookmark' title='JMI &#8211; Java-to-Matlab Interface'>JMI &#8211; Java-to-Matlab Interface</a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/' rel='bookmark' title='Matlab callbacks for Java events'>Matlab callbacks for Java events</a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>MLintFailureFiles or: Why can&#8217;t I save my m-file?!</title><link>http://undocumentedmatlab.com/blog/mlintfailurefiles/</link> <comments>http://undocumentedmatlab.com/blog/mlintfailurefiles/#comments</comments> <pubDate>Thu, 02 Feb 2012 00:14:15 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Editor]]></category> <category><![CDATA[Mlint]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2688</guid> <description><![CDATA[Sometimes Matlab gets into a state where it cannot use a valid m-file. This article explains what can be done.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/recovering-previous-editor-state/' rel='bookmark' title='Recovering previous editor state'>Recovering previous editor state</a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/more-undocumented-timing-features/' rel='bookmark' title='More undocumented timing features'>More undocumented timing features</a> <small>There are several undocumented ways in Matlab to get CPU and clock data...</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-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...</small></li><li><a
href='http://undocumentedmatlab.com/blog/udd-events-and-listeners/' rel='bookmark' title='UDD Events and Listeners'>UDD Events and Listeners</a> <small>UDD event listeners can be used to listen to property value changes and other important events of Matlab objects...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A short while ago, I was working at a client&#8217;s location on some project, when a very strange thing happened: when editing an m-file in the Matlab editor, the mlint process (the one that reports errors and warnings on the right-hand side of the editor) suddenly decided that it has had enough with this specific file. It labeled the entire file as red, meaning that it has errors that prevent it from being run, although as far as we could both see the file was valid and had no syntax errors and actually ran fine.</p><p>Here&#8217;s a screenshot of the message that mlint reported in the editor:</p><p><center><div
class="wp-caption aligncenter" style="width: 542px"><img
alt="erroneous mlint error report" src="http://UndocumentedMatlab.com/images/MLintFailureFiles_mlint_msg.png" title="erroneous mlint error report" width="532" height="44"/><p
class="wp-caption-text">erroneous mlint error report</p></div></center></p><p>Mlint&#8217;s confusion would not have bothered us if it were not for a seemingly-related issue: When we tried to save the file, the Matlab editor would only allow us to save the file under a different name. When we attempted to restart Matlab, the same behavior repeated for this file, and we could not proceed with our actual work, modifying and saving our m-file.</p><p>Not easily intimidated, I tried saving the file under a different name in the editor, then renaming it back outside Matlab. No good, same problem.</p><h3 id="MLintFailureFiles">MLintFailureFiles or: yet another use for <i>prefdir</i></h3><p>The clue that eventually led to the fix was that somehow Matlab remembered the &#8220;faulty&#8221; state of the m-file even after I restarted Matlab. Matlab likes to store persistent cross-session information in a single folder, which is the user&#8217;s <i><b>prefdir</b></i> folder. I have used this to my advantage only a few weeks ago, when I <a
target="_blank" href="http://undocumentedmatlab.com/blog/recovering-previous-editor-state/">explained</a> how to use the stored editor state file that is stored in that folder.</p><p>My hunch was that Matlab might store the mlint&#8217;s &#8220;faulty&#8221; m-file state somewhere in there. I didn&#8217;t have to look very far: one of the files most recently modified had the tell-tale name of <code>MLintFailureFiles</code>. This turns out to be an empty file that contains the full pathname of the &#8220;faulty&#8221; m-files, one file per line.</p><p>The fix turned out to be very easy: simply remove my file from the MLintFailureFiles file (or delete MLintFailureFiles entirely) and restart Matlab. Problem solved – everything back to normal. Resume breathing.</p><h3 id="undocumented">Undocumented? well, not exactly</h3><p>I then went back to the suggestion in the mlint message and true enough I found related information in the documentation, at the bottom of the doc-page for using mlint. This is from R2008a&#8217;s doc page:</p><blockquote><p> <b>About M-Lint and Unexpected MATLAB&reg; Termination</b></p><p>Under some circumstances, when you are editing an M-file, M-Lint can cause the MATLAB session to terminate unexpectedly. The next time you start MATLAB, it displays the following message and disables M-Lint for the M-file that was open in the editor when MATLAB terminated.</p><pre>M-Lint caused your previous MATLAB session to terminate unexpectedly.
Please send this message and file name to The MathWorks.
See "About M-Lint and Unexpected MATLAB Termination" in the MATLAB documentation for details.
</pre><p></p><p>If you want, while waiting for a response from The MathWorks&trade;, you can attempt to reenable M-Lint for the file by following these steps:</p><ol><li>In the Editor, reopen the file that you were editing when MATLAB terminated.</li><li>Remove the lines of code that you believe M-Lint could not handle.</li><li>In a text editor, open the MLintFailureFiles file in your preferences directory. (This is the directory that MATLAB returns when you run prefdir.)</li><li>Save and reopen the M-file.</li></ol></blockquote><p>As you can see, this is not very helpful because it&#8217;s missing the step of actually editing (or deleting) the MLintFailureFiles file.</p><p>Additional documentation of this issue and workaround can be found in two separate Mathworks technical notes: <a
target="_blank" rel="nofollow" href="www.mathworks.com/support/solutions/en/data/1-74JVT3/">here</a> and <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-6ZFY8F/">here</a>.</p><p>A Matlab user has even created a <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/29319">utility</a> on the File Exchange that simply deletes this file, basing his work on the first note above.</p><p>The second note indicates that the problem (and workaround) occur in Matlab releases R2008a-R2010b, and indeed the above-mentioned issue (and workaround) can no longer be found in the latest docs (as far as I could see). However, even if this issue (and MLintFailureFiles) may no longer be relevant in the latest Matlab releases, we may still see MLintFailureFiles in our <i><b>prefdir</b></i>, because the <i><b>prefdir</b></i> contents are automatically copied from the previous release whenever we install a newer Matlab release.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/recovering-previous-editor-state/' rel='bookmark' title='Recovering previous editor state'>Recovering previous editor state</a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/more-undocumented-timing-features/' rel='bookmark' title='More undocumented timing features'>More undocumented timing features</a> <small>There are several undocumented ways in Matlab to get CPU and clock data...</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-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...</small></li><li><a
href='http://undocumentedmatlab.com/blog/udd-events-and-listeners/' rel='bookmark' title='UDD Events and Listeners'>UDD Events and Listeners</a> <small>UDD event listeners can be used to listen to property value changes and other important events of Matlab objects...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/mlintfailurefiles/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Matlab-Java memory leaks, performance</title><link>http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/</link> <comments>http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/#comments</comments> <pubDate>Fri, 20 Jan 2012 00:56:10 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Memory]]></category> <category><![CDATA[Semi-documented feature]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[Performance]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2665</guid> <description><![CDATA[Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-performance/' rel='bookmark' title='Plot performance'>Plot performance</a> <small>Undocumented inner plot mechanisms can be used to significantly improved plotting performance...</small></li><li><a
href='http://undocumentedmatlab.com/blog/datestr-performance/' rel='bookmark' title='datestr performance'>datestr performance</a> <small>Caching is a simple and very effective means to improve code performance, as demonstrated for the datestr function....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matrix-processing-performance/' rel='bookmark' title='Matrix processing performance'>Matrix processing performance</a> <small>Matrix operations performance is affected by internal subscriptions in a counter-intuitive way....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>There are several ways of retrieving information from a Java object into Matlab. On the face of it, all these methods look similar. But it turns out that there are important differences between them in terms of memory leakage and performance.</p><h3 id="Problem">The problem: &#8220;Matlab crashes&#8221; &#8211; now go figure&#8230;</h3><p>A client of one of my Matlab programs recently complained that Matlab crashes after several hours of extensive use of the program. The problem looked like something that is memory related (messages such as Matlab&#8217;s out-of-memory error or Java&#8217;s heap-space error). Apparently this happens even on 64-bit systems having lots of memory, where memory should never be a problem.</p><p>Well, we know that this is only in theory, but in practice Matlab&#8217;s internal memory management has problems that occasionally lead to such crashes. This is one of the reasons, by the way, that recent Matlab releases have added the preference option of increasing the default Java heap space (the previous way to do this was <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-18I2C/">a bit complex</a>). Still, even with a high Java heap space setting and lots of RAM, Matlab crashed after using my program for several hours.</p><p>Not pleasant at all, even a bit of an embarrassment for me. I&#8217;m used to crashing Matlab, but only as a result of my playing around with the internals &#8211; I would hate it to happen to my clients.</p><h3 id="Finding">Finding the leak</h3><p>While we can do little with Matlab&#8217;s internal memory manager, I started searching for the exact location of the memory leak and then to find a way to overcome it. I&#8217;ll save readers the description about the grueling task of finding out exactly where the memory leak occurred in a program that has thousands of lines of code and where events get fired asynchronously on a constant basis. <a
target="_blank" href="http://undocumentedmatlab.com/blog/undocumented-profiler-options/">Matlab Profiler&#8217;s undocumented memory profiling option</a> helped me quite a bit, as well as lots of intuition and trial-and-error. Detecting memory leak is never easy, and I consider myself somewhat lucky this time to have both detected the leak source and a workaround.</p><p>It turned out that the leakage happens in a callback that gets invoked multiple times per second by a Java object (see related articles <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">here</a> and <a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/">here</a>). Each time the Matlab callback function is invoked, it reads the event information from the supplied Java event-data (the callback&#8217;s second input parameter). Apparently, about 1KB of memory gets leaked whenever this event-data is being read. This may appear a very small leak, but multiply this by some 50-100K callback invocations per hour and you get a leakage of 50-100MB/hour. Not a small leak at all; more of a flood you could say&#8230;</p><h3 id="get">Using <i><b>get</b>()</i></h3><p>The leakage culprit turned out to be the following code snippet:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% 160 uSecs per call, with memory leak</span>
eventData  = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hEventData,<span style="color: #080;">&#123;</span><span style="color:#A020F0;">'EventName'</span>,<span style="color:#A020F0;">'ParamNames'</span>,<span style="color:#A020F0;">'EventData'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
eventName  = eventData<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span>;
paramNames = eventData<span style="color: #080;">&#123;</span><span style="color: #33f;">2</span><span style="color: #080;">&#125;</span>;
paramData  = eventData<span style="color: #080;">&#123;</span><span style="color: #33f;">3</span><span style="color: #080;">&#125;</span>.<span style="color: #0000FF;">cell</span>;</pre></div></div><p>In this innocent-looking code, <code>hEventData</code> is a Java object that contains the <b>EventName, ParamNames, EventData</b> properties: <b>EventName</b> is a Java <code>String</code>, that is automatically converted by Matlab&#8217;s <i><b>get</b>()</i> function into a Matlab string (<i><b>char</b></i> array); <b>ParamNames</b> is a Java array of <code>String</code>s, that gets automatically converted into a Matlab cell-array of string; and <b>EventData</b> is a Java array of <code>Object</code>s that needs to be converted into a Matlab cell array using the built-in <i><b>cell</b></i> function, as <a
target="_blank" href="http://undocumentedmatlab.com/blog/converting-java-vectors-to-matlab-arrays/">described</a> in one of my recent articles.</p><p>The code is indeed innocent, works really well and is actually extremely fast: each invocation takes of this code segment takes less than 0.2 millisecs. Unfortunately, because of the memory leak I needed to find a better alternative.</p><h3 id="handle">Using <i><b>handle</b>()</i></h3><p>The first idea was to use the built-in <i><b>handle</b>()</i> function, under the assumption that it would solve the memory leak, as <a
target="_blank" rel="nofollow" href="http://mathforum.org/kb/message.jspa?messageID=5950839">reported here</a>. In fact, MathWorks specifically advises to use <i><b>handle</b>()</i> rather than to work with &#8220;naked&#8221; Java objects, when <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/#memory_leak">setting Java object callbacks</a>. The official documentation of the <i><b>set</b></i> function <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/ref/set.html#f67-433534">says</a>:</p><blockquote><p>Do not use the set function on Java objects as it will cause a memory leak.</p></blockquote><p>It stands to reason then that a similar memory leak happens with <i><b>get</b></i> and that a similar use of <i><b>handle</b></i> would solve this problem:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% 300 uSecs per call, with memory leak</span>
s = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>hEventData<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
eventName  = s.<span style="">EventName</span>;
paramNames = s.<span style="">ParamNames</span>;
paramData  = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>s.<span style="">EventData</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Unfortunately, this variant, although working correctly, still leaks memory, and also performs almost twice as worse than the original version, taking some 0.3 milliseconds to execute per invocation. Looks like this is a dead end.</p><h3 id="accessor">Using Java accessor methods</h3><p>The next attempt was to use the Java object&#8217;s internal accessor methods for the requested properties. These are <code>public</code> methods of the form <i>getXXX(), isXXX(), setXXX()</i> that enable Matlab to treat XXX as a property by its <i><b>get</b></i> and <i><b>set</b></i> functions. In our case, we need to use the getter methods, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% 380 uSecs per call, no memory leak</span>
eventName  = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>hEventData.<span style="">getEventName</span><span style="color: #080;">&#41;</span>;
paramNames = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>hEventData.<span style="">getParamNames</span><span style="color: #080;">&#41;</span>;
paramData  = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>hEventData.<span style="">getEventData</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Here, the method <i>getEventName()</i> returns a Java <code>String</code>, that we convert into a Matlab string using the <i><b>char</b></i> function. In our previous two variants, the <i><b>get</b></i> function did this conversion for us automatically, but when we use the Java method directly we need to convert the results ourselves. Similarly, when we call <i>getParamNames()</i>, we need to use the <i><b>cell</b></i> function to convert the Java <code>String[]</code> array into a Matlab cell array.</p><p>This version at last doesn&#8217;t leak any memory. Unfortunately, it has an even worse performance: each invocation takes almost 0.4 milliseconds. The difference may seem insignificant. However, recall that this callback gets called dozens of times each second, so the total adds up quickly. It would be nice if there were a faster alternative that does not leak any memory.</p><h3 id="struct">Using <i><b>struct</b>()</i></h3><p>Luckily, I found just such an alternative. At 0.24 millisecs per invocation, it is almost as fast as the leaky best-performance original <i><b>get</b></i> version. Best of all, it leaks no memory, at least none that I could detect.</p><p>The mechanism relies on the little-known fact that public fields of Java objects can be retrieved in Matlab using the built-in <i><b>struct</b></i> function. For example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; fields = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="color: #0000FF;">Rectangle</span><span style="color: #080;">&#41;</span>
fields = 
             x<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
             y<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
         width<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
        height<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
      OUT_LEFT<span style="color: #F0F;">:</span> <span style="color: #33f;">1</span>
       OUT_TOP<span style="color: #F0F;">:</span> <span style="color: #33f;">2</span>
     OUT_RIGHT<span style="color: #F0F;">:</span> <span style="color: #33f;">4</span>
    OUT_BOTTOM<span style="color: #F0F;">:</span> <span style="color: #33f;">8</span>
&nbsp;
&gt;&gt; fields = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Dimension</span><span style="color: #080;">&#41;</span>
fields = 
     width<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
    height<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span></pre></div></div><p>Note that this useful mechanism is not mentioned in <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/matlab_external/f4873.html#f46643">the main documentation page for accessing Java object fields</a>, although it is indeed mentioned in <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/matlab_external/f6671.html#f61403">another doc-page</a> &#8211; I guess this is a documentation oversight.</p><p>In any case, I converted my Java object to use public (rather than private) fields, so that I could use this <i><b>struct</b></i> mechanism (Matlab can only access public fields). Yes I know that using private fields is a better programming practice and all that (I&#8217;ve programmed OOP for some 15 years&#8230;), but sometimes we need to do ugly things in the interest of performance. The latest version now looks like this:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% 240 uSecs per call, no memory leak</span>
s = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span>hEventData<span style="color: #080;">&#41;</span>;
eventName  = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>s.<span style="">eventName</span><span style="color: #080;">&#41;</span>;
paramNames = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>s.<span style="">paramNames</span><span style="color: #080;">&#41;</span>;
paramData  = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>s.<span style="">eventData</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>This solved the memory leakage issue for my client. I felt fortunate that I was not only able to detect Matlab&#8217;s memory leak but also find a working workaround without sacrificing performance or functionality.</p><p>In this particular case, I was lucky to have full control over my Java object, to be able to convert its fields to become public. Unfortunately, we do not always have similar control over the object that we use, because they were coded by a third party.</p><p>By the way, Matlab itself uses this <i><b>struct</b></i> mechanism in its code-base. For example, Matlab timers are implemented using Java objects (<code>com.mathworks.timer.TimerTask</code>). The timer callback in Matlab code converts the Java timer event data into a Matlab struct using the <i><b>struct</b></i> function, in <i>%matlabroot%/toolbox/matlab/iofun/@timer/timercb.m</i>. The users of the timer callbacks then get passed a simple Matlab EventData struct without ever knowing that the original data came from a Java object.</p><p>As an interesting corollary, this same <i><b>struct</b></i> mechanism can be used to detect internal properties of Matlab class objects. For example, in the timers again, we can get the underlying timer&#8217;s Java object as follows (note the highlighted warning, which I find a bit ironic given the context):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; timerObj = timerfind
&nbsp;
   Timer Object<span style="color: #F0F;">:</span> timer-<span style="color: #33f;">1</span>
&nbsp;
   Timer Settings
      ExecutionMode<span style="color: #F0F;">:</span> singleShot
             Period<span style="color: #F0F;">:</span> <span style="color: #33f;">1</span>
           BusyMode<span style="color: #F0F;">:</span> drop
            Running<span style="color: #F0F;">:</span> off
&nbsp;
   Callbacks
           TimerFcn<span style="color: #F0F;">:</span> @myTimerFcn
           ErrorFcn<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
           StartFcn<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
            StopFcn<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
&nbsp;
&gt;&gt; timerFields = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span>timerObj<span style="color: #080;">&#41;</span>
<span style="display:block;background-color: #ffc;"><span style="color: #0000FF;">Warning</span><span style="color: #F0F;">:</span> Calling <span style="color: #0000FF;">STRUCT</span> on an object prevents the object from hiding its implementation details and should thus be avoided.</span><span style="display:block;background-color: #ffc;"><span style="">Use</span> <span style="color: #0000FF;">DISP</span> or DISPLAY to see the visible public details of an object. <span style="">See</span> <span style="color:#A020F0;">'help struct'</span> <span style="color: #0000FF;">for</span> <span style="color: #0000FF;">more</span> information.</span><span style="display:block;background-color: #ffc;"><span style="color: #080;">&#40;</span><span style="color: #0000FF;">Type</span> &quot;warning off MATLAB<span style="color: #F0F;">:</span>structOnObject&quot; to suppress this <span style="color: #0000FF;">warning</span>.<span style="color: #080;">&#41;</span></span>timerFields = 
         ud<span style="color: #F0F;">:</span> <span style="color: #080;">&#123;</span><span style="color: #080;">&#125;</span>
    jobject<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 javahandle.<span style="">com</span>.<span style="">mathworks</span>.<span style="">timer</span>.<span style="">TimerTask</span><span style="color: #080;">&#93;</span></pre></div></div><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-performance/' rel='bookmark' title='Plot performance'>Plot performance</a> <small>Undocumented inner plot mechanisms can be used to significantly improved plotting performance...</small></li><li><a
href='http://undocumentedmatlab.com/blog/datestr-performance/' rel='bookmark' title='datestr performance'>datestr performance</a> <small>Caching is a simple and very effective means to improve code performance, as demonstrated for the datestr function....</small></li><li><a
href='http://undocumentedmatlab.com/blog/matrix-processing-performance/' rel='bookmark' title='Matrix processing performance'>Matrix processing performance</a> <small>Matrix operations performance is affected by internal subscriptions in a counter-intuitive way....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/feed/</wfw:commentRss> <slash:comments>11</slash:comments> </item> <item><title>Command Window text manipulation</title><link>http://undocumentedmatlab.com/blog/command-window-text-manipulation/</link> <comments>http://undocumentedmatlab.com/blog/command-window-text-manipulation/#comments</comments> <pubDate>Wed, 28 Dec 2011 18:00:49 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Desktop]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2645</guid> <description><![CDATA[Special control characters can be used to format text output in Matlab's Command Window.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/' rel='bookmark' title='cprintf &#8211; display formatted color text in the Command Window'>cprintf &#8211; display formatted color text in the Command Window</a> <small>cprintf is a utility that utilized undocumented Matlab desktop functionalities to display color and underline-styled formatted text in the Command Window...</small></li><li><a
href='http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors-part2/' rel='bookmark' title='Changing Matlab&#8217;s Command Window colors &#8211; part 2'>Changing Matlab&#8217;s Command Window colors &#8211; part 2</a> <small>The Matlab Command Window enables a limited degree of inline color customization - this post describes how to use it...</small></li><li><a
href='http://undocumentedmatlab.com/blog/editormacro-v2-setting-command-window-key-bindings/' rel='bookmark' title='EditorMacro v2 &#8211; setting Command Window key-bindings'>EditorMacro v2 &#8211; setting Command Window key-bindings</a> <small>The EditorMacro utility was extended to support built-in Matlab Editor and Command-Window actions and key-bindings. This post describes the changes and the implementation details....</small></li><li><a
href='http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/' rel='bookmark' title='Changing Matlab&#8217;s Command Window colors'>Changing Matlab&#8217;s Command Window colors</a> <small>Matlab's Command Window foreground and background colors can be modified programmatically, using some of Matlab's undocumented internal Java classes. Here's how....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Sometimes the most obscure problems have simple solutions.</p><p>A few days ago, a Matlab user <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/315437">asked</a> whether it is possible to solve the flashing effect whenever he cleared the Command Window. It turns out that this user runs a long process, and wanted to display interim information (progress etc.) in the Command Window. The obvious solution that he employed was to display the data in the Command Window, and then erase the window (using <i><b>clc</b></i>) and rewrite the data periodically.</p><h3 id="Solution">Using control characters</h3><p>Now, there are obviously other ways of doing this, the most obvious being a dedicated GUI window that displays the information and updates periodically. But to solve the user&#8217;s immediate issue, the solution I <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/315437#862184">provided</a> (acting on the 80-20 Pareto principle &#8211; a dedicated GUI window is better but would take longer to set up) was to use control characters to erase old data when displaying new data, rather than clearing the window. Here is the basic pseudo-code that I suggested (slightly modified):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">reverseStr = <span style="color:#A020F0;">''</span>;
<span style="color: #0000FF;">for</span> idx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> someLargeNumber
&nbsp;
   <span style="color: #228B22;">% Do some computation here...</span>
&nbsp;
   <span style="color: #228B22;">% Display the progress</span>
   percentDone = <span style="color: #33f;">100</span> * idx / someLargeNumber;
   msg = <span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Percent done: %3.1f'</span>, percentDone<span style="color: #080;">&#41;</span>
   <span style="color: #0000FF;">fprintf</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span>reverseStr, msg<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
   reverseStr = <span style="color: #0000FF;">repmat</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'\b'</span><span style="color: #080;">&#41;</span>, <span style="color: #33f;">1</span>, <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>msg<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>The idea is to use the backspace control-character (<a
target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Backspace">BS</a>, or <i><b>sprintf</b></i>(&#8216;\b&#8217;), or <i><b>char</b></i>(8)) repeatedly in order to erase the preceding characters from the Command Window, then print the new data.</p><p>(see related comments by Helge <a
target="_blank" href="http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/#comment-56187">here</a>)</p><h3 id="Compatibility">Compatibility aspects</h3><p>Back in the good-ol&#8217;-days, when I was hacking away c-shell scripts, ages ago when I created command-prompt C apps, and aeons ago on Vax (R.I.P.), I would have used the carriage-return control-char (<a
target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Carriage_return">CR</a>, or <i><b>sprintf</b></i>(&#8216;\r&#8217;), or <i><b>char</b></i>(13)) to completely erase everything up to the beginning of the current line. This would be much simpler than the repeated backspaces (using <i><b>repmat</b></i> in the pseudo-code above). Unfortunately, CRs behave differently in Matlab&#8217;s Command Window, causing \r to jump to the next line (similarly to \n), rather than erase to the beginning of the line. This is probably due to Java&#8217;s JTextArea&#8217;s implementation (on which the Command Window is based), and not due to Matlab itself. In any case, it shows that not all control characters behave the same way on different environments, which is actually not very surprising.</p><p>Another widely-used control character, the bell control-char (<a
target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Bell_character">BEL</a>, or <i><b>sprintf</b></i>(&#8216;\g&#8217;), or <i><b>char</b></i>(7)) is not accepted at all by Matlab&#8217;s implementation of <i><b>sprintf</b></i> and its variants. Matlab users can always use the <i><b>beep</b></i> function of course, I&#8217;m just pointing this out as another inconsistency between Matlab&#8217;s <i><b>*printf</b></i>() implementation and that of other environments. And don&#8217;t even think of using raw device escape sequences (ah, the good-ol&#8217;-days, long gone now&#8230;).</p><p>Control characters that <i>are</i> accepted in Matlab and behave as expected include: FF (\f), NL (\n) and TAB (\t). These can be used in <i><b>fprintf</b></i> to modify the text spacing. The <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/ref/sprintf.html"><i><b>sprintf</b></i> documentation</a> mentions which of the control characters are accepted, so this is not, strictly speaking, an undocumented aspect. However, note that there is a discrepancy between the list of accepted control chars in the online/<i><b>doc</b></i> page compared to the <i><b>help</b></i> section (\a and \v are mentioned in the former but not in the latter).</p><p>For another type of Command Window text manipulation, namely colors, refer to my <a
target="_blank" href="http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/">cprintf utility</a>. For anyone who hasn&#8217;t noticed, I recently updated the utility on the Matlab File Exchange. Due to some internal modifications by MathWorks to the Command Window implementation in R2011b, <i><b>cprintf</b></i> no longer needs to pad color segments with spaces, and separate color segments can now be directly adjacent to each other (in R2011a and earlier, the spaces are unfortunately needed).</p><p>Have you used control characters in an innovative manner in your work? If so, please share your experience in a <a
target="_blank" href="http://UndocumentedMatlab.com/blog/command-window-text-manipulation/#respond">comment</a>.</p><p>Happy New Year everyone!</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/' rel='bookmark' title='cprintf &#8211; display formatted color text in the Command Window'>cprintf &#8211; display formatted color text in the Command Window</a> <small>cprintf is a utility that utilized undocumented Matlab desktop functionalities to display color and underline-styled formatted text in the Command Window...</small></li><li><a
href='http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors-part2/' rel='bookmark' title='Changing Matlab&#8217;s Command Window colors &#8211; part 2'>Changing Matlab&#8217;s Command Window colors &#8211; part 2</a> <small>The Matlab Command Window enables a limited degree of inline color customization - this post describes how to use it...</small></li><li><a
href='http://undocumentedmatlab.com/blog/editormacro-v2-setting-command-window-key-bindings/' rel='bookmark' title='EditorMacro v2 &#8211; setting Command Window key-bindings'>EditorMacro v2 &#8211; setting Command Window key-bindings</a> <small>The EditorMacro utility was extended to support built-in Matlab Editor and Command-Window actions and key-bindings. This post describes the changes and the implementation details....</small></li><li><a
href='http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/' rel='bookmark' title='Changing Matlab&#8217;s Command Window colors'>Changing Matlab&#8217;s Command Window colors</a> <small>Matlab's Command Window foreground and background colors can be modified programmatically, using some of Matlab's undocumented internal Java classes. Here's how....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/command-window-text-manipulation/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Determining axes zoom state</title><link>http://undocumentedmatlab.com/blog/determining-axes-zoom-state/</link> <comments>http://undocumentedmatlab.com/blog/determining-axes-zoom-state/#comments</comments> <pubDate>Thu, 10 Nov 2011 18:45:38 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2529</guid> <description><![CDATA[The information of whether or not an axes is zoomed or panned can easily be inferred from an internal undocumented object.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/axes-looseinset-property/' rel='bookmark' title='Axes LooseInset property'>Axes LooseInset property</a> <small>Matlab plot axes have an undocumented LooseInset property that sets empty margins around the axes, and can be set to provide a tighter fit of the axes to their surroundings....</small></li><li><a
href='http://undocumentedmatlab.com/blog/tri-state-checkbox/' rel='bookmark' title='Tri-state checkbox'>Tri-state checkbox</a> <small>Matlab checkboxes can easily be made to support tri-state functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/recovering-previous-editor-state/' rel='bookmark' title='Recovering previous editor state'>Recovering previous editor state</a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-liminclude-properties/' rel='bookmark' title='Plot LimInclude properties'>Plot LimInclude properties</a> <small>The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A couple of days ago, a reader of Matlab&#8217;s official Desktop blog <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/desktop/2007/12/10/focused-on-zooming/#comment-8204">asked</a> whether it is possible to determine if an axes has been zoomed or not.</p><p>I have encountered this question myself some time ago, when I tried to customize a radar plot: The grid in radar plots does not automatically re-draw when the plot is zoomed, as in regular rectangular plots. Instead, the radial (angle) and circular (range) grid lines are statically painted when the plot is created, and remain fixed when the plot is zoomed or panned. Therefore, if we zoom in on a small-enough plot segment, we will not see any grid lines at all. It would be beneficial to repaint the grid-lines upon every zoom and pan event. I will not dive into the details today, but the important thing for today&#8217;s article is that the algorithm needed to know whether or not the axes is currently zoomed or not.</p><p>The official response is that this information is not readily available. We could of course store the axes limits somewhere and then compare them to the current limits in run-time. This will cause complications if the plotted data (and thereby the axes limits) change automatically during program use, even without any zooming/panning. It is also problematic if the user resets the zoom limits (as Jiro has correctly <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/desktop/2007/12/10/focused-on-zooming/#comment-8206">pointed out</a>).</p><p>Today I&#8217;ll highlight another possible solution, that perhaps not many Matlab users are familiar with: Apparently, whenever zooming or panning occur, a hidden <b>ApplicationData</b> object is automatically added to the affected axes, with information about the original axes meta-data: limits, aspect ratio, camera info and view angles. This object is also automatically updated whenever we use <b><i>zoom reset</i></b> to reset the zoom limits.</p><p>So basically, all we have to do in run-time is to compare our current access limits with the stored info: if they are the same (or if the app-data object is missing) then the plot is unzoomed and unpanned; otherwise it is. So simple.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">origInfo = <span style="color: #0000FF;">getappdata</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>, <span style="color:#A020F0;">'matlab_graphics_resetplotview'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">if</span> <span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>origInfo<span style="color: #080;">&#41;</span>
   isZoomed = <span style="color: #0000FF;">false</span>;
<span style="color: #0000FF;">elseif</span> isequal<span style="color: #080;">&#40;</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>, origInfo.<span style="color: #0000FF;">XLim</span><span style="color: #080;">&#41;</span> <span style="color: #F0F;">&amp;&amp;</span> <span style="color: #F0F;">...</span>
       <span style="">isequal</span><span style="color: #080;">&#40;</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>, origInfo.<span style="color: #0000FF;">YLim</span><span style="color: #080;">&#41;</span> <span style="color: #F0F;">&amp;&amp;</span> <span style="color: #F0F;">...</span>
       <span style="">isequal</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'ZLim'</span><span style="color: #080;">&#41;</span>, origInfo.<span style="color: #0000FF;">ZLim</span><span style="color: #080;">&#41;</span>
   isZoomed = <span style="color: #0000FF;">false</span>;
<span style="color: #0000FF;">else</span>
   isZoomed = <span style="color: #0000FF;">true</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>This still does not solve the problem of limit changes when the plot data is changed, but it may perhaps be a bit easier to use than manually storing the limits before plotting. (And yes, of course the if-else statement above could be made a one-liner logical construct, but I think this way is more readable).</p><p>Zooming and panning add some additional data to <b>ApplicationData</b>, and also use/modify some other hidden properties of the axes handle (use the <a
target="_blank" href="http://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/"><b><i>getundoc</i></b> function</a> to see them). If you use one of them for any useful purpose, please report your usage in a <a
href="http://undocumentedmatlab.com/blog/determining-axes-zoom-state/#Respond">comment below</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/axes-looseinset-property/' rel='bookmark' title='Axes LooseInset property'>Axes LooseInset property</a> <small>Matlab plot axes have an undocumented LooseInset property that sets empty margins around the axes, and can be set to provide a tighter fit of the axes to their surroundings....</small></li><li><a
href='http://undocumentedmatlab.com/blog/tri-state-checkbox/' rel='bookmark' title='Tri-state checkbox'>Tri-state checkbox</a> <small>Matlab checkboxes can easily be made to support tri-state functionality....</small></li><li><a
href='http://undocumentedmatlab.com/blog/recovering-previous-editor-state/' rel='bookmark' title='Recovering previous editor state'>Recovering previous editor state</a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-liminclude-properties/' rel='bookmark' title='Plot LimInclude properties'>Plot LimInclude properties</a> <small>The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/determining-axes-zoom-state/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Multi-line tooltips</title><link>http://undocumentedmatlab.com/blog/multi-line-tooltips/</link> <comments>http://undocumentedmatlab.com/blog/multi-line-tooltips/#comments</comments> <pubDate>Thu, 03 Nov 2011 02:05:36 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[GUI]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[HTML]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[Tooltip]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2518</guid> <description><![CDATA[Multi-line tooltips are very easy to set up, once you know your way around a few undocumented hiccups.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li><li><a
href='http://undocumentedmatlab.com/blog/multi-column-grid-legend/' rel='bookmark' title='Multi-column (grid) legend'>Multi-column (grid) legend</a> <small>This article explains how to use undocumented axes listeners for implementing multi-column plot legends...</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>I often use tooltips in my Matlab GUIs. They are a fantastically intuitive and unobtrusive visual helper for users to understand the GUI. In this blog, I have already posted several articles about how to tweak tooltip contents (<a
target="_blank" href="http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/">here</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/additional-uicontrol-tooltip-hacks/">here</a>, and <a
target="_blank" href="http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/">here</a>). Today I would like to expand on one of the aspects that were already covered, namely that of multi-line tooltips.</p><h3 id="Basic">Basic multi-line tooltips</h3><p>The basic multi-line tooltip consists of a simple string that includes the newline character. for example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'this is line 1'</span> <span style="color: #33f;">10</span> <span style="color:#A020F0;">'and this is line 2'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p>Or better (platform independent):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'this is line 1\nand this is line 2'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="A simple multi-line tooltip string" src="http://UndocumentedMatlab.com/images/tooltip_multi-line1.png" title="A simple multi-line tooltip string" width="120" /><p
class="wp-caption-text">A simple multi-line tooltip string</p></div></center></p><p>As can be seen, this is very simple to set up. Unfortunately, the font cannot be customized. Which leads us to:</p><h3 id="HTML">HTML-rendered multi-line tooltips</h3><p>Tooltips, like most other Matlab controls, <a
target="_blank" href="http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/">supports HTML rendering</a>. This is a natural corollary of the fact that Java Swing, on which all Matlab controls are based, <a
target="_blank" rel="nofollow" href="http://download.oracle.com/javase/tutorial/uiswing/components/html.html">automatically supports HTML</a>. All we need to do is to add &#8216;<code>&lt;HTML&gt;</code>&#8216; at the very beginning of our tooltip string, and then we can embed HTML tags within the rest of the string:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'tooltipString'</span>, <span style="color:#A020F0;">'&lt;html&gt;&lt;b&gt;line #1&lt;/b&gt;&lt;br /&gt;&lt;i&gt;&lt;font color=&quot;red&quot;&gt;line#2&lt;/font&gt;&lt;/i&gt;&lt;/html&gt;'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Multi-line HTML'ed tooltip" src="http://undocumentedmatlab.com/blog/wp-content/uploads/2009/03/html2.png" title="Multi-line HTML'ed tooltip" width="54" /><p
class="wp-caption-text">Multi-line HTML'ed tooltip</p></div></center></p><p>And a more sophisticated example, used within my <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14225-java-based-data-table">createTable utility</a> on the File Exchange:</p><p><center><div
class="wp-caption aligncenter" style="width: 607px"><img
alt="A more complex multi-line HTML-based tooltip" src="http://UndocumentedMatlab.com/images/table.png" title="A more complex multi-line HTML-based tooltip" width="597" /><p
class="wp-caption-text">A more complex multi-line HTML-based tooltip</p></div></center></p><p>Note that there is no need to close the final HTML tags in the tooltip string, although it&#8217;s not an error to do so (as I have done above).</p><h3 id="Problem">The problem with formatted multi-line tooltip</h3><p>Unfortunately, none of these two methods work when we wish to display formatted multi-line tooltips. For example, suppose that we wish to display struct data in a tooltip:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myData = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'a'</span>,<span style="color: #0000FF;">pi</span>, <span style="color:#A020F0;">'b'</span>,-<span style="color: #33f;">4</span>, <span style="color:#A020F0;">'very_long_field_name'</span>,<span style="color:#A020F0;">'short txt'</span><span style="color: #080;">&#41;</span>
myData = 
                       a<span style="color: #F0F;">:</span> <span style="color: #33f;">3.14159265358979</span>
                       b<span style="color: #F0F;">:</span> -<span style="color: #33f;">4</span>
    very_long_field_name<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'short txt'</span>
&nbsp;
&gt;&gt; myDataStr = <span style="color: #0000FF;">evalc</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'disp(myData)'</span><span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, myDataStr<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Badly-formatted multi-line tooltip" src="http://UndocumentedMatlab.com/images/tooltip_multi-line2.png" title="Badly-formatted multi-line tooltip" width="200" /><p
class="wp-caption-text">Badly-formatted multi-line tooltip</p></div></center></p><p>If we try to use HTML, the result looks even worse, because if the HTML-renderer detects a newline character embedded in the string, it simply uses the regular (non-HTML) renderer:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;'</span> myDataStr <span style="color:#A020F0;">'&lt;/html&gt;'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Failure to parse a string using HTML" src="http://UndocumentedMatlab.com/images/tooltip_multi-line3.png" title="Failure to parse a string using HTML" width="200" /><p
class="wp-caption-text">Failure to parse a string using HTML</p></div></center></p><p>Even if we fix this by replacing all line-separators with &#8216;<code>&lt;br/&gt;</code>&#8216;, we still get a badly-formatted tooltip, because HTML ignores all white spaces:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; myDataStr2 = <span style="color: #0000FF;">strrep</span><span style="color: #080;">&#40;</span>myDataStr, <span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'\n'</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'&lt;br /&gt;'</span><span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;'</span> myDataStr2 <span style="color:#A020F0;">'&lt;/html&gt;'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 210px"><img
alt="Badly-formatted multi-line tooltip" src="http://UndocumentedMatlab.com/images/tooltip_multi-line4.png" title="Badly-formatted multi-line tooltip" width="200" /><p
class="wp-caption-text">Badly-formatted multi-line tooltip</p></div></center></p><p>(you can see that it&#8217;s HTML-formatted by the fact that the tooltip contents do not have internal margins like in the non-HTML tooltip above)</p><h3 id="Fixed-width">Fixed-width font multi-line tooltips</h3><p>We now recall the HTML <code>&lt;pre&gt;</code> tag, which tells HTML not to ignore white-spaces. In most web browsers, it also defaults to using a fixed-width font. Unfortunately, this is not the case here:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;&lt;pre&gt;'</span> myDataStr2<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 250px"><img
alt="Still not quite right..." src="http://UndocumentedMatlab.com/images/tooltip_multi-line5.png" title="Still not quite right..." width="240" /><p
class="wp-caption-text">Still not quite right...</p></div></center></p><p>Which brings us to the final customization of the day, namely explicitly forcing the tooltip to use a fixed-width font:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hControl, <span style="color:#A020F0;">'TooltipString'</span>, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'&lt;html&gt;&lt;pre&gt;&lt;font face=&quot;courier new&quot;&gt;'</span> myDataStr2 <span style="color:#A020F0;">'&lt;/font&gt;'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 330px"><img
alt="Finally - a well-formatted multi-line tooltip" src="http://UndocumentedMatlab.com/images/tooltip_multi-line6.png" title="Finally - a well-formatted multi-line tooltip" width="320" /><p
class="wp-caption-text">Finally - a well-formatted multi-line tooltip</p></div></center></p><p>Even more powerful customizations can be achieved using CSS rather than pure HTML. This will be discussed a future article.</html></pre><p></html></pre><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/' rel='bookmark' title='Spicing up Matlab uicontrol tooltips'>Spicing up Matlab uicontrol tooltips</a> <small>Matlab uicontrol tooltips can be spiced-up using HTML and CSS, including fonts, colors, tables and images...</small></li><li><a
href='http://undocumentedmatlab.com/blog/multi-column-grid-legend/' rel='bookmark' title='Multi-column (grid) legend'>Multi-column (grid) legend</a> <small>This article explains how to use undocumented axes listeners for implementing multi-column plot legends...</small></li><li><a
href='http://undocumentedmatlab.com/blog/inactive-control-tooltips-event-chaining/' rel='bookmark' title='Inactive Control Tooltips &amp; Event Chaining'>Inactive Control Tooltips &#038; Event Chaining</a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/multi-line-tooltips/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>datestr performance</title><link>http://undocumentedmatlab.com/blog/datestr-performance/</link> <comments>http://undocumentedmatlab.com/blog/datestr-performance/#comments</comments> <pubDate>Wed, 05 Oct 2011 20:17:28 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2453</guid> <description><![CDATA[Caching is a simple and very effective means to improve code performance, as demonstrated for the datestr function.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-performance/' rel='bookmark' title='Plot performance'>Plot performance</a> <small>Undocumented inner plot mechanisms can be used to significantly improved plotting performance...</small></li><li><a
href='http://undocumentedmatlab.com/blog/datenum-performance/' rel='bookmark' title='Datenum performance'>Datenum performance</a> <small>The performance of the built-in Matlab function datenum can be significantly improved by using an undocumented internal help function...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matrix-processing-performance/' rel='bookmark' title='Matrix processing performance'>Matrix processing performance</a> <small>Matrix operations performance is affected by internal subscriptions in a counter-intuitive way....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A few months ago, I posted an <a
target="_blank" href="http://undocumentedmatlab.com/blog/datenum-performance/">article</a> showing how we can use some internal help functions to significantly improve the performance of the commonly-used <i><b>datenum</b></i> function. The catch is that we must be certain of certain preconditions before we can use this method, otherwise we might get incorrect results.</p><p>Today I wish to share a related experience that happened to me yesterday, when I needed to improve the performance of a client&#8217;s application. When profiling the application, I found that a major performance hotspot was a repeated call to the <i><b>datestr</b></i> function, each time with several thousand date items.</p><p><i><b>datestr</b></i> is the opposite function of <i><b>datenum</b></i>: it receives date values and returns date strings. Unlike <i><b>datenum</b></i>, however, <i><b>datestr</b></i> does not use a highly optimized native-code library function that we could use directly. Instead, it loops over all date values and sequentially applies the requested string pattern.</p><p>The natural reaction in such a case would perhaps be to vectorize the code (something that MathWorks should have done in the first place I guess). But in this case I used a different solution, that I would like to share today:</p><p>In any programming languages, Matlab included, the most effective performance tip is to cache processing results. Caching often makes the code slightly more complex and less maintainable, but the performance benefits are immediate and significant. In Matlab, benefits of caching can often surpass even those of vectorization (using both vectorization <u>and</u> caching is of course even better).</p><p>In the case of <i><b>datestr</b></i>, if we can be certain of the precondition that the output string format is the same, we can cache the results, and even use vectorization. In my case, I plotted historical daily stock quotes data and so I was assured that (1) all dates are integers and that (2) I always use the same date-string format &#8216;dd-mmm-yyyy&#8217;.</p><p>First, let&#8217;s define the wrapper function <i><b>datestr2</b></i> with the necessary caching and vectorization:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% datestr2 - faster variant of datestr, for integer date values since 1/1/2000</span>
<span style="color: #0000FF;">function</span> dateStrs = datestr2<span style="color: #080;">&#40;</span>dateVals,<span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #0000FF;">persistent</span> dateStrsCache
  <span style="color: #0000FF;">persistent</span> dateValsCache
&nbsp;
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>dateStrsCache<span style="color: #080;">&#41;</span>
      origin = <span style="color: #0000FF;">datenum</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'1-Jan-2000'</span><span style="color: #080;">&#41;</span>;
      dateValsCache = origin<span style="color: #F0F;">:</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">now</span>+<span style="color: #33f;">100</span><span style="color: #080;">&#41;</span>;
      dateStrsCache = <span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span>dateValsCache,<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>
&nbsp;
  <span style="color: #080;">&#91;</span>tf,loc<span style="color: #080;">&#93;</span> = <span style="color: #0000FF;">ismember</span><span style="color: #080;">&#40;</span>dateVals, dateValsCache<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">all</span><span style="color: #080;">&#40;</span>tf<span style="color: #080;">&#41;</span>
      dateStrs = dateStrsCache<span style="color: #080;">&#40;</span>loc,<span style="color: #F0F;">:</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">else</span>
      dateStrs = <span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span>dateVals,<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: #0000FF;">end</span>  <span style="color: #228B22;">% datestr2</span></pre></div></div><p>As can be seen, the first time that <i><b>datestr2</b></i> is called, it computes and caches all <i><b>datestr</b></i> values for all the dates since Jan 1, 2000. Subsequent calls to <i><b>datestr2</b></i> simply retrieve the relevant cache values. Note that the input date entries need not be sorted.</p><p>In case that an input date number is not found in the cache, <i><b>datestr2</b></i> automatically falls-back to using the built-in <i><b>datestr</b></i> for the entire input list. This could of course be improved to add the new entries to the cache &#8211; I leave this as a reader exercise.</p><p>The bottom line was a <b>150-times (!!!) speed improvement</b> for a 1000-item date vector (50mS => 0.3mS on my system):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Prepare a 1000-vector of dates, starting 3 years ago until today</span>
&gt;&gt; dateVals = <span style="color: #0000FF;">fix</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">now</span><span style="color: #080;">&#41;</span>+<span style="color: #080;">&#40;</span>-<span style="color: #33f;">1000</span><span style="color: #F0F;">:</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Run the standard datestr function =&gt; 50mS</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; s1=<span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span>dateVals<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.049089</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; s1=<span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span>dateVals<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.048086</span> seconds.
&nbsp;
<span style="color: #228B22;">% Now run our datestr2 function (caching already done before) =&gt; 0.3 mS</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; s2=datestr2<span style="color: #080;">&#40;</span>dateVals<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.222031</span> seconds.   <span style="color: #228B22;">% initial cache preparation takes 222 mS</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; s2=datestr2<span style="color: #080;">&#40;</span>dateVals<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000313</span> seconds.   <span style="color: #228B22;">% subsequent datestr2 calls take 0.3 mS</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; s2=datestr2<span style="color: #080;">&#40;</span>dateVals<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000296</span> seconds.
&nbsp;
<span style="color: #228B22;">% Ensure that the two functions give exactly the same results</span>
&gt;&gt; isequal<span style="color: #080;">&#40;</span>s1,s2<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
     <span style="color: #33f;">1</span></pre></div></div><p>So what have we learned from this?</p><ol><li>To improve performance we must profile the code. Very often the performance bottlenecks occur in non-intuitive very specific places that can be surgically handled without requiring any major redesign. In this case, I simply had to replace calls to <i><b>datestr</b></i> with <i><b>datestr2</b></i> in the application&#8217;s code.</li><li>Vectorization is not always as cost-effective as caching</li><li>Major performance improvements do NOT necessarily involve undocumented functions or tricks: In fact, today&#8217;s post about caching uses fully-documented pure-Matlab code.</li><li>Different performance hotspots can have different solutions: caching, vectorization, <a
target="_blank" href="http://undocumentedmatlab.com/blog/datenum-performance/">internal library functions</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/plot-liminclude-properties/">undocumented graphics properties</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/plot-performance/">smart property selection</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/performance-scatter-vs-line/">smart function selection</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/matrix-processing-performance/">smart indexing</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/cellfun-undocumented-performance-boost/">smart parameter selection</a> etc.</li></ol><p>In a later post I will show how similar modifications to internal Matlab functions can dramatically improve the performance of the <i><b>uitable</b></i> function. Anyone who has tried using <i><b>uitable</b></i> with more than a few dozen cells will surely understand why this is important&#8230;</p><p>Do you have a favorite performance trick not mentioned above? If so, please post a <a
href="http://undocumentedmatlab.com/blog/datestr-performance/#respond">comment</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-performance/' rel='bookmark' title='Plot performance'>Plot performance</a> <small>Undocumented inner plot mechanisms can be used to significantly improved plotting performance...</small></li><li><a
href='http://undocumentedmatlab.com/blog/datenum-performance/' rel='bookmark' title='Datenum performance'>Datenum performance</a> <small>The performance of the built-in Matlab function datenum can be significantly improved by using an undocumented internal help function...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matrix-processing-performance/' rel='bookmark' title='Matrix processing performance'>Matrix processing performance</a> <small>Matrix operations performance is affected by internal subscriptions in a counter-intuitive way....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/datestr-performance/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>Reading non-Latin text files</title><link>http://undocumentedmatlab.com/blog/reading-non-latin-text-files/</link> <comments>http://undocumentedmatlab.com/blog/reading-non-latin-text-files/#comments</comments> <pubDate>Wed, 28 Sep 2011 14:51:50 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[Simulink]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2447</guid> <description><![CDATA[A workaround that enables reading non-Latin text files in Matlab is shown<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/fig-files-format/' rel='bookmark' title='FIG files format'>FIG files format</a> <small>FIG files are actually MAT files in disguise. This article explains how this can be useful in Matlab applications....</small></li><li><a
href='http://undocumentedmatlab.com/blog/command-window-text-manipulation/' rel='bookmark' title='Command Window text manipulation'>Command Window text manipulation</a> <small>Special control characters can be used to format text output in Matlab's Command Window. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/' rel='bookmark' title='cprintf &#8211; display formatted color text in the Command Window'>cprintf &#8211; display formatted color text in the Command Window</a> <small>cprintf is a utility that utilized undocumented Matlab desktop functionalities to display color and underline-styled formatted text in the Command Window...</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-status-bar-text/' rel='bookmark' title='Setting status-bar text'>Setting status-bar text</a> <small>The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>In the spirit of the Jewish New Year that begins tonight, I would like to share a workaround that I received from blog reader Ro&#8217;ee Gilron of Tel-Aviv University:</p><p>Matlab users who use non-Latin computer Locales are aware of the issues that the Matlab Command Window has had with such languages for many years. I am not sure whether these problems are due to the LTR nature of Hebrew/Arabic, or their use of a non-supported code-page, or some other reason. To this day (R2011b), I am not aware of any fix or workaround for these issues.</p><p>But it seems that in addition, Matlab has a problem reading files that contain text in these languages, even when the computer&#8217;s Locale is set correctly, to a Locale that supports the non-Latin text. This is where Ro&#8217;ee&#8217;s workaround helps. In his words:</p><p><q><i>To give some more background, this used to work with a 32bit system, and an older version of Matlab (7.1). Now it doesn&#8217;t. Saving the file in UTF-8 and using <b>fopen</b> and <b>textscan</b> instead of <b>importdata</b> gives me this:</p><p>nowords =<br
/> &#8216;ï»¿×©×œ×‘×§&#8217;<br
/> &#8216;×”×ª×œ×›×‘&#8217;<br
/> &#8216;× ×™×›×˜×¨&#8217;<br
/> &#8216;×ª×œ×¤×•×¨×©&#8217;<br
/> &#8216;×œ×§×˜× &#8216;<br
/> &#8216;×ž×–×•×—×©&#8217;<br
/> &#8216;×©×œ×˜×™×§&#8217;<br
/> &#8216;×˜×™×‘×¨&#8217;<br
/> &#8216;×¢×•×œ×’&#8217;<br
/> &#8216;×¡×œ×‘×•×—×“&#8217;<br
/> &#8216;×ž×©×•×—×’×•×ª&#8217;<br
/> &#8216;×ž×œ×•×’×¡×•×ª&#8217;<br
/> &#8216;×¡×‘×§&#8217;<br
/> &#8216;×¦×ž×©×¨&#8217;<br
/> &#8216;×”×›×¨×™×‘&#8217;<br
/> &#8216;×ª×ž×¦×™×œ&#8217;<br
/> </i></q></p><p>The solution is as follows (requires Simulink):</p><p>1) Change system Locale to Hebrew: <a
target="_blank" rel="nofollow" href="http://windows.microsoft.com/en-US/windows7/Change-the-system-locale">http://windows.microsoft.com/en-US/windows7/Change-the-system-locale</a></p><p>(this doesn’t change the language of the OS etc.).</p><p>2) Change the encoding that Matlab uses:<br
/> <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/toolbox/simulink/slref/slcharacterencoding.html">http://www.mathworks.com/help/toolbox/simulink/slref/slcharacterencoding.html</a></p><p>They tell you not to, but I did&#8230; &#8211; you must change it to encoding that works for Hebrew: <a
target="_blank" rel="nofollow" href="http://www.iana.org/assignments/character-sets">http://www.iana.org/assignments/character-sets</a></p><p>Any other language should work as well (I hope&#8230;). For Hebrew the code that works for me is ISO_8859-8</p><p>3) You should now be able to read TXT files that have Hebrew characters in them.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; a=<span style="color:#A020F0;">'הצלחה!'</span>
a =
!
&nbsp;
&gt;&gt; currentCharacterEncoding = slCharacterEncoding<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>;
&gt;&gt; currentCharacterEncoding = get_param<span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>, <span style="color:#A020F0;">'CharacterEncoding'</span><span style="color: #080;">&#41;</span>  <span style="color: #228B22;">% equivalent alternative</span>
currentCharacterEncoding =
windows-<span style="color: #33f;">1252</span>
&nbsp;
<span style="color: #228B22;">% Now modify the default encoding to something more useful</span>
&gt;&gt; slCharacterEncoding<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'ISO_8859-8'</span><span style="color: #080;">&#41;</span>
&gt;&gt; set_param<span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>, <span style="color:#A020F0;">'CharacterEncoding'</span>, <span style="color:#A020F0;">'ISO_8859-8'</span><span style="color: #080;">&#41;</span>;   <span style="color: #228B22;">% equivalent alternative</span>
&nbsp;
&gt;&gt; currentCharacterEncoding = slCharacterEncoding<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
currentCharacterEncoding =
ISO-<span style="color: #33f;">8859</span>-<span style="color: #33f;">8</span>
&nbsp;
&gt;&gt; a=<span style="color:#A020F0;">'הצלחה!'</span>
a =
!                  <span style="color: #228B22;">% still no good in the Command Window...</span>
&nbsp;
<span style="color: #228B22;">% Let's try to read a file with some Hebrew words:</span>
&gt;&gt; neutral = importdata<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'neutral.txt'</span><span style="color: #080;">&#41;</span>
neutral = 
שולחן'
    <span style="color:#A020F0;">'כסא'</span>
    <span style="color:#A020F0;">'מנורה'</span>
    <span style="color:#A020F0;">'צלחת'</span>
    <span style="color:#A020F0;">'סיר'</span>
    <span style="color:#A020F0;">'מזלג'</span></pre></div></div><p>So, it appears that while we did not solve the problems with the Command Window, at least we can now read the prayer book for our New Year prayers&#8230;</p><p>Let this be a year of fulfillment, prosperity, health and happiness to all. <i><b>Shana Tova everybody!</b></i></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/fig-files-format/' rel='bookmark' title='FIG files format'>FIG files format</a> <small>FIG files are actually MAT files in disguise. This article explains how this can be useful in Matlab applications....</small></li><li><a
href='http://undocumentedmatlab.com/blog/command-window-text-manipulation/' rel='bookmark' title='Command Window text manipulation'>Command Window text manipulation</a> <small>Special control characters can be used to format text output in Matlab's Command Window. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/' rel='bookmark' title='cprintf &#8211; display formatted color text in the Command Window'>cprintf &#8211; display formatted color text in the Command Window</a> <small>cprintf is a utility that utilized undocumented Matlab desktop functionalities to display color and underline-styled formatted text in the Command Window...</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-status-bar-text/' rel='bookmark' title='Setting status-bar text'>Setting status-bar text</a> <small>The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/reading-non-latin-text-files/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>getundoc &#8211; get undocumented object properties</title><link>http://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/</link> <comments>http://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/#comments</comments> <pubDate>Wed, 21 Sep 2011 19:00:16 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[HG2]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[schema.prop]]></category> <category><![CDATA[Undocumented property]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2437</guid> <description><![CDATA[getundoc is a very simple utility that displays the hidden (undocumented) properties of a specified handle object.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/undocumented-cursorbar-object/' rel='bookmark' title='Undocumented cursorbar object'>Undocumented cursorbar object</a> <small>Matlab's internal undocumented graphics.cursorbar object can be used to present dynamic data-tip cross-hairs...</small></li><li><a
href='http://undocumentedmatlab.com/blog/udd-properties/' rel='bookmark' title='UDD Properties'>UDD Properties</a> <small>UDD provides a very convenient way to add customizable properties to existing Matlab object handles...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-liminclude-properties/' rel='bookmark' title='Plot LimInclude properties'>Plot LimInclude properties</a> <small>The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....</small></li><li><a
href='http://undocumentedmatlab.com/blog/displaying-hidden-handle-properties/' rel='bookmark' title='Displaying hidden handle properties'>Displaying hidden handle properties</a> <small>I present two ways of checking undocumented hidden properties in Matlab Handle Graphics (HG) handles...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Last week, I <a
target="_blank" href="http://undocumentedmatlab.com/blog/controlling-plot-data-tips/#Properties">presented</a> the list of undocumented properties available in Matlab&#8217;s cursor mode and data-tip objects. Over the past two years, I had posted <a
target="_blank" href="http://undocumentedmatlab.com/blog/category/hidden-property/">quite a few other articles</a> on this website that used such undocumented properties. So today I will show exactly how such properties can be discovered.</p><p>Hidden properties are object properties that for some reason or other MathWorks has decided not to expose to the general public. They can still be used by Matlab users, just like any other regular property. But if you use the built-in <i><b>get</b></i> and <i><b>set</b></i> functions to list the object&#8217;s properties, you will not find the hidden properties listed. You need to know the hidden properties&#8217; exact name in order to use them. Which is where today&#8217;s post can help, by showing you how to list these hidden properties. I <a
target="_blank" href="http://undocumentedmatlab.com/blog/displaying-hidden-handle-properties/">wrote about this</a> a couple of years ago, and today&#8217;s article will expand on that original post.</p><p>Hidden properties are by their very nature undocumented and not officially supported. For this reason, you should take extra care when relying on them in your code. They could change functionality or even be removed without prior notice in any future Matlab release. Still, some of these properties enable very important functionality, as I have often shown on this website.</p><h3 id="HideUndocumented">HideUndocumented</h3><p>There are two distinct manners by which undocumented properties can be seen in Matlab. The simplest was <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/1183#2277">reported</a> by Hans Olsson all the way back in 1997, in one of the very earliest posts on the CSSM newsgroup (there is no earlier public report, as far as I could tell). Since then, this method was mentioned in about a dozen <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/search_results?dur=all&#038;page=1&#038;query=hideundocumented&#038;search=+Go+&#038;search_string=hideundocumented&#038;search_submit=cssm">other CSSM posts</a>.</p><p>By setting the Matlab root (handle 0)&#8217;s <b>HideUndocumented</b> property to &#8216;off&#8217; (default=&#8217;on&#8217;), subsequent calls to the built-in <i><b>get</b></i> and <i><b>set</b></i> functions list the hidden properties in addition to the regular ones. Note that <b>HideUndocumented</b> is itself a hidden property, which is why Hans&#8217; post is so important &#8211; he presented us with the loose end that then enabled us to untangle the hidden properties in any other HG object.</p><p>Here is a simple example, showing <b>HideUndocumented</b>&#8216;s effect on the root (handle 0) object handle itself (hidden properties are highlighted):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Display only regular properties</span>
&gt;&gt; <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>
	CallbackObject = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	CommandWindowSize = <span style="color: #080;">&#91;</span><span style="color: #33f;">86</span> <span style="color: #33f;">51</span><span style="color: #080;">&#93;</span>
	CurrentFigure = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	<span style="color: #0000FF;">Diary</span> = off
	DiaryFile = <span style="color: #0000FF;">diary</span>
	<span style="color: #0000FF;">Echo</span> = off
	FixedWidthFontName = Courier New
	<span style="color: #0000FF;">Format</span> = longG
	FormatSpacing = compact
	Language = he_il
	MonitorPositions = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">2</span> by <span style="color: #33f;">4</span><span style="color: #080;">&#41;</span> <span style="color: #0000FF;">double</span> array<span style="color: #080;">&#93;</span>
	<span style="color: #0000FF;">More</span> = off
	PointerLocation = <span style="color: #080;">&#91;</span><span style="color: #33f;">1084</span> <span style="color: #33f;">590</span><span style="color: #080;">&#93;</span>
	RecursionLimit = <span style="color: #080;">&#91;</span><span style="color: #33f;">500</span><span style="color: #080;">&#93;</span>
	ScreenDepth = <span style="color: #080;">&#91;</span><span style="color: #33f;">32</span><span style="color: #080;">&#93;</span>
	ScreenPixelsPerInch = <span style="color: #080;">&#91;</span><span style="color: #33f;">96</span><span style="color: #080;">&#93;</span>
	ScreenSize = <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <span style="color: #33f;">1440</span> <span style="color: #33f;">900</span><span style="color: #080;">&#93;</span>
	ShowHiddenHandles = off
	Units = pixels
&nbsp;
	BeingDeleted = off
	ButtonDownFcn = 
	Children = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	Clipping = on
	CreateFcn = 
	DeleteFcn = 
	BusyAction = queue
	HandleVisibility = on
	HitTest = on
	Interruptible = on
	Parent = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	Selected = off
	SelectionHighlight = on
	Tag = 
	<span style="color: #0000FF;">Type</span> = root
	<span style="color: #0000FF;">UIContextMenu</span> = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	UserData = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	Visible = on
&nbsp;
<span style="color: #228B22;">% Display ALL properties (including hidden ones, which are highlighted below)</span>
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>,<span style="color:#A020F0;">'HideUndocumented'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>
&gt;&gt; <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>
<span style="display:block;background-color: #ffc;">	BlackAndWhite = off</span>	CallbackObject = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	CommandWindowSize = <span style="color: #080;">&#91;</span><span style="color: #33f;">86</span> <span style="color: #33f;">51</span><span style="color: #080;">&#93;</span>
	CurrentFigure = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	<span style="color: #0000FF;">Diary</span> = off
	DiaryFile = <span style="color: #0000FF;">diary</span>
	<span style="color: #0000FF;">Echo</span> = off
<span style="display:block;background-color: #ffc;">	ErrorMessage = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">1</span> by <span style="color: #33f;">79</span><span style="color: #080;">&#41;</span> <span style="color: #0000FF;">char</span> array<span style="color: #080;">&#93;</span></span>	FixedWidthFontName = Courier New
	<span style="color: #0000FF;">Format</span> = longG
	FormatSpacing = compact
<span style="display:block;background-color: #ffc;">	HideUndocumented = off</span>	Language = he_il
	MonitorPositions = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">2</span> by <span style="color: #33f;">4</span><span style="color: #080;">&#41;</span> <span style="color: #0000FF;">double</span> array<span style="color: #080;">&#93;</span>
	<span style="color: #0000FF;">More</span> = off
	PointerLocation = <span style="color: #080;">&#91;</span><span style="color: #33f;">1022</span> <span style="color: #33f;">82</span><span style="color: #080;">&#93;</span>
<span style="display:block;background-color: #ffc;">	PointerWindow = <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span><span style="color: #080;">&#93;</span></span>	RecursionLimit = <span style="color: #080;">&#91;</span><span style="color: #33f;">500</span><span style="color: #080;">&#93;</span>
	ScreenDepth = <span style="color: #080;">&#91;</span><span style="color: #33f;">32</span><span style="color: #080;">&#93;</span>
	ScreenPixelsPerInch = <span style="color: #080;">&#91;</span><span style="color: #33f;">96</span><span style="color: #080;">&#93;</span>
	ScreenSize = <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <span style="color: #33f;">1440</span> <span style="color: #33f;">900</span><span style="color: #080;">&#93;</span>
	ShowHiddenHandles = off
	Units = pixels
<span style="display:block;background-color: #ffc;">	AutomaticFileUpdates = on</span>&nbsp;
	BeingDeleted = off
<span style="display:block;background-color: #ffc;">	PixelBounds = <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span> <span style="color: #33f;">0</span> <span style="color: #33f;">0</span> <span style="color: #33f;">0</span><span style="color: #080;">&#93;</span></span>	ButtonDownFcn = 
	Children = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	Clipping = on
	CreateFcn = 
	DeleteFcn = 
	BusyAction = queue
	HandleVisibility = on
<span style="display:block;background-color: #ffc;">	HelpTopicKey = </span>	HitTest = on
	Interruptible = on
	Parent = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	Selected = off
	SelectionHighlight = on
<span style="display:block;background-color: #ffc;">	Serializable = on</span>	Tag = 
	<span style="color: #0000FF;">Type</span> = root
	<span style="color: #0000FF;">UIContextMenu</span> = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
	UserData = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
<span style="display:block;background-color: #ffc;">	ApplicationData = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">1</span> by <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> <span style="color: #0000FF;">struct</span> array<span style="color: #080;">&#93;</span></span><span style="display:block;background-color: #ffc;">	Behavior = <span style="color: #080;">&#91;</span> <span style="color: #080;">&#40;</span><span style="color: #33f;">1</span> by <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> <span style="color: #0000FF;">struct</span> array<span style="color: #080;">&#93;</span></span>	Visible = on
<span style="display:block;background-color: #ffc;">	XLimInclude = on</span><span style="display:block;background-color: #ffc;">	YLimInclude = on</span><span style="display:block;background-color: #ffc;">	ZLimInclude = on</span><span style="display:block;background-color: #ffc;">	CLimInclude = on</span><span style="display:block;background-color: #ffc;">	ALimInclude = on</span><span style="display:block;background-color: #ffc;">	IncludeRenderer = on</span></pre></div></div><h3 id="schema">Property definitions</h3><p>An entirely different mechanism uses the <i><b>schema.prop</b></i> definitions that were <a
target="_blank" href="http://undocumentedmatlab.com/blog/udd-properties/">presented here</a> by Donn Scull at the beginning of 2011. The idea is to get the object&#8217;s <i><b>classhandle</b></i> reference, from it to get the list of properties definitions, and for each property look at its <b>Visible</b> meta-property: hidden properties will simply have <b>Visible</b>=&#8217;off&#8217;, whereas regular properties will have &#8216;on&#8217;.</p><p>It turns out that there is not always a full correspondence between these two mechanism. I can&#8217;t remember specific examples, and perhaps these were fixed in the latest Matlab releases. It doesn&#8217;t matter, because merging the list of hidden properties reported by these two methods is always safe to do. Which is exactly what my <i><b>getundoc</b></i> utility does:</p><h3 id="getundoc">getundoc utility</h3><p>The <i><b>getundoc</b></i> utility is based on another utility by the same name, posted by <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/authors/31831">Duane Hanselman</a> to the Matlab File Exchange in 2006 (Duane has elected to remove all his submissions from FEX a year or two ago, but that&#8217;s an entirely separate [and extremely heated] topic for a different discussion). Duane&#8217;s original <i><b>getundoc</b></i> utility relied only on the first (<b>HideUndocumented</b>) mechanism.</p><p>I have since expanded this utility to include support for the second mechanism, as well as support for the upcoming HG2 (see below). The updated <i><b>getundoc</b></i> is now <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/32934-getundoc-get-undocumented-object-properties">available for download</a> on the File Exchange. Since it&#8217;s a very short utility, I will digress from my norm and simply present its code, in its present form, here:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> c = getundoc<span style="color: #080;">&#40;</span>arg<span style="color: #080;">&#41;</span>
<span style="color: #228B22;">%GETUNDOC Get Undocumented Object Properties.</span>
<span style="color: #228B22;">% GETUNDOC('OBJECT') or GETUNDOC(H) returns a structure of</span>
<span style="color: #228B22;">% undocumented properties (names &amp; values) for the object having handle</span>
<span style="color: #228B22;">% H or indentified by the string 'OBJECT'.</span>
<span style="color: #228B22;">%</span>
<span style="color: #228B22;">% For example, GETUNDOC('axes') or GETUNDOC(gca) returns undocumented</span>
<span style="color: #228B22;">% property names and values for the axes object.</span>
&nbsp;
<span style="color: #228B22;">% Extension of Duane Hanselman's original utility (which is no longer</span>
<span style="color: #228B22;">% available on the File Exchange):</span>
<span style="color: #228B22;">% D.C. Hanselman, University of Maine, Orono, ME 04469</span>
<span style="color: #228B22;">% MasteringMatlab@yahoo.com</span>
<span style="color: #228B22;">% Mastering MATLAB 7</span>
<span style="color: #228B22;">% 2006-01-06</span>
&nbsp;
<span style="color: #0000FF;">if</span> <span style="color: #0000FF;">nargin</span>~=<span style="color: #33f;">1</span>
   <span style="color: #0000FF;">error</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'One Input Required.'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">if</span> <span style="color: #0000FF;">ischar</span><span style="color: #080;">&#40;</span>arg<span style="color: #080;">&#41;</span> <span style="color: #228B22;">% GETUNDOC('OBJECT')</span>
   <span style="color: #0000FF;">switch</span> <span style="color: #0000FF;">lower</span><span style="color: #080;">&#40;</span>arg<span style="color: #080;">&#41;</span>
   <span style="color: #0000FF;">case</span> <span style="color:#A020F0;">'root'</span>                                                       <span style="color: #228B22;">% root</span>
      h=<span style="color: #33f;">0</span>;
      hf=<span style="color: #33f;">0</span>;
   <span style="color: #0000FF;">case</span> <span style="color:#A020F0;">'figure'</span>                                                   <span style="color: #228B22;">% figure</span>
      h=<span style="color: #0000FF;">figure</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Visible'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;
      hf=h;
   <span style="color: #0000FF;">otherwise</span>                          <span style="color: #228B22;">% some other string name of an object</span>
      hf=<span style="color: #0000FF;">figure</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Visible'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;
      object=str2func<span style="color: #080;">&#40;</span>arg<span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">try</span>
         h=object<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Parent'</span>,hf,<span style="color:#A020F0;">'Visible'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">catch</span>
         <span style="color: #0000FF;">error</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Unknown Object Type String Provided.'</span><span style="color: #080;">&#41;</span>         
      <span style="color: #0000FF;">end</span>
   <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">elseif</span> <span style="color: #0000FF;">ishandle</span><span style="color: #080;">&#40;</span>arg<span style="color: #080;">&#41;</span> <span style="color: #228B22;">% GETUNDOC(H)</span>
   h=arg;
   hf=<span style="color: #33f;">0</span>;
<span style="color: #0000FF;">else</span>
   <span style="color: #0000FF;">error</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Unknown Object Handle Provided.'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">end</span>
&nbsp;
wstate=<span style="color: #0000FF;">warning</span>;
<span style="color: #0000FF;">warning</span> off                                      <span style="color: #228B22;">% supress warnings about obsolete properties</span>
<span style="color: #0000FF;">try</span> <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>,<span style="color:#A020F0;">'HideUndocumented'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">catch</span>; <span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% Fails in HG2</span>
undocfnames=<span style="color: #0000FF;">fieldnames</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>h<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;                  <span style="color: #228B22;">% get props including undocumented</span>
<span style="color: #0000FF;">try</span> <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>,<span style="color:#A020F0;">'HideUndocumented'</span>,<span style="color:#A020F0;">'on'</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">catch</span>; <span style="color: #0000FF;">end</span>   <span style="color: #228B22;">% Fails in HG2</span>
docfnames=<span style="color: #0000FF;">fieldnames</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>h<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;                    <span style="color: #228B22;">% get props excluding undocumented</span>
&nbsp;
<span style="color: #228B22;">% Yair 18/3/2010 - add a few more undocs:</span>
<span style="color: #0000FF;">try</span>
    <span style="color: #228B22;">% This works in HG1</span>
    props = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>classhandle<span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>h<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'properties'</span><span style="color: #080;">&#41;</span>;
    undocfnames = <span style="color: #080;">&#91;</span>undocfnames; <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>props<span style="color: #080;">&#40;</span><span style="color: #0000FF;">strcmp</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>props,<span style="color:#A020F0;">'Visible'</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'Name'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>;
<span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Yair 18/9/2011: In HG2, the above fails, so use the following workaround:</span>
    <span style="color: #0000FF;">try</span>
        prop = <span style="color: #0000FF;">findprop</span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>h<span style="color: #080;">&#41;</span>,undocfnames<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
        props = prop.<span style="">DefiningClass</span>.<span style="">PropertyList</span>;
        undocfnames = <span style="color: #080;">&#91;</span>undocfnames; <span style="color: #080;">&#123;</span>props.<span style="">Name</span><span style="color: #080;">&#125;</span>'<span style="color: #080;">&#93;</span>;   <span style="color: #228B22;">% {props([props.Hidden]).Name}</span>
    <span style="color: #0000FF;">catch</span>
        <span style="color: #228B22;">% ignore...</span>
    <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>
&nbsp;
c = <span style="color: #0000FF;">setdiff</span><span style="color: #080;">&#40;</span>undocfnames,docfnames<span style="color: #080;">&#41;</span>;      <span style="color: #228B22;">% extract undocumented</span>
&nbsp;
<span style="color: #228B22;">% Get the values in struct format, if relevant</span>
<span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>c<span style="color: #080;">&#41;</span>
  s = <span style="color: #0000FF;">struct</span>;
  <span style="color: #0000FF;">for</span> fieldIdx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>c<span style="color: #080;">&#41;</span>
      <span style="color: #0000FF;">try</span>
          fieldName = c<span style="color: #080;">&#123;</span>fieldIdx<span style="color: #080;">&#125;</span>;
          s.<span style="color: #080;">&#40;</span>fieldName<span style="color: #080;">&#41;</span> = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>h,fieldName<span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">catch</span>
          s.<span style="color: #080;">&#40;</span>fieldName<span style="color: #080;">&#41;</span> = <span style="color:#A020F0;">'???'</span>;
      <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">end</span>
  c = s;
<span style="color: #0000FF;">end</span>
<span style="color: #228B22;">% Yair end</span>
&nbsp;
<span style="color: #0000FF;">if</span> hf~=<span style="color: #33f;">0</span>                     <span style="color: #228B22;">% delete hidden figure holding selected object</span>
   <span style="color: #0000FF;">delete</span><span style="color: #080;">&#40;</span>hf<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">warning</span><span style="color: #080;">&#40;</span>wstate<span style="color: #080;">&#41;</span></pre></div></div><p>Usage of this utility is extremely simple:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; getundoc<span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
             ALimInclude<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
         ApplicationData<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 <span style="color: #0000FF;">struct</span><span style="color: #080;">&#93;</span>
    AutomaticFileUpdates<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
                Behavior<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 <span style="color: #0000FF;">struct</span><span style="color: #080;">&#93;</span>
           BlackAndWhite<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'off'</span>
             CLimInclude<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
            ErrorMessage<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x79 <span style="color: #0000FF;">char</span><span style="color: #080;">&#93;</span>
            HelpTopicKey<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
        HideUndocumented<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
         IncludeRenderer<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
             PixelBounds<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span> <span style="color: #33f;">0</span> <span style="color: #33f;">0</span> <span style="color: #33f;">0</span><span style="color: #080;">&#93;</span>
           PointerWindow<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
            Serializable<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
             XLimInclude<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
             YLimInclude<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
             ZLimInclude<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
&nbsp;
&gt;&gt; getundoc<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
               ALimInclude<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
    ActivePositionProperty<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'position'</span>
           ApplicationData<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 <span style="color: #0000FF;">struct</span><span style="color: #080;">&#93;</span>
              BackingStore<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
                  Behavior<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 <span style="color: #0000FF;">struct</span><span style="color: #080;">&#93;</span>
               CLimInclude<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
                CurrentKey<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
           CurrentModifier<span style="color: #F0F;">:</span> <span style="color: #080;">&#123;</span>1x0 <span style="color: #0000FF;">cell</span><span style="color: #080;">&#125;</span>
                 Dithermap<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>64x3 <span style="color: #0000FF;">double</span><span style="color: #080;">&#93;</span>
             DithermapMode<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'manual'</span>
              DoubleBuffer<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
            ExportTemplate<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
               FixedColors<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>3x3 <span style="color: #0000FF;">double</span><span style="color: #080;">&#93;</span>
                   HelpFcn<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
              HelpTopicKey<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
              HelpTopicMap<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
           IncludeRenderer<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
                 JavaFrame<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 com.<span style="">mathworks</span>.<span style="">hg</span>.<span style="">peer</span>.<span style="">HG1FigurePeer</span><span style="color: #080;">&#93;</span>
               MinColormap<span style="color: #F0F;">:</span> <span style="color: #33f;">64</span>
             OuterPosition<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #33f;">436</span> <span style="color: #33f;">374</span> <span style="color: #33f;">568</span> <span style="color: #33f;">502</span><span style="color: #080;">&#93;</span>
               PixelBounds<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span> <span style="color: #33f;">0</span> <span style="color: #33f;">560</span> <span style="color: #33f;">420</span><span style="color: #080;">&#93;</span>
             PrintTemplate<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
              Serializable<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
                    UseHG2<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'off'</span>
                WaitStatus<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
               XLimInclude<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
               YLimInclude<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
               ZLimInclude<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span></pre></div></div><h3 id="HG2">Fixes for HG2</h3><p>Unfortunately, in the new <a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-hg2/">HG2</a> (which is still not in production, but we must be prepared, mustn&#8217;t we?), the original mechanism above (<b>HideUndocumented</b>) fails completely (there is no such property in the new <code>matlab.ui.root</code> object), whereas the second mechanism (UDD property defs) needs to be modified: Apparently, <i><b>classhandle</b></i> fails for HG2 object handles. Instead, we use the workaround of using <i><b>findprop</b></i> to get the property definition for any regular property, then get its parent (the requested class definition), and then down again to list all available properties. Note that in HG2, the relevant meta-property is <b>Hidden</b> which holds logical (<b><i>true/false</i></b>) values, as opposed to <b>Visible</b> and &#8216;off&#8217;/'on&#8217; values for HG1 above.</p><p>All of these fixes are now incorporated in the <i><b>getundoc</b></i> code that is listed above.</p><p>When comparing the list of hidden properties in the existing HG1 and the new HG2, we see many interesting differences. And yes: the figure&#8217;s <a
target="_blank" href="http://undocumentedmatlab.com/blog/minimize-maximize-figure-window/#JavaFrame"><b>JavaFrame</b></a> property was indeed removed in HG2. Bummer! (don&#8217;t worry &#8211; there are several workarounds up my sleeve&#8230;)</p><p>Do you have any favorite hidden property that you use in your code? If so, please tell us about it in a comment <a
href="http://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/#respond">below</a>.</p><p>p.s. &#8211; For all the numerous good people telling me about <a
target="_blank" href="http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/"><i><b>cprintf</b></i></a> &#8211; Yes: I am aware that the latest R2011b release has broken <i><b>cprintf</b></i>&#8216;s functionality. I plan to release a workaround sometime soon, when I have some spare time. I&#8217;ll keep everybody posted of course. Please be patient. (if you can&#8217;t wait, you can always <a
target="_blank" href="http://undocumentedmatlab.com/consulting/">hire me</a> to fix it sooner; otherwise I need to give priority to my paying clients&#8230;)</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/undocumented-cursorbar-object/' rel='bookmark' title='Undocumented cursorbar object'>Undocumented cursorbar object</a> <small>Matlab's internal undocumented graphics.cursorbar object can be used to present dynamic data-tip cross-hairs...</small></li><li><a
href='http://undocumentedmatlab.com/blog/udd-properties/' rel='bookmark' title='UDD Properties'>UDD Properties</a> <small>UDD provides a very convenient way to add customizable properties to existing Matlab object handles...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-liminclude-properties/' rel='bookmark' title='Plot LimInclude properties'>Plot LimInclude properties</a> <small>The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....</small></li><li><a
href='http://undocumentedmatlab.com/blog/displaying-hidden-handle-properties/' rel='bookmark' title='Displaying hidden handle properties'>Displaying hidden handle properties</a> <small>I present two ways of checking undocumented hidden properties in Matlab Handle Graphics (HG) handles...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Controlling plot data-tips</title><link>http://undocumentedmatlab.com/blog/controlling-plot-data-tips/</link> <comments>http://undocumentedmatlab.com/blog/controlling-plot-data-tips/#comments</comments> <pubDate>Wed, 14 Sep 2011 20:42:40 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[modemanager]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[Undocumented property]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2432</guid> <description><![CDATA[Data-tips are an extremely useful plotting tool that can easily be controlled programmatically.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/accessing-plot-brushed-data/' rel='bookmark' title='Accessing plot brushed data'>Accessing plot brushed data</a> <small>Plot data brushing can be accessed programmatically using very simple pure-Matlab code...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-linesmoothing-property/' rel='bookmark' title='Plot LineSmoothing property'>Plot LineSmoothing property</a> <small>LineSmoothing is a hidden and undocumented plot line property that creates anti-aliased (smooth unpixelized) lines in Matlab plots...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-liminclude-properties/' rel='bookmark' title='Plot LimInclude properties'>Plot LimInclude properties</a> <small>The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....</small></li><li><a
href='http://undocumentedmatlab.com/blog/com-activex-tips/' rel='bookmark' title='COM/ActiveX tips &amp; tricks'>COM/ActiveX tips &#038; tricks</a> <small>This article describes several little-known tips useful for COM / ActiveX programming in Matlab...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Plot <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/creating_plots/f4-44221.html">data tips</a> are a great visualization aid for Matlab plots. They enable users to interactively click on a plot location and see a tool-tip that contains the clicked location&#8217;s coordinates. The displayed tooltip text is even customizable using documented properties of the <i><b><a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/ref/datacursormode.html">datacursormode</a></b></i> object.</p><p><center><div
class="wp-caption aligncenter" style="width: 441px"><img
alt="plot data tips" src="http://UndocumentedMatlab.com/images/datatips.png" title="plot data tips" width="431" /><p
class="wp-caption-text">plot data tips</p></div></center></p><p>A client has recently asked me to automatically display an attached data-tip to the last data point of a plotted time series of values. The idea was to immediately see what the latest value of the data series is.</p><p>Unfortunately, the official documentation clearly says that:</p><blockquote><p>You place data tips only by clicking data objects on graphs. You cannot place them programmatically (by executing code to position a data cursor).</p></blockquote><p>Well, this has never stopped us before, has it?</p><h3 id="Creating">Creating new data tips</h3><p>Under the hood, data tips use a data-cursor mode, which shares many similarities in behavior and programming code with the other plot modes (zoom, pan, <a
target="_blank" href="http://undocumentedmatlab.com/blog/accessing-plot-brushed-data/">data-brushing</a>, etc.). At any one time, only a single such mode can be active in any figure window (this is a known limitation of the design). The code itself it actually quite complex and handles numerous edge-cases. Understanding it by simply reading the code (under %matlabroot%\toolbox\matlab\graphics\) is actually pretty difficult. A much easier way to understand the programming flow is to liberally distribute breakpoints (start in <i>datacursormode.m</i>) and interactively activate the functionality, then debug the code step-by-step.</p><p>Luckily, it turns out that the code to create a new data-tip is actually quite simple: first get the data-cursor mode object, then create a new data tip using the mode&#8217;s <i>createDatatip()</i> method, update some data-tip properties and finally update the data-tip&#8217;s position:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% First plot the data</span>
hLine = <span style="color: #0000FF;">plot</span><span style="color: #080;">&#40;</span>xdata, ydata<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% First get the figure's data-cursor mode, activate it, and set some of its properties</span>
cursorMode = datacursormode<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>cursorMode, <span style="color:#A020F0;">'enable'</span>,<span style="color:#A020F0;">'on'</span>, <span style="color:#A020F0;">'UpdateFcn'</span>,@setDataTipTxt, <span style="color:#A020F0;">'NewDataCursorOnClick'</span>,<span style="color: #0000FF;">false</span><span style="color: #080;">&#41;</span>;
<span style="color: #228B22;">% Note: the optional @setDataTipTxt is used to customize the data-tip's appearance</span>
&nbsp;
<span style="color: #228B22;">% Note: the following code was adapted from %matlabroot%\toolbox\matlab\graphics\datacursormode.m</span>
<span style="color: #228B22;">% Create a new data tip</span>
hTarget = handle<span style="color: #080;">&#40;</span>hLine<span style="color: #080;">&#41;</span>;
hDatatip = cursorMode.<span style="">createDatatip</span><span style="color: #080;">&#40;</span>hTarget<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Create a copy of the context menu for the datatip:</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hDatatip,<span style="color:#A020F0;">'UIContextMenu'</span>,<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>cursorMode,<span style="color:#A020F0;">'UIContextMenu'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hDatatip,<span style="color:#A020F0;">'HandleVisibility'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hDatatip,<span style="color:#A020F0;">'Host'</span>,hTarget<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hDatatip,<span style="color:#A020F0;">'ViewStyle'</span>,<span style="color:#A020F0;">'datatip'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Set the data-tip orientation to top-right rather than auto</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hDatatip,<span style="color:#A020F0;">'OrientationMode'</span>,<span style="color:#A020F0;">'manual'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hDatatip,<span style="color:#A020F0;">'Orientation'</span>,<span style="color:#A020F0;">'top-right'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Update the datatip marker appearance</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hDatatip, <span style="color:#A020F0;">'MarkerSize'</span>,<span style="color: #33f;">5</span>, <span style="color:#A020F0;">'MarkerFaceColor'</span>,<span style="color:#A020F0;">'none'</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'MarkerEdgeColor'</span>,<span style="color:#A020F0;">'k'</span>, <span style="color:#A020F0;">'Marker'</span>,<span style="color:#A020F0;">'o'</span>, <span style="color:#A020F0;">'HitTest'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Move the datatip to the right-most data vertex point</span>
position = <span style="color: #080;">&#91;</span>xdata<span style="color: #080;">&#40;</span><span style="color: #0000FF;">end</span><span style="color: #080;">&#41;</span>,ydata<span style="color: #080;">&#40;</span><span style="color: #0000FF;">end</span><span style="color: #080;">&#41;</span>,<span style="color: #33f;">1</span>; xdata<span style="color: #080;">&#40;</span><span style="color: #0000FF;">end</span><span style="color: #080;">&#41;</span>,ydata<span style="color: #080;">&#40;</span><span style="color: #0000FF;">end</span><span style="color: #080;">&#41;</span>,-<span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>;
update<span style="color: #080;">&#40;</span>hDatatip, position<span style="color: #080;">&#41;</span>;</pre></div></div><h3 id="Updating">Updating an existing data tip</h3><p>To modify the appearance of a data-tip, we first need to get access to the <code>hDatatip</code> object that we created earlier, either programmatically, or interactively (or both). Since we can access pre-stored handles only of programmatically-created (not interactively-created) data-tips, we need to use a different method. There are actually two ways to do this:</p><p>The basic way is to search the relevant axes for objects that have <b>Tag</b>=&#8217;DataTipMarker&#8217;. For each data-tip, we will get two such handles: one for the marker (<b>Type</b>=&#8217;line&#8217;) and the other for the text box tooltip (<b>Type</b>=&#8217;text&#8217;). We can use these to update (for example) the marker size, color and style; and the text&#8217;s font, border and colors.</p><p>A better way is to access the <code>graphics.datatip</code> object itself. This can be done using two hidden properties of the <i><b>datacursormode</b></i> object:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Get the list of all data-tips in the current figure</span>
&gt;&gt; cursorMode = datacursormode<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span><span style="color: #080;">&#41;</span>
cursorMode =
	graphics.<span style="">datacursormanager</span>
&nbsp;
&gt;&gt; cursorMode.<span style="">DataCursors</span>
<span style="color: #0000FF;">ans</span> =
	graphics.<span style="">datatip</span><span style="color: #F0F;">:</span> <span style="color: #33f;">2</span>-by-<span style="color: #33f;">1</span>
&nbsp;
&gt;&gt; cursorMode.<span style="">CurrentDataCursor</span>
<span style="color: #0000FF;">ans</span> =
	graphics.<span style="">datatip</span>
&nbsp;
&gt;&gt; cursorMode.<span style="">CurrentDataCursor</span>.<span style="color: #0000FF;">get</span>
            Annotation<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 hg.<span style="">Annotation</span><span style="color: #080;">&#93;</span>
           DisplayName<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
           HitTestArea<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'off'</span>
          BeingDeleted<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'off'</span>
         ButtonDownFcn<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
              Children<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>2x1 <span style="color: #0000FF;">double</span><span style="color: #080;">&#93;</span>
              Clipping<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
             CreateFcn<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
             DeleteFcn<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
            BusyAction<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'queue'</span>
      HandleVisibility<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'off'</span>
               HitTest<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'off'</span>
         Interruptible<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
                Parent<span style="color: #F0F;">:</span> <span style="color: #33f;">492.005493164063</span>
    SelectionHighlight<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
                   Tag<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
                  <span style="color: #0000FF;">Type</span><span style="color: #F0F;">:</span> <span style="color:#A020F0;">'hggroup'</span>
              UserData<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
              Selected<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'off'</span>
             FontAngle<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'normal'</span>
              FontName<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'Helvetica'</span>
              FontSize<span style="color: #F0F;">:</span> <span style="color: #33f;">8</span>
             FontUnits<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'points'</span>
            FontWeight<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'normal'</span>
             EdgeColor<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #33f;">0.8</span> <span style="color: #33f;">0.8</span> <span style="color: #33f;">0.8</span><span style="color: #080;">&#93;</span>
       BackgroundColor<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <span style="color: #33f;">0.933333333333333</span><span style="color: #080;">&#93;</span>
             TextColor<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span> <span style="color: #33f;">0</span> <span style="color: #33f;">0</span><span style="color: #080;">&#93;</span>
                Marker<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'o'</span>
            MarkerSize<span style="color: #F0F;">:</span> <span style="color: #33f;">5</span>
       MarkerEdgeColor<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'k'</span>
       MarkerFaceColor<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'none'</span>
       MarkerEraseMode<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'normal'</span>
             Draggable<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
                String<span style="color: #F0F;">:</span> <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'Date: 01/09/11'</span>  <span style="color:#A020F0;">'Value: 573.24'</span><span style="color: #080;">&#125;</span>
               Visible<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
             StringFcn<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
             UpdateFcn<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
         <span style="color: #0000FF;">UIContextMenu</span><span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 <span style="color: #0000FF;">uicontextmenu</span><span style="color: #080;">&#93;</span>
                  Host<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 graph2d.<span style="">lineseries</span><span style="color: #080;">&#93;</span>
           Interpolate<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'off'</span></pre></div></div><p>We can see that the returned <code>graphics.datatip</code> object includes properties of both the text-box and the marker, making it easy to modify. Moreover, we can use its aforementioned <i>update</i> method to move the datatip to a different plot position (see example in the code above). In addition, we can also use the self-explanatory <i>getCursorInfo(), getaxes(), makeCurrent(), movetofront()</i> methods, and a few others.</p><h3 id="Properties">Cursor mode and data-tip properties</h3><p>The <code>graphics.datacursormanager</code> and the <code>graphics.datatip</code> objects have several public properties that we can use:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; cursorMode.<span style="color: #0000FF;">get</span>
              Enable<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'off'</span>
    SnapToDataVertex<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'on'</span>
        DisplayStyle<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'datatip'</span>
           UpdateFcn<span style="color: #F0F;">:</span> @setDataTipTxt
              <span style="color: #0000FF;">Figure</span><span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 <span style="color: #0000FF;">figure</span><span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; cursorMode.<span style="">CurrentDataCursor</span>.<span style="color: #0000FF;">get</span>
            Annotation<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 hg.<span style="">Annotation</span><span style="color: #080;">&#93;</span>
           DisplayName<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
           HitTestArea<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'off'</span>
      <span style="color: #F0F;">...</span> <span style="color: #228B22;">% See the list above</span></pre></div></div><p>Both these objects have plenty of additional hidden properties. You can inspect them using my <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/17935-uiinspect-display-methods-properties-callbacks-of-an-object">uiinspect utility</a>. Here is a brief list for reference (R2011b):</p><p><code>graphics.datacursormanager</code>:</p><ul><li>CurrentDataCursor</li><li>DataCursors</li><li>Debug</li><li>DefaultExportVarName</li><li>DefaultPanelPosition</li><li>EnableAxesStacking</li><li>EnableZStacking</li><li>ExternalListeners</li><li>HiddenUpdateFcn</li><li>NewDataCursorOnClick</li><li>OriginalRenderer</li><li>OriginalRendererMode</li><li>PanelDatatipHandle</li><li>PanelHandle</li><li>PanelTextHandle</li><li>UIContextMenu</li><li>UIState</li><li>ZStackMinimum</li></ul><p><code>graphics.datatip</code>:</p><ul><li>ALimInclude</li><li>ApplicationData</li><li>Behavior</li><li>CLimInclude</li><li>DataCursorHandle</li><li>DataManagerHandle</li><li>Debug</li><li>DoThrowStartDragEvent</li><li>EmptyArgUpdateFcn</li><li>EnableAxesStacking</li><li>EnableZStacking</li><li>EraseMode</li><li>EventObject</li><li>ExternalListenerHandles</li><li>HelpTopicKey</li><li>HostAxes</li><li>HostListenerHandles</li><li>IncludeRenderer</li><li>Invalid</li><li>IsDeserializing</li><li>MarkerHandle</li><li>MarkerHandleButtonDownFcn</li><li>Orientation</li><li>OrientationMode</li><li>OrientationPropertyListener</li><li>OriginalDoubleBufferState</li><li>PixelBounds</li><li>PointsOffset</li><li>Position</li><li>SelfListenerHandles</li><li>Serializable</li><li>TextBoxHandle</li><li>TextBoxHandleButtonDownFcn</li><li>Version</li><li>ViewStyle</li><li>XLimInclude</li><li>YLimInclude</li><li>ZLimInclude</li><li>ZStackMinimum</li><li>uistate</li></ul><p>As can be seen, if we really want, we can always use the <b>MarkerHandle</b> or <b>TextBoxHandle</b> directly.</p><h3 id="Deleting">Deleting data tips</h3><p>To delete a specific data-tip, simply call the cursor mode&#8217;s <i>removeDataCursor()</i> method; to delete all data-tips, call its <i>removeAllDataCursors()</i> method:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Delete the current data-tip</span>
cursorMode.<span style="">removeDataCursor</span><span style="color: #080;">&#40;</span>cursorMode.<span style="">CurrentDataCursor</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">% Delete all data-tips</span>
cursorMode.<span style="">removeAllDataCursors</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span></pre></div></div><p>Have you used plot data-tips in some nifty way? If so, please share your experience in a <a
href="http://undocumentedmatlab.com/blog/controlling-plot-data-tips/#respond">comment</a> below.</p><p>p.s. &#8211; did you notice that Java was not mentioned anywhere above? Mode managers use pure-Matlab functionality.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/accessing-plot-brushed-data/' rel='bookmark' title='Accessing plot brushed data'>Accessing plot brushed data</a> <small>Plot data brushing can be accessed programmatically using very simple pure-Matlab code...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-linesmoothing-property/' rel='bookmark' title='Plot LineSmoothing property'>Plot LineSmoothing property</a> <small>LineSmoothing is a hidden and undocumented plot line property that creates anti-aliased (smooth unpixelized) lines in Matlab plots...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-liminclude-properties/' rel='bookmark' title='Plot LimInclude properties'>Plot LimInclude properties</a> <small>The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....</small></li><li><a
href='http://undocumentedmatlab.com/blog/com-activex-tips/' rel='bookmark' title='COM/ActiveX tips &amp; tricks'>COM/ActiveX tips &#038; tricks</a> <small>This article describes several little-known tips useful for COM / ActiveX programming in Matlab...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/controlling-plot-data-tips/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           category
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-02-09 05:25:37 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/category/stock-matlab-function/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      3.238s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1328790336; expires=Fri, 08-Feb-2013 12:25:36 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Thu, 09 Feb 2012 12:25:37 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Thu, 09 Feb 2012 13:25:37 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               0c265e5a1c6a8de8587267dd8f55f8ac
Content-Encoding:   gzip
-->
