<?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>Compiler &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/compiler/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Thu, 27 Oct 2022 11:03:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>Matlab compilation quirks &#8211; take 2</title>
		<link>https://undocumentedmatlab.com/articles/matlab-compilation-quirks-take-2?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=matlab-compilation-quirks-take-2</link>
					<comments>https://undocumentedmatlab.com/articles/matlab-compilation-quirks-take-2#respond</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 31 May 2017 18:00:42 +0000</pubDate>
				<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[Toolbox]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Hanan Kavitz]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6919</guid>

					<description><![CDATA[<p>A few hard-to-trace quirks with Matlab compiler outputs are explained. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-compilation-quirks-take-2">Matlab compilation quirks &#8211; take 2</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/quirks-with-compiled-matlab-dlls" rel="bookmark" title="Quirks with compiled Matlab DLLs">Quirks with compiled Matlab DLLs </a> <small>Several quirks with Matlab-compiled DLLs are discussed and workarounds suggested. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/quirks-with-parfor-vs-for" rel="bookmark" title="Quirks with parfor vs. for">Quirks with parfor vs. for </a> <small>Parallelizing loops with Matlab's parfor might generate unexpected results. Users beware! ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/secure-ssl-connection-between-matlab-and-postgresql" rel="bookmark" title="Secure SSL connection between Matlab and PostgreSQL">Secure SSL connection between Matlab and PostgreSQL </a> <small>It is tricky, but quite possible, to use SSL to connect Matlab to a PostgreSQL database. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-infiniband-with-matlab-parallel-computing-toolbox" rel="bookmark" title="Using Infiniband with Matlab Parallel Computing Toolbox">Using Infiniband with Matlab Parallel Computing Toolbox </a> <small>Infiniband networking can significantly improve PCT performance in Matlab parallelization and distributed computing. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><i>Once again I would like to welcome guest blogger <a href="https://www.mathworks.com/matlabcentral/profile/authors/2392999-hanan-kavitz" rel="nofollow" target="_blank">Hanan Kavitz</a> of <a href="http://www.appliedmaterials.com" rel="nofollow" target="_blank">Applied Materials</a>. Hanan posted a couple of guest posts here over the past few years, including a post last year about <a href="/articles/quirks-with-compiled-matlab-dlls" target="_blank">quirks with Matlab-compiled DLLs</a>. Today Hanan will follow up on that post by discussing several additional quirks that they have encountered with Matlab compilations/deployment.</i></p>
<h3 id="make"><i>Don&#8217;t fix it, if it ain&#8217;t broke&#8230;</i></h3>
<p>In Applied Materials Israel (PDC) we use Matlab code for both algorithm development and deployment (production). As part of the dev-ops build system, which builds our product software versions, we build Matlab artifacts (binaries) from the Matlab source code.<br />
A typical software version has several hundreds Matlab artifacts that are automatically rebuilt on a daily basis, and we have many such versions – totaling many thousands of compilations each day.<br />
This process takes a long time, so we were looking for a way to make it more efficient.<br />
The idea that we chose to implement sounds simple – take a single binary module in any software version (Ex. foo.exe &#8211; Matlab-compiled exe) and check it: if the source code for this module has not changed since the last compilation then simply <b>don&#8217;t compile it</b>, just copy it from previous software version repository. Since most of our code doesn&#8217;t change daily (some of it hasn&#8217;t changed in years), we can skip the compilation time of most binaries and just copy them from some repository of previously compiled binaries.<br />
<span id="more-6919"></span><br />
In a broader look, avoiding lengthy compilations cycles by not compiling unchanged code is a common programming practice, implemented by all modern compilers. For example, the ‘make’ utility uses a ‘makefile’ to check the time stamps of all dependencies of every object file in order to decide which object requires recompilation. In reality, this is not always the best solution as time stamps may be incorrect, but it works well in the vast majority of cases.<br />
Coming back to Matlab, now comes the hard part – how could our build system know that nothing has changed in module X and that something has changed in module Y? How does it even know which source files it needs to ensure didn&#8217;t change?<br />
The credit for the idea goes to my manager, Lior Cohen, as follows: You can actually check the dependency of a given binary after compilation. <b>The basis of the solution is that a Matlab executable is in fact a compressed (zip) file</b>. The idea is then to:</p>
<ol>
<li>Compile the binary once</li>
<li>Unzip the binary and “see” all your dependencies (source files are encrypted and resources are not, but we only need the list of file names – not their content).</li>
<li>Now build a list of all your dependency files and compute the CRC value of each from the source control. Save it for the next time you are required to compile this module.</li>
<li>In the next compilation cycle, find this dependency list, review it, dependency source file at a time and make sure CRC of the dependency hasn’t changed since last time.</li>
<li>If no dependency CRC has changed, then copy the binary from the repository of previous software version, without compiling.</li>
<li>Otherwise, recompile the binary and rebuild the CRC list of all dependencies again, in preparation for the next compilation cycle.</li>
</ol>
<p>That&#8217;s it! That simple? Well&#8230; not really – the reality is a bit more complex since there are many other dependencies that need to be checked. Some of them are:</p>
<ol>
<li>Did the requested Matlab version of the binary change since the last compilation?</li>
<li>Did the compilation instructions themselves (we have a sort of ‘makefile’) change?</li>
</ol>
<p>Basically, I implemented a policy that if anything changed, or if the dependency check itself failed, then we don&#8217;t take any chances and just compile this binary. Keeping in mind that this dependencies check and file copying is much faster than a Matlab compilation, we save a lot of actual compilation time using this method.<br />
<b>Bottom line</b>: Given a software version containing hundreds of compilation instructions to execute and assuming not much has changed in the version (which is often the case), we skip over 90% of compilations altogether and only rebuild what really changed. The result is a version build that takes about half an hour, instead of many hours. Moreover, since the compilation process is working significantly less, we get fewer failures, fewer stuck or crashed mcc processes, and [not less importantly] less maintenance required by me.<br />
Note that in our implementation we rely on the undocumented fact that Matlab binaries are in fact compressed zip archives. If and when a future Matlab release will change the implementation such that the binaries will no longer be zip archives, another way will need to be devised in order to ensure the consistency of the target executable with its dependent source files.</p>
<h3 id="antivirus"><i>Don&#8217;t kill it, if it ain&#8217;t bad&#8230;</i></h3>
<p>I want to share a very weird issue I investigated over a year ago when using Matlab compiled exe. It started with a user showed me a Matlab compiled exe that didn&#8217;t run &#8211; I&#8217;m not talking about a regular Matlab exception: the process was crashing with an MS Windows popup window popping, stating something very obscure.<br />
It was a very weird behavior that I couldn&#8217;t explain – the compiler seemed to work well but the compiled executable process kept crashing. Compiling completely different code showed the same behavior.<br />
This issue has to do with the system compiler configuration that is being used. As you might know, when installing the Matlab compiler, before the first compilation is ever made, the user has to state the C compiler that the Matlab compiler should use in its compilation process. This is done by command ‘mbuild –setup’. This command asks the users to choose the C compiler and saves the configuration (batch file back then, xml in the newer versions of Matlab) in the user&#8217;s <i><b>prefdir</b></i> folder. At the time we were using Microsoft Visual C++ compiler 9.0 SP1.<br />
The breakthrough in the investigation came when I ran mcc command with <code>–verbose</code> flag, which outputs much more compilation info than I would typically ever want&#8230; I discovered that although the target executable file had been created, a post compilation step failed to execute, while issuing a very cryptic error message:</p>
<blockquote><p>mt.exe : general error c101008d: Failed to write the updated manifest to the resource of file &#8220;&#8230;&#8221;. Access is denied.</p></blockquote>
<p><center><figure style="width: 450px" class="wp-caption aligncenter"><a href="/images/deployment_error.png" target="_blank"><img decoding="async" alt="cryptic compilation error (click to zoom)" src="https://undocumentedmatlab.com/images/deployment_error.png" title="cryptic compilation error (click to zoom)" width="450" height="65" /></a><figcaption class="wp-caption-text">cryptic compilation error (click to zoom)</figcaption></figure></center><br />
The failure was in one of the ‘post link’ commands in the configuration batch file – something obscure such as this:</p>
<pre lang="bat">set POSTLINK_CMDS2=mt.exe -outputresource: %MBUILD_OUTPUT_FILE_NAME%;%MANIFEST_RESOURCE% -manifest "%MANIFEST_FILE_NAME%"</pre>
<p>This line of code takes an XML manifest file and inserts it into the generated binary file (<a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa375649(v=vs.85).aspx" rel="nofollow" target="_blank">additional details</a>).<br />
If you open a valid R2010a (and probably other old versions as well) Matlab-generated exe in a text editor you can actually see a small XML code embedded in it, while in a non-functioning exe I could not see this XML code.<br />
So why would this command fail?<br />
It turned out, as funny as it sounds, to be an antivirus issue – our IT department updated its antivirus policies and this ‘post link’ command suddenly became an illegal operation. Once our IT eased the policy, this command worked well again and the compiled executables stopped crashing, to our great joy.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-compilation-quirks-take-2">Matlab compilation quirks &#8211; take 2</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/quirks-with-compiled-matlab-dlls" rel="bookmark" title="Quirks with compiled Matlab DLLs">Quirks with compiled Matlab DLLs </a> <small>Several quirks with Matlab-compiled DLLs are discussed and workarounds suggested. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/quirks-with-parfor-vs-for" rel="bookmark" title="Quirks with parfor vs. for">Quirks with parfor vs. for </a> <small>Parallelizing loops with Matlab's parfor might generate unexpected results. Users beware! ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/secure-ssl-connection-between-matlab-and-postgresql" rel="bookmark" title="Secure SSL connection between Matlab and PostgreSQL">Secure SSL connection between Matlab and PostgreSQL </a> <small>It is tricky, but quite possible, to use SSL to connect Matlab to a PostgreSQL database. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-infiniband-with-matlab-parallel-computing-toolbox" rel="bookmark" title="Using Infiniband with Matlab Parallel Computing Toolbox">Using Infiniband with Matlab Parallel Computing Toolbox </a> <small>Infiniband networking can significantly improve PCT performance in Matlab parallelization and distributed computing. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/matlab-compilation-quirks-take-2/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Quirks with compiled Matlab DLLs</title>
		<link>https://undocumentedmatlab.com/articles/quirks-with-compiled-matlab-dlls?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=quirks-with-compiled-matlab-dlls</link>
					<comments>https://undocumentedmatlab.com/articles/quirks-with-compiled-matlab-dlls#respond</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 10 Feb 2016 19:00:00 +0000</pubDate>
				<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Hanan Kavitz]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6262</guid>

					<description><![CDATA[<p>Several quirks with Matlab-compiled DLLs are discussed and workarounds suggested. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/quirks-with-compiled-matlab-dlls">Quirks with compiled Matlab DLLs</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-compilation-quirks-take-2" rel="bookmark" title="Matlab compilation quirks &#8211; take 2">Matlab compilation quirks &#8211; take 2 </a> <small>A few hard-to-trace quirks with Matlab compiler outputs are explained. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/speeding-up-compiled-apps-startup" rel="bookmark" title="Speeding up compiled apps startup">Speeding up compiled apps startup </a> <small>The MCR_CACHE_ROOT environment variable can reportedly help to speed-up deployed Matlab executables....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/quirks-with-parfor-vs-for" rel="bookmark" title="Quirks with parfor vs. for">Quirks with parfor vs. for </a> <small>Parallelizing loops with Matlab's parfor might generate unexpected results. Users beware! ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications" rel="bookmark" title="Docking figures in compiled applications">Docking figures in compiled applications </a> <small>Figures in compiled applications cannot officially be docked since R2008a, but this can be done using a simple undocumented trick....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><i>I would like to introduce guest blogger <a href="http://www.mathworks.com/matlabcentral/profile/authors/2392999-hanan-kavitz" rel="nofollow" target="_blank">Hanan Kavitz</a> of <a href="http://www.appliedmaterials.com" rel="nofollow" target="_blank">Applied Materials</a>. Today Hanan will discuss several quirks that they have encountered with compiled Matlab DLLs.</i><br />
I work for Applied Materials Israel (PDC) in the algorithm development department. My group provides Matlab software solutions across all of our products. I am a big fan of Yair&#8217;s blog and was happy to receive an invitation to be a guest blogger.<br />
In PDC we are using Matlab compiler and Java Builder to deploy our algorithmic products at the customer sites. The software we produce includes binaries of three sorts: exe, C++ dlls, and jar files, all compiled using Matlab deployment tools.<br />
C++ dlls are not as common and are a relatively new kind of binary in production so there’s not as much experience using them in PDC and from time to time we discover weird behaviors that might be interesting to the readers. Here are three of the latest quirks that we encountered:<br />
<span id="more-6262"></span></p>
<h3 id="tmp">Leftover tmp files</h3>
<p>Recently I faced a problem that googling revealed that I might be the first to encounter it (outside MathWorks development team) as there was no mention of this anywhere. It all started when I got a mail from a fellow developer with content of the type: “<i>Hi Hanan, what are these dump files for ???</i>” and a screenshot of <i><b>tempdir</b></i> showing multiple tmp files that all had a naming convention of ‘mathworks_tmp_XXX_YYY’ (xxx and yyy being some numbers, presumably process ids):<br />
<center><figure style="width: 414px" class="wp-caption aligncenter"><a href="/images/compiler_tmp_files.jpg" target="_blank"><img fetchpriority="high" decoding="async" alt="multiple leftover tmp files in tmpdir (click for details)" src="https://undocumentedmatlab.com/images/compiler_tmp_files.jpg" title="multiple leftover tmp files in tmpdir (click for details)" width="414" height="227" /></a><figcaption class="wp-caption-text">multiple leftover tmp files in tmpdir (click for details)</figcaption></figure></center><br />
Each file was about 43MB in size, and combined they consumed the entire free space in the hard-disk, preventing applications from lunching.<br />
At first I looked in our code for the code that will produce files with this naming convention and once I was sure there is no such code I was comfortable to assume that they are MathWorks internal-use files (indeed – who other than MathWorkers would call their files <i>mathworks_tmp_&#8230;</i>).<br />
Compiling and running a small ‘hello world’ C++ dll showed that every time that a Matlab-compiled C++ dll executed and ran, a temp file is created and then deleted in the <i>%tmp%</i> dir (<i><b>tempdir</b></i>). If the process that runs this dll is ‘killed’ during the run, the temp file is not deleted and remains in the <i>%tmp%</i> folder, accumulating over time until the hard disk becomes completely full.<br />
On another machine I found a whole bunch of different temp files with different extensions. Opening them in editor didn&#8217;t reveal their purpose. They all had two things in common:</p>
<ol>
<li>All had a naming convention <i>mathworks_tmp_XXX__YYY</i>, whatever the extension of the file may be.</li>
<li>They are all useless when the Matlab application is done running.</li>
</ol>
<p>Once this was clear, the solution was very simple:</p>
<ul>
<li>When lunching the process that runs Matlab compiled dll, change the <i>%tmp%</i> directory to a new one.</li>
<li>Delete this directory once done running or on the next process start.</li>
</ul>
<h3 id="mclInitializeApplication">mclInitializeApplication</h3>
<p>As a separate discussion, our compiled algorithm is <a href="https://en.wikipedia.org/wiki/Embarrassingly_parallel" rel="nofollow" target="_blank">embarrassingly parallel</a> in nature so our C++ team is running it in parallel processes across several machines and many cores.<br />
As with all Matlab-compiled C++ DLLs, it needs to be initialized with a call to <i><b>mclInitializeApplication</b></i> per running process. This worked well when we had a relatively small number of processes running concurrently but lately we encountered a problem that out of dozens of calls to this function from time to time a call is stuck (not failed – exactly that: stuck) and the process needs to be killed.<br />
We don’t know why this happens and the solution we are currently using is pretty “shaky” at best – we retry the call to this function several times until it eventually works (and it does work after several tries).<br />
I have a suspicion that something is not entirely parallel with this function and there exists some hidden internal shared resource among different processes but no way to know exactly as this is an internal MathWorks function.</p>
<h3 id="multithreading">Implicit multithreading</h3>
<p>This is relatively old issue we encountered at the beginning when we just started using Matlab-compiled DLLs, so this might not surprise some readers. While Matlab&#8217;s builtin (automatic) multithreading is great when used in the MATLAB IDE (non-compiled), it creates a problem when running compiled DLLs in parallel across multiple processes: this multithreading ‘starves’ for cores because they are busy at running other processes.<br />
The solution is simple and well documented – when calling <i><b>mclInitializeApplication</b></i> before launching the DLL, pass it the <i>singleCompThread</i> flag to prevent implicit multithreading. We found this single threaded DLL to run faster because it reduced CPU ‘starvation’ drastically in a multi process environment.</p>
<h3 id="addendum">Addendum (by Yair again):</h3>
<p><i>I am delighted to announce that a few days ago I received an apparently-automated email from MathWorks telling me that 4 of the issues that I have reported early last year were fixed in R2015b. IMHO, this marks a very important step of closing the loop with the original issue reporter, something that I have <a href="/articles/couple-of-matlab-bugs-and-workarounds#comment-339302" rel="nofollow" target="_blank">suggested publicly</a> back in December 2014 (and also privately over the years). I believe that such automated feedbacks increase user motivation to report misbehaving or missing features, which will ultimately make Matlab better for the benefit of us all. I also believe that this proves once again my claim that beneath the corporate jargon, MathWorks is indeed a company run by engineers like us, who cares about its users and the interaction with them more than typical in the corporate world. I can only praise this email and hope that it is now part of the ongoing development release process, rather than being an isolated case. If I may suggest a followup improvement, it would be nice to receive such emails before the new release is out (i.e., during the beta phase) so that the original reporter could have a chance to test it on the beta before it actually goes live. But in any case, thanks MathWorks for listening and making yet another improvement 🙂<br />
Happy New Year of the Monkey everybody!</i></p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/quirks-with-compiled-matlab-dlls">Quirks with compiled Matlab DLLs</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-compilation-quirks-take-2" rel="bookmark" title="Matlab compilation quirks &#8211; take 2">Matlab compilation quirks &#8211; take 2 </a> <small>A few hard-to-trace quirks with Matlab compiler outputs are explained. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/speeding-up-compiled-apps-startup" rel="bookmark" title="Speeding up compiled apps startup">Speeding up compiled apps startup </a> <small>The MCR_CACHE_ROOT environment variable can reportedly help to speed-up deployed Matlab executables....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/quirks-with-parfor-vs-for" rel="bookmark" title="Quirks with parfor vs. for">Quirks with parfor vs. for </a> <small>Parallelizing loops with Matlab's parfor might generate unexpected results. Users beware! ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications" rel="bookmark" title="Docking figures in compiled applications">Docking figures in compiled applications </a> <small>Figures in compiled applications cannot officially be docked since R2008a, but this can be done using a simple undocumented trick....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/quirks-with-compiled-matlab-dlls/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Matlab compiler bug and workaround</title>
		<link>https://undocumentedmatlab.com/articles/matlab-compiler-bug-and-workaround?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=matlab-compiler-bug-and-workaround</link>
					<comments>https://undocumentedmatlab.com/articles/matlab-compiler-bug-and-workaround#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 28 Jan 2015 18:00:02 +0000</pubDate>
				<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[Toolbox]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Bug]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=5529</guid>

					<description><![CDATA[<p>Both the Matlab compiler and the publish function have errors when parsing block-comments in Matlab m-code. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-compiler-bug-and-workaround">Matlab compiler bug and workaround</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/bug-and-workaround-in-timeseries-plot" rel="bookmark" title="Bug and workaround in timeseries plot">Bug and workaround in timeseries plot </a> <small>Matlab's internal hgconvertunits function has a bug that affects timeseries plots. Luckily there is a simple workaround....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects" rel="bookmark" title="Types of undocumented Matlab aspects">Types of undocumented Matlab aspects </a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-compilation-quirks-take-2" rel="bookmark" title="Matlab compilation quirks &#8211; take 2">Matlab compilation quirks &#8211; take 2 </a> <small>A few hard-to-trace quirks with Matlab compiler outputs are explained. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>I recently consulted at a client who uses R2010a and compiles his code for distribution. Debugging compiled code is often tricky, since we do not have the Matlab desktop to help us debug stuff (well, actually we do have access to a scaled-down desktop for minimal debugging using some undocumented internal hooks, but that&#8217;s a topic for a separate article). In my client&#8217;s case, I needed to debug a run-time error that threw an exception to the console:</p>
<div class="wp_syntax">
<div class="code">
<pre class="c" style="font-family:monospace;"><span style="color: red;">Error using strtrim
Input should be a string or a cell array of strings.
Error in updateGUI (line 121)</span></pre>
</div>
</div>
<p>Sounds simple enough to debug right? Just go to <i>updateGUI.m</i> line #121 and fix the call to <i><b>strtrim</b>()</i>, correct?<br />
Well, not so fast&#8230; It turns out that updateGUI.m line #121 is an empty line surrounded on either side by one-line comments. This is certainly not the line that caused the error. The actual call to <i><b>strtrim</b>()</i> only occurs in line #147!<br />
What&#8217;s going on?<br />
The answer is that the Matlab compiler has a bug with comment blocks &#8211; lines surrounded by <code>%{</code> and <code>%}</code>:</p>
<div class="wp_syntax">
<div class="code">
<pre class="matlab" style="font-family:monospace;">15   <span style="color: #228B22;">%{</span>
16       <span style="color: #228B22;">This is a comment block that is</span>
17       <span style="color: #228B22;">ignored by the Matlab engine,</span>
18       <span style="color: #228B22;">and causes a line-numbering</span>
19       <span style="color: #228B22;">error in the Matlab compiler!</span>
20   <span style="color: #228B22;">%}</span>
21&nbsp;
22   a = strtrim<span style="color: #080;">(</span><span style="color: #0000FF;">3.1416</span><span style="color: #080;">)</span>;  <span style="color: #228B22;">% this generates an error</span></pre>
</div>
</div>
<p>In the example above, the 6-line comment block is ignored as expected by the Matlab engine in both interactive and compiled modes. However, whereas in interactive mode the error is correctly reported for line #22, in compiled mode it is reported for line #17. Apparently the compiler replaces any block comment with a single comment line before parsing the m-file.<br />
<span id="more-5529"></span><br />
The workaround is simple: count all the block-comment lines that precede the reported error line in the original m-file, and add that number to the reported line. In the example above, 17 + (6-1) = 22. Note that the source code could have multiple block-comments that precede the reported line, and they should all be counted. Here is a basic code snippet that does this parsing and opens the relevant m-file at the correct location:</p>
<pre lang='matlab'>
% Read the source file (*.m)
str = fileread(filename,'*char');
% Split the contents into separate lines
lines = strtrim(strsplit(str',10));
% Search for comment block tokens
start_idx = find(strcmp(lines,'%{'));
end_idx   = find(strcmp(lines,'%}'));
% Count the number of block-comment lines in the source code
delta_idx = end_idx - start_idx;
relevant_idx = sum(start_idx < reported_line_number);
total_comment_block_lines = sum(delta_idx(1:relevant_idx));
% Open the source file at the correct line
opentoline(filename, reported_line_number + total_comment_block_lines);
</pre>
<p>This code snippet can fairly easily be wrapped in a stand-alone utility by anyone who wishes to do so. You'd need to generate the proper full-path source-code (m-file) filename of course, since this is not reported by the deployed application in its error message. You'd also need to take care of edge-cases such as p-coded or mex files, or missing source-code m-files. You'd also need to parse the input parameters (presumably filename and reported_line_number, but possibly also other inputs).<br />
This is another example of a bug that does not officially exist in Matlab's public <a target="_blank" rel="nofollow" href="https://www.mathworks.com/support/bugreports">bug parade</a> (at least, I couldn't find it). I've reported other such undocumented bugs in previous articles (<a target="_blank" href="/articles/couple-of-matlab-bugs-and-workarounds">here</a> and <a target="_blank" href="/articles/couple-of-bugs-and-workarounds">here</a>). Please don't start another comments tirade on MathWorks' bug-publication policies. I think that issue has already been discussed ad nauseam.<br />
<b><u>Update</u></b>: according to <a target="_blank" href="/articles/matlab-compiler-bug-and-workaround#comment-345140">Tom's comment</a> below, this bug was apparently fixed in R2012b.<br />
Another related bug related to block comments is that at least on some Matlab releases (I haven't tested properly to determine which releases), block comments are NOT properly ignored by the Editor's <i><b>publish</b></i> functionality. Instead, at least on those releases where the bug occurs, publishing code that includes block comments parses the block's contents as if it was runnable code. In other words, <i><b>publish</b></i> treats <code>%{</code> and <code>%}</code> as one-line comments rather than as tokens marking the ends of a block comment.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-compiler-bug-and-workaround">Matlab compiler bug and workaround</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/bug-and-workaround-in-timeseries-plot" rel="bookmark" title="Bug and workaround in timeseries plot">Bug and workaround in timeseries plot </a> <small>Matlab's internal hgconvertunits function has a bug that affects timeseries plots. Luckily there is a simple workaround....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects" rel="bookmark" title="Types of undocumented Matlab aspects">Types of undocumented Matlab aspects </a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-compilation-quirks-take-2" rel="bookmark" title="Matlab compilation quirks &#8211; take 2">Matlab compilation quirks &#8211; take 2 </a> <small>A few hard-to-trace quirks with Matlab compiler outputs are explained. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/matlab-compiler-bug-and-workaround/feed</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>Using pure Java GUI in deployed Matlab apps</title>
		<link>https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-pure-java-gui-in-deployed-matlab-apps</link>
					<comments>https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 10 Jul 2014 16:21:09 +0000</pubDate>
				<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Karthik Ponudurai]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4909</guid>

					<description><![CDATA[<p>Using pure-Java GUI in deployed Matlab apps requires a special yet simple adaptation. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps">Using pure Java GUI in deployed Matlab apps</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/removing-user-preferences-from-deployed-apps" rel="bookmark" title="Removing user preferences from deployed apps">Removing user preferences from deployed apps </a> <small>An unsupported MathWorks Technical Solution explains how to remove private information from deployed (compiled) matlab applications. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/speeding-up-compiled-apps-startup" rel="bookmark" title="Speeding up compiled apps startup">Speeding up compiled apps startup </a> <small>The MCR_CACHE_ROOT environment variable can reportedly help to speed-up deployed Matlab executables....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/splash-window-for-deployed-applications" rel="bookmark" title="Splash window for deployed applications">Splash window for deployed applications </a> <small>Deployed (compiled) Matlab applications take a long time to load. I present a splash window that loads immadiately, solving this problem. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events-in-r2014a" rel="bookmark" title="Matlab callbacks for Java events in R2014a">Matlab callbacks for Java events in R2014a </a> <small>R2014a changed the way in which Java objects expose events as Matlab callbacks. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><i>I would like to welcome repeat guest blogger Karthik Ponudurai, who has previously written here about the <a target="_blank" href="/articles/jtattoo-look-and-feel-demo">JTattoo look-and-feel</a>, and about <a target="_blank" href="/articles/matlab-java-interface-using-static-control">integration of Java controls&#8217; events with Matlab callbacks</a>. Today, Karthik discusses a problem when we wish to use pure-Java GUIs in deployed Matlab programs, and the undocumented simple solution.<br />
<b><u>Reminder</u></b>: I will be visiting several US cities (Minneapolis, Boston and New York) in July 22-31 (<a target="_blank" href="/articles/usa-visit-july-2014">details</a>). Let me know if you&#8217;d like to meet me there.</i></p>
<h3 id="introduction">Introduction</h3>
<p>Using a pure-Java Swing-based Graphical User Interface (GUI) has several important advantages compared to using a pure-Matlab GUI:</p>
<ul>
<li>Java GUI widget toolkit provides a large collection of components (scrollbar, slider, etc&#8230;) and layouts (card, spring, etc&#8230;)</li>
<li>Multiple event handling options</li>
<li>Mature third-party Java Swing IDEs for quick and easy GUI development (Netbeans, Eclipse, etc&#8230;)</li>
<li>Supports pluggable look and feel that allows applications to have a look and feel that is unrelated to the underlying platform.</li>
<li>Java Swing&#8217;s window icon can be modified, whereas Mathworks apparently places an [<i>entirely unreasonable</i>] license restriction on modifying the Matlab figure window icon.</li>
</ul>
<p>When Matlab is in development (non-deployed) mode, we can load and display a Java <code>JFrame</code> using the following Matlab code:</p>
<pre lang='matlab'>
% Add Java library to dynamic Java classpath
javaaddpath([pwd '\ExampleWindow.jar']);
% Get example Java window from the library
jFrame = examplewindow.JavaWindow();
% Get Java buttons
% Note: see https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events-in-r2014a
plotMeshButton = handle(jFrame.getPlotMeshButton(),    'CallbackProperties');
showWarnButton = handle(jFrame.getShowWarnDlgButton(), 'CallbackProperties');
% Set Java button callbacks
set(plotMeshButton, 'ActionPerformedCallback', @myPlotMeshCallback);
set(showWarnButton, 'ActionPerformedCallback', @myShowWarnCallback);
% Display the Java window
jFrame.setVisible(true);
</pre>
<p><center><a target="_blank" href="/images/JFrame1a.gif"><img decoding="async" alt="Java JFrame created in Matlab (click for full-size image)" src="https://undocumentedmatlab.com/images/JFrame1a.gif" title="Java JFrame created in Matlab (click for full-size image)" width="100%" style="max-width: 407px;" /></a><br />
Java JFrame created in Matlab (click for full-size image)</center></p>
<h3 id="problem">The problem</h3>
<p>All this works great when ran within the Matlab environment. However, when the Matlab source code file (.m) is compiled as a Windows standalone executable application, when running the resulting executable the Java window appears for a short period and then terminates (exits). Without a visible Matlab figure, the compiled Matlab application does not retain the Java window open. The JFrame flashes on for a split-second, and then vanishes.<br />
<span id="more-4909"></span></p>
<h3 id="solution">The solution</h3>
<p>To fix this problem, it turns out that we only need to add the Matlab <i><b>waitfor</b></i> command after making the Java window visible:</p>
<pre lang='matlab'>
% (earlier code as above)
% Display the Java window
jFrame.setVisible(true);
if isdeployed
    waitfor(jFrame);
end
</pre>
<p><center><a target="_blank" href="/images/JFrame2a.gif"><img decoding="async" alt="Java JFrame in deployed Matlab (click for full-size image)" src="https://undocumentedmatlab.com/images/JFrame2a.gif" title="Java JFrame in deployed Matlab (click for full-size image)" width="100%" style="max-width: 982px;" /></a><br />
Java JFrame in deployed Matlab (click for full-size image)</center><br />
The <i><b>waitfor</b></i> command holds the Java window open, but still enables full interactivity with the window: When a control event is triggered, the specified callback is executed, and then Matlab returns to the <i><b>waitfor</b></i>.<br />
A zip file containing the Matlab source-code and its resulting executable can be <a target="_blank" href="/files/JFrame_example_window.zip">downloaded from here</a>. You might get a security notification when downloading the zip file, due to the contained exe file. Also note that to run the executable, you need to install Matlab MCR R2012b (8.0) or later, otherwise you will get a run-time error.<br />
Finally, note that closing the Java GUI exits the JVM and by extension the entire containing program (Matlab). Naturally, this has no impact in deployed mode, but to prevent it from exiting the Matlab environment in non-deployed mode, don&#8217;t close the Java window by clicking its &#8220;X&#8221; button etc., but rather by calling <code>jFrame.dispose();</code> in your Matlab code or the Matlab Desktop&#8217;s Command Window.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps">Using pure Java GUI in deployed Matlab apps</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/removing-user-preferences-from-deployed-apps" rel="bookmark" title="Removing user preferences from deployed apps">Removing user preferences from deployed apps </a> <small>An unsupported MathWorks Technical Solution explains how to remove private information from deployed (compiled) matlab applications. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/speeding-up-compiled-apps-startup" rel="bookmark" title="Speeding up compiled apps startup">Speeding up compiled apps startup </a> <small>The MCR_CACHE_ROOT environment variable can reportedly help to speed-up deployed Matlab executables....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/splash-window-for-deployed-applications" rel="bookmark" title="Splash window for deployed applications">Splash window for deployed applications </a> <small>Deployed (compiled) Matlab applications take a long time to load. I present a splash window that loads immadiately, solving this problem. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events-in-r2014a" rel="bookmark" title="Matlab callbacks for Java events in R2014a">Matlab callbacks for Java events in R2014a </a> <small>R2014a changed the way in which Java objects expose events as Matlab callbacks. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps/feed</wfw:commentRss>
			<slash:comments>27</slash:comments>
		
		
			</item>
		<item>
		<title>Disabling menu entries in deployed docked figures</title>
		<link>https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=disabling-menu-entries-in-deployed-docked-figures</link>
					<comments>https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 14 Nov 2012 18:00:46 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Listeners]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Alexander Mering]]></category>
		<category><![CDATA[Callbacks]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Desktop]]></category>
		<category><![CDATA[Docking]]></category>
		<category><![CDATA[FindJObj]]></category>
		<category><![CDATA[JavaFrame]]></category>
		<category><![CDATA[Listener]]></category>
		<category><![CDATA[Menubar]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3344</guid>

					<description><![CDATA[<p>Matlab's standard menu items can and should be removed from deployed  docked figures. This article explains how. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures">Disabling menu entries in deployed docked figures</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications" rel="bookmark" title="Docking figures in compiled applications">Docking figures in compiled applications </a> <small>Figures in compiled applications cannot officially be docked since R2008a, but this can be done using a simple undocumented trick....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps" rel="bookmark" title="Using pure Java GUI in deployed Matlab apps">Using pure Java GUI in deployed Matlab apps </a> <small>Using pure-Java GUI in deployed Matlab apps requires a special yet simple adaptation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/jgraph-in-matlab-figures" rel="bookmark" title="JGraph in Matlab figures">JGraph in Matlab figures </a> <small>JGraph is a powerful open-source Java library that can easily be integrated in Matlab figures. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><i>Last week I presented an <a target="_blank" href="/articles/removing-user-preferences-from-deployed-apps/">article</a> explaining how to solve an issue with deployed (compiled) Matlab applications. Today I&#8217;d like to welcome guest blogger <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/authors/208302">Alexander Mering</a>, who will explain how to disable standard Matlab menu items in deployed docked Matlab figures. Alexander has been an avid follower of this blog, and from his <a target="_blank" rel="nofollow" href="https://www.mathworks.com/matlabcentral/newsreader/search_results?dur=all&#038;search_string=authorid%3A146471">CSSM posts</a> we can tell that he&#8217;s been heavily using advanced GUI features presented in this blog. His article today nicely shows how we can use different building-blocks presented in different articles in this blog, to achieve something new and useful.</i><br />
As Yair pointed out in many occasions, the power of Matlab could be greatly enhanced using the underlying Java mechanism. To me, while developing a larger standalone tool for technical calculations, these hints are worth a mint (as I guess for many of you).<br />
One of these very useful hints is the ability to <a target="_blank" href="/articles/docking-figures-in-compiled-applications/">dock figure windows in standalone applications</a>. This perfectly fits to my understanding of a &#8220;clean desktop&#8221;, i.e., having as less as possible separate windows. Since in many calculations dozens of figures are generated, the desktop gets up crowded very fast &#8211; if these are not grouped. So docking is essential (at least for me). Unfortunately there seems to be a serious problem with the resulting menu entries (at least in R2011b on Win XP), leading to a crash of the standalone application. Based on the posts by Yair, I will sketch a possible route to avoid this issue.</p>
<h3 id="symptom">The symptom</h3>
<p>In the compiled application, docking could be accomplished by accessing the figure frame&#8217;s underlying Java level:</p>
<pre lang='matlab'>
% get java frame for sophisticated modifications
jframe = get(handle(figure_handle), 'JavaFrame');
% allow docking
jframe.fHG1Client.setClientDockable(true)
</pre>
<p>Using this modification, the user is now allowed to dock and undock the figures manually. For initial docking of the figure,</p>
<pre lang='matlab'>javaFrame.fHG1Client.setClientWindowStyle(true,false)</pre>
<p>could be used during figure creation. Unfortunately, there are menu entries in the Figures container which are either unwanted (since not usable) or even directly crash the standalone applications:<br />
<span id="more-3344"></span><br />
<center><figure style="width: 320px" class="wp-caption aligncenter"><img decoding="async" alt="Useless Debug menu items in deployed applications" src="https://undocumentedmatlab.com/images/Useless_debug_menu.png" title="Useless Debug menu items in deployed applications" width="320" height="267" /><figcaption class="wp-caption-text">Useless Debug menu items in deployed applications</figcaption></figure></center><br />
<center><figure style="width: 504px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Menu items crashing deployed applications" src="https://undocumentedmatlab.com/images/Buggy_desktop_menu.png" title="Menu items crashing deployed applications" width="504" height="241" /><figcaption class="wp-caption-text">Menu items crashing deployed applications</figcaption></figure></center><br />
Since crashing menu entries will be found and used by end-users (though these are somehow hidden), these prohibit the usage of the docking feature as long as these could be invoked. So how can we disable / remove these menu items?</p>
<h3 id="unsuccessful">The unsuccessful solution</h3>
<p>Unfortunately, the straight forward solution of getting the handle to the Figures containers&#8217; menu bar and remove the unwanted items does not work. The reason for this is the (to me unexpected behavior) that the menu bar seems to be rebuilt whenever a figure is docked/undocked.<br />
This is actually the same behavior that automatically rebuilds the Editor&#8217;s menu bar whenever an editor file is added/removed. The Editor container is basically the same docking container as the Figures container, as shown by Yair&#8217;s <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/16650-setfigdockgroup"><i><b>setFigDockGroup</b></i> utility</a>:<br />
<center><figure style="width: 504px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Docking a figure in the Editor container (group)" src="https://undocumentedmatlab.com/images/setFigDockGroup.png" title="Docking a figure in the Editor container (group)" width="504" height="551" /><figcaption class="wp-caption-text">Docking a figure in the Editor container (group)</figcaption></figure></center><br />
Therefore, removing unwanted menu items only helps until the next figure docking/undocking. To make it even worse: also pressing any of the buttons within the document bar (if having more than one figure) somehow rebuilds the entire menu structure, reverting our changes. So the solution becomes a bit more complex.</p>
<h3 id="solution">The working solution</h3>
<p>For the working solution, many pieces presented by Yair should be put together. The first piece results from the question how to detect a dock/undock event. Since no such callback is defined, we need to use a property listener as Yair showed in his post about the <a target="_blank" href="/articles/continuous-slider-callback/">continuous slider callback</a>:</p>
<pre lang='matlab'>
% listen to the WindowStyle property to detect docking / undocking events
hProp = findprop(handle(figure_handle),'WindowStyle');  % a schema.prop object
% if the event occurs, invoke the callback
hlistener = handle.listener(handle(figure_handle), hProp, 'PropertyPostSet',{@(source, event) Callback_DockingFcn});
% attach listener to the GUI since it needs to be known (as long as the figure exists)
setappdata(figure_handle, 'Handle_Listener', hlistener);
</pre>
<p>Now, whenever the figure&#8217;s <b>WindowStyle</b> property (which controls the docking state) is changed, our docking callback is invoked.<br />
The next piece of the puzzle takes care of the menu rebuild whenever any document bar button is pressed. To overcome this behavior, the idea is to define the <b>MousePressed</b> callback of theses buttons to (again) invoke the docking callback. This is necessary for two reasons: First, pressing the button (i.e., changing the current figure) rebuilds the menu, overwriting our changed menu entries. Secondly, all other buttons are also somehow rebuilt and the callbacks are removed if a new figure is docked.<br />
The handles to the document bar buttons could be found using Yair&#8217;s <a target="_blank" href="/articles/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i> utility</a>. We have already seen that the Editor container is analogous to the Figures docking container. So let&#8217;s use the method described by Yair for <a target="_blank" href="/articles/accessing-the-matlab-editor/">accessing the Editor container</a>, to access the Figures container:</p>
<pre lang='matlab'>
figures_container = javaObjectEDT(matlab_instance.getGroupContainer('Figures'));
figures_frame = javaObjectEDT(figures_container.getTopLevelAncestor);
</pre>
<p>Once we get the Java Frame for the Figures container, the buttons could be found by digging through its children. This finally allows to set the callback using</p>
<pre lang='matlab'>
DTDocumentBar = javaObjectEDT(figures_frame.getRootPane.getLayeredPane.getComponent(1).getComponent(1).getComponent(0).getComponent(0).getComponent(1).getComponent(0));
ContentPanel = javaObjectEDT(DTDocumentBar.getComponent(0).getComponent(0).getViewport.getView);
if ~isempty(ContentPanel.getComponents) % less than two documents are open and no DTDocumentbar exists
    drawnow; pause(0.05)
    GroupPanel = javaObjectEDT(ContentPanel.getComponent(0));
    GroupPanel_Elements = javaObjectEDT(GroupPanel.getComponents);
    % change the MousePressed Callback for each of the buttons to invoke the function which disables the menu
    for n = 1 : GroupPanel.getComponentCount
        thisElement = GroupPanel_Elements(n);
        if isequal(char(thisElement.getClass.toString), 'class com.mathworks.widgets.desk.DTDocumentBar$DocumentButton')
            set(handle(thisElement, 'CallbackProperties'), 'MousePressedCallback', {@(source, event) Cbrake_Callback_Diagrams_DockingFcn})
        end
    end
    drawnow; pause(0.05)
end
</pre>
<p>where the loop runs through the current buttons in the document bar.<br />
As the last step of our procedure, we finally remove (or disable) the menu entries which are unwanted. This is achieved by extracting the handle to the Figures menu by:</p>
<pre lang='matlab'>figures_menu = javaObjectEDT(figures_frame.getJMenuBar);</pre>
<p>Running through the menu items, searching for the unwanted entries (as long as they have pre-defined <a target="_blank" href="/articles/modifying-default-toolbar-menubar-actions/">menu-item names</a>) at the end sets us into the position to take care of the menu items:</p>
<pre lang='matlab'>
% run through top-level menu items
for n = 1 : figures_menu.getMenuCount
    % completely deactivate Debugging options
    if isequal(char(figures_menu.getMenu(n-1).getName), 'DesktopDebugMenu')
        DesktopDebugMenuPos = n - 1;
    end
    % Remove some items from the Desktop menu
    if isequal(char(figures_menu.getMenu(n-1).getName), 'DesktopMenu')
        desktop_menu = javaObjectEDT(figures_menu.getMenu(n-1));
        DeletePos = [];
        for m = 1: desktop_menu.getMenuComponentCount
            if ismember({char(desktop_menu.getMenuComponent(m-1).getName)}, ...
                        {'ToggleFigure PaletteCheckBoxMenuItem', 'TogglePlot BrowserCheckBoxMenuItem', 'ToggleProperty EditorCheckBoxMenuItem'})
                DeletePos(end+1) = m - 1;
            end
        end
        for m = length(DeletePos) : -1 : 1
            desktop_menu.remove(DeletePos(m))
        end
    end
end
% finally remove the "Debug" menu
if ~isempty(DesktopDebugMenuPos)
    figures_menu.remove(DesktopDebugMenuPos)
end
</pre>
<p>Since this callback is invoked whenever a figure is docked/undocked, or the currently shown figure is changed (by pressing the document bar button), all unwanted menu items within the Figures menu could be removed.<br />
As a result, the new Figures container menu looks like:<br />
<center><figure style="width: 319px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Deployed menu without unwanted items" src="https://undocumentedmatlab.com/images/Reduced_figures_menu.png" title="Deployed menu without unwanted items" width="319" height="205" /><figcaption class="wp-caption-text">Deployed menu without unwanted items</figcaption></figure></center></p>
<h3 id="remarks">Remarks</h3>
<p>I must admit that above solution is still imperfect. For instance, sometimes there is a larger delay between the docking (or button press event) and the removing of the menu item. Nevertheless, this solution allows me to distribute my standalone with docked figures without having menu items directly leading to a fatal error.<br />
Obviously, the solution has some positive side effects:</p>
<ul>
<li>As could be seen from the screen shot, the Matlab desktop becomes available also within your compiled applications. This might be wanted. If not, it could be removed the same way as the other menu items. One drawback of making the desktop available should be mentioned: In my tests, the standalone Matlab desktop shows the whole list of recent files I have in the Matlab editor at compile time. This is somehow ugly but not that problematic.</li>
<li>Additional menu items could be added, giving more possibilities for modifications.</li>
</ul>
<p>I have uploaded a first version of the docking and creation functions, together with a small test project, to the <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/39026-figures-menu-adaption-for-standalone-applications">Matlab file Exchange</a>. Readers are welcome to download the code and send me improvement suggestions. Or you could simply <a href="/articles/disabling-menu-entries-in-deployed-docked-figures#respond">leave a comment</a> below.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures">Disabling menu entries in deployed docked figures</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications" rel="bookmark" title="Docking figures in compiled applications">Docking figures in compiled applications </a> <small>Figures in compiled applications cannot officially be docked since R2008a, but this can be done using a simple undocumented trick....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps" rel="bookmark" title="Using pure Java GUI in deployed Matlab apps">Using pure Java GUI in deployed Matlab apps </a> <small>Using pure-Java GUI in deployed Matlab apps requires a special yet simple adaptation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/jgraph-in-matlab-figures" rel="bookmark" title="JGraph in Matlab figures">JGraph in Matlab figures </a> <small>JGraph is a powerful open-source Java library that can easily be integrated in Matlab figures. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures/feed</wfw:commentRss>
			<slash:comments>14</slash:comments>
		
		
			</item>
		<item>
		<title>Removing user preferences from deployed apps</title>
		<link>https://undocumentedmatlab.com/articles/removing-user-preferences-from-deployed-apps?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=removing-user-preferences-from-deployed-apps</link>
					<comments>https://undocumentedmatlab.com/articles/removing-user-preferences-from-deployed-apps#respond</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 07 Nov 2012 18:00:19 +0000</pubDate>
				<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Mathworks.com]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3302</guid>

					<description><![CDATA[<p>An unsupported MathWorks Technical Solution explains how to remove private information from deployed (compiled) matlab applications. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/removing-user-preferences-from-deployed-apps">Removing user preferences from deployed apps</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps" rel="bookmark" title="Using pure Java GUI in deployed Matlab apps">Using pure Java GUI in deployed Matlab apps </a> <small>Using pure-Java GUI in deployed Matlab apps requires a special yet simple adaptation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/changing-system-preferences-programmatically" rel="bookmark" title="Changing system preferences programmatically">Changing system preferences programmatically </a> <small>Matlab user/system preferences can be changed programmatically, from within your Matlab application or from the Matlab desktop command prompt. This post details how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/splash-window-for-deployed-applications" rel="bookmark" title="Splash window for deployed applications">Splash window for deployed applications </a> <small>Deployed (compiled) Matlab applications take a long time to load. I present a splash window that loads immadiately, solving this problem. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/speeding-up-compiled-apps-startup" rel="bookmark" title="Speeding up compiled apps startup">Speeding up compiled apps startup </a> <small>The MCR_CACHE_ROOT environment variable can reportedly help to speed-up deployed Matlab executables....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Every now and then I stumble upon a MathWorks <a target="_blank" rel="nofollow" href="http://www.mathworks.com/support/product/technical-solutions-index.html">technical note/solution</a> that uses undocumented features. I already posted a few of these here (e.g., <a target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-3SR0YI/">this one</a> for the <a target="_blank" href="/articles/continuous-slider-callback/">continuous slider movement</a> issue, and <a target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-3MY8PN/">this one</a> for the <a target="_blank" href="/articles/minimize-maximize-figure-window/">figure maximization/minimization</a> issue). In fact, you may be surprised to know that there are dozens of other official technical notes, solutions and articles that use undocumented/unsupported Matlab functionality. Yummy!<br />
Of the numerous official resources containing unofficial information (I can&#8217;t help but smile), do you have a personal favorite? If so, please post a link in a <a href="/articles/removing-user-preferences-from-deployed-apps/#respond">comment</a> below, with a short description of what it is about.<br />
Today, I show one such technical solution (<a target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-1UOGW7/">http://www.mathworks.com/support/solutions/en/data/1-1UOGW7/</a>) that solves a privacy issue in deployed (compiled) Matlab applications. IMHO, the privacy issue is generally harmless, but users can judge by themselves. Obviously, if someone is stupid enough to store real secret information in the Matlab preferences, then the privacy issue could become more important.<br />
<span id="more-3302"></span><br />
The solution refers to Matlab release 7.1 (R14SP3), but is also relevant for later releases. I will let the text speak for itself:</p>
<blockquote><p><i></p>
<h4>When I execute an application compiled using MATLAB Compiler 4.3 (R14SP3), how can I prevent personal information such as my MATLAB preferences information from being extracted?</h4>
<h3 id="problem">Problem Description:</h3>
<p>I have compiled an application &#8216;test&#8217; using MATLAB Compiler 4.3 (R14SP3). Upon execution I noticed that a new folder gets created in the &#8216;test_mcr&#8217; folder. This folder is named &#8216;test_<string of numbers and letters>&#8216;.<br />
Opening this folder revealed that some of the files were stored in my MATLAB preferences directory, including my MATLAB preferences file.<br />
I want to know if there is any way to avoid the creation of this directory when my application executes.</p>
<h3 id="solution">Solution:</h3>
<p>It is not possible to avoid having MATLAB Compiler include the preferences directory in each compiled application. The Compiler includes the user&#8217;s preference information because the application may rely on it. There are two possible workarounds:<br />
1) On UNIX, use SETENV and the ! operator to compile. For example:</p>
<pre lang='matlab'>
!setenv MATLAB_PREFDIR /tmp/emptydir;
mcc -Nmgv test
</pre>
<p>On Windows, open a DOS command window and execute</p>
<pre lang='dos'>set -- USERPROFILE = C:\Temp\emptydir</pre>
<p>Start MATLAB from this DOS prompt (to use the appropriate environment variable in the MATLAB session) and execute the MCC command at the MATLAB command prompt.<br />
Note: If you are using MATLAB Compiler 4.4 (R2006a) you can also use the SETENV function at the MATLAB command prompt</p>
<pre lang='matlab'>
setenv('-- USERPROFILE','C:\Temp\emptydir');
mcc -Nmgv test
</pre>
<p>This temporarily sets the preference directory to /tmp/emptydir or C:\Temp\emptydir, and the compiled application will therefore only have the default settings (or the preference directory settings from your installation of MATLAB 7.0.4 (R14SP2)).<br />
2) Move the contents of the preference directory, or rename the directory, before compilation, and restore afterwards. To do this on Windows, for example, execute the following command to compile the function test.m:</p>
<pre lang='matlab'>mymcc -m test.m</pre>
<p>The code of the function mymcc is:</p>
<pre lang='matlab'>function mymcc(varargin)
% MYMCC does the same as MCC. The only difference is that the created
% application doesn't contain information of the MATLAB
% prefdir anymore.
%
% For the description of possible options please look at the help of
% the command MCC.
command = 'mcc ';
for i = 1 : length(varargin)
   command = [command,varargin{i},' '];
end
a = prefdir;
b = regexp(a,'\','start');
c = a(b(end)+1:end);
p = cd;
cd(prefdir)
cd ..
!md tmp
cd(prefdir)
!move * ../tmp
cd(p)
% Because of the displacement MATLAB cannot access the saved preferences
% and that's why you have to indicate each time, which compiler shall be used.
try
   eval(command);
catch
   warning('WarnTests:Warning', ...
           'Calling mcc causes Error. Please check your input arguments.\n No file was compiled ...\n')
end
cd(prefdir)
cd ..
cd tmp
eval(['!move * ../',c]);
cd ..
!rmdir tmp
cd(p);
</pre>
<p>A similar function could also be written for the UNIX platform.</string></i><br />
<b>Note that the above workaround is not officially supported by MathWorks.</b><br />
<i></p>
<h3 id="related">Related Solutions:</h3>
<p><a target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-9BKF3F/">Why are my preference files included in my stand-alone application created with MATLAB Compiler 4.9 (R2008b)?</a><br />
Problem Description:<br />
If I look in the cache folder that my compiled application creates, I can see that the .matlab folder contains my preference files with my last development activities in plain text. The files are: cwdhistory.m, history.m, matlab.prf, MATLAB_Editor_State.xml, etc. How can I avoid the inclusion of these files in the stand-alone application?<br />
Solution:<br />
The stand-alone does not include your preference files. You can see the preference files in the cache folder because you are running the stand-alone in your development environment. At runtime, the stand-alone determines those preferences from a local MATLAB installation and copies them to the cache folder for it&#8217;s own use. If you run your stand-alone application on a machine without a MATLAB installation, you will see that the cache folder does not contain your preference files.<br />
</i></p></blockquote>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/removing-user-preferences-from-deployed-apps">Removing user preferences from deployed apps</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps" rel="bookmark" title="Using pure Java GUI in deployed Matlab apps">Using pure Java GUI in deployed Matlab apps </a> <small>Using pure-Java GUI in deployed Matlab apps requires a special yet simple adaptation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/changing-system-preferences-programmatically" rel="bookmark" title="Changing system preferences programmatically">Changing system preferences programmatically </a> <small>Matlab user/system preferences can be changed programmatically, from within your Matlab application or from the Matlab desktop command prompt. This post details how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/splash-window-for-deployed-applications" rel="bookmark" title="Splash window for deployed applications">Splash window for deployed applications </a> <small>Deployed (compiled) Matlab applications take a long time to load. I present a splash window that loads immadiately, solving this problem. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/speeding-up-compiled-apps-startup" rel="bookmark" title="Speeding up compiled apps startup">Speeding up compiled apps startup </a> <small>The MCR_CACHE_ROOT environment variable can reportedly help to speed-up deployed Matlab executables....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/removing-user-preferences-from-deployed-apps/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Speeding up compiled apps startup</title>
		<link>https://undocumentedmatlab.com/articles/speeding-up-compiled-apps-startup?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=speeding-up-compiled-apps-startup</link>
					<comments>https://undocumentedmatlab.com/articles/speeding-up-compiled-apps-startup#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 22 Aug 2012 20:22:49 +0000</pubDate>
				<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3085</guid>

					<description><![CDATA[<p>The MCR_CACHE_ROOT environment variable can reportedly help to speed-up deployed Matlab executables.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/speeding-up-compiled-apps-startup">Speeding up compiled apps startup</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/removing-user-preferences-from-deployed-apps" rel="bookmark" title="Removing user preferences from deployed apps">Removing user preferences from deployed apps </a> <small>An unsupported MathWorks Technical Solution explains how to remove private information from deployed (compiled) matlab applications. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/quirks-with-compiled-matlab-dlls" rel="bookmark" title="Quirks with compiled Matlab DLLs">Quirks with compiled Matlab DLLs </a> <small>Several quirks with Matlab-compiled DLLs are discussed and workarounds suggested. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps" rel="bookmark" title="Using pure Java GUI in deployed Matlab apps">Using pure Java GUI in deployed Matlab apps </a> <small>Using pure-Java GUI in deployed Matlab apps requires a special yet simple adaptation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications" rel="bookmark" title="Docking figures in compiled applications">Docking figures in compiled applications </a> <small>Figures in compiled applications cannot officially be docked since R2008a, but this can be done using a simple undocumented trick....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>A few weeks ago, I discussed a <a target="_blank" href="/articles/splash-window-for-deployed-applications/">splash wrapper application</a> that alleviates much of the pain of the slow startup of deployed (compiled) Matlab applications. While such a splash wrapper is indeed useful, it may also be possible to achieve an actual speedup of the compiled app&#8217;s startup using the <a target="_blank" rel="nofollow" href="http://www.mathworks.com/help/compiler/mcr-component-cache-and-ctf-archive-embedding.html">MCR_CACHE_ROOT</a> environment variable.<br />
The following has been reported to me by a reader of this blog. Please note that I cannot independently confirm the correctness of the report (if anyone can let me know I would be grateful):<br />
Normally, the MCR and the stand-alone executable is unpacked upon every startup in the user&#8217;s temp dir, and deleted when the user logs out. Apparently, when the MCR_CACHE_ROOT environment variable is set, these files are only unpacked once and kept for later reuse. If this report is indeed true, this could significantly speed up the startup time of a compiled application in subsequent invocations.<br />
On Linux:</p>
<pre lang='bash'>
export MCR_CACHE_ROOT=/tmp/mcr_cache_root_$-- USER   # local to host
mkdir -p @MCR_CACHE_ROOT
./myExecutable
</pre>
<p>On Windows:</p>
<pre lang='dos'>
REM set MCR_CACHE_ROOT=%TEMP%
set MCR_CACHE_ROOT="C:\Documents and Settings\Yair\Matlab Cache\"
myExecutable.exe
</pre>
<p><span id="more-3085"></span><br />
If you wish to set this env variable permanently on Windows, look at the explanation provided <a target="_blank" rel="nofollow" href="http://comp.uark.edu/~npai/StreBanD/FAQ.html">here</a>.<br />
Setting MCR_CACHE_ROOT is especially important when running the executable from a network (NFS) location, since unpacking onto a network location could be quite slow. If the executable is run in parallel on different machines (for example, a computer cluster running a parallel program), then this might even <a target="_blank" rel="nofollow" href="http://bioinfo.ittc.ku.edu/faq.php?type=11#47">cause lock-outs</a> when different clusters try to access the same network location. In both cases, the solution is to set MCR_CACHE_ROOT to a local folder (e.g., /tmp or %TEMP%). If you plan to reuse the extracted files again, then perhaps you should not delete the extracted files but reuse them. Otherwise, simply delete the temporary folder after the executable ends. In the following example, $RANDOM is a bash function that returns a random number:</p>
<pre lang='bash'>
export MCR_CACHE_ROOT=/tmp/mcr$RANDOM
./matlab_executable
rm -rf $MCR_CACHE_ROOT
</pre>
<p>Setting MCR_CACHE_ROOT can also be used to solve other performance bottlenecks in deployed applications, as explained in a <a target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-7RH0IV/">MathWorks technical solution</a> and a related article <a target="_blank" rel="nofollow" href="http://matlab4compbio.blogspot.co.il/2010/04/how-to-solve-mcr-cache-access-problems.html">here</a>.<br />
In a related matter, compiled Matlab executables may fail with a <code>Could not access the MCR component cache</code> error, when Matlab cannot write in the MCR cache directory due to missing permission rights. This can be avoided by setting MCR_CACHE_ROOT to a <a target="_blank" rel="nofollow" href="http://wiki.rcs.manchester.ac.uk/community/MatlabWithCondor#MCR_CACHE_ROOT">non-existent directory</a>, or to a folder in which there is global access permissions (/tmp or %TEMP% are usually such writable folders) &#8211; see related posts <a target="_blank" rel="nofollow" href="https://www.mathworks.com/matlabcentral/newsreader/view_thread/283432">here</a> and <a target="_blank" rel="nofollow" href="https://www.mathworks.com/matlabcentral/newsreader/view_thread/255547">here</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/speeding-up-compiled-apps-startup">Speeding up compiled apps startup</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/removing-user-preferences-from-deployed-apps" rel="bookmark" title="Removing user preferences from deployed apps">Removing user preferences from deployed apps </a> <small>An unsupported MathWorks Technical Solution explains how to remove private information from deployed (compiled) matlab applications. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/quirks-with-compiled-matlab-dlls" rel="bookmark" title="Quirks with compiled Matlab DLLs">Quirks with compiled Matlab DLLs </a> <small>Several quirks with Matlab-compiled DLLs are discussed and workarounds suggested. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps" rel="bookmark" title="Using pure Java GUI in deployed Matlab apps">Using pure Java GUI in deployed Matlab apps </a> <small>Using pure-Java GUI in deployed Matlab apps requires a special yet simple adaptation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications" rel="bookmark" title="Docking figures in compiled applications">Docking figures in compiled applications </a> <small>Figures in compiled applications cannot officially be docked since R2008a, but this can be done using a simple undocumented trick....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/speeding-up-compiled-apps-startup/feed</wfw:commentRss>
			<slash:comments>32</slash:comments>
		
		
			</item>
		<item>
		<title>Splash window for deployed applications</title>
		<link>https://undocumentedmatlab.com/articles/splash-window-for-deployed-applications?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=splash-window-for-deployed-applications</link>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 26 Jul 2012 17:46:39 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Performance]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3028</guid>

					<description><![CDATA[<p>Deployed (compiled) Matlab applications take a long time to load. I present a splash window that loads immadiately, solving this problem. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/splash-window-for-deployed-applications">Splash window for deployed applications</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps" rel="bookmark" title="Using pure Java GUI in deployed Matlab apps">Using pure Java GUI in deployed Matlab apps </a> <small>Using pure-Java GUI in deployed Matlab apps requires a special yet simple adaptation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/removing-user-preferences-from-deployed-apps" rel="bookmark" title="Removing user preferences from deployed apps">Removing user preferences from deployed apps </a> <small>An unsupported MathWorks Technical Solution explains how to remove private information from deployed (compiled) matlab applications. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications" rel="bookmark" title="Docking figures in compiled applications">Docking figures in compiled applications </a> <small>Figures in compiled applications cannot officially be docked since R2008a, but this can be done using a simple undocumented trick....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures" rel="bookmark" title="Disabling menu entries in deployed docked figures">Disabling menu entries in deployed docked figures </a> <small>Matlab's standard menu items can and should be removed from deployed docked figures. This article explains how. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>One of the most annoying features of the Matlab compiler is the fact that compiled (deployed) applications take a loooooooooong time to load the first time that they are run on the target platform, or after a computer restart. This can take anywhere from 15-60 seconds, depending on platform characteristics. During this time, there is absolutely no visual indication that the application is loading. The application displays no window or other visual cue to let the user know something is loading.<br />
I have seen many users double-click the application icon again and again in frustration, thinking that perhaps the application did not start when they first tried. In fact, this launches multiple applications and MCR loads at the same time, which only prolongs the overall time that it takes the application to actually load and display.</p>
<p>The &#8220;normal&#8221; solution for this would be to display a splash figure or start-up message at the very beginning of the application. Unfortunately, this doesn&#8217;t help because the vast majority of the launch time is due to MCR load time, which happens before any user code is executed. The result is that our application&#8217;s splash figure or msgbox will only be displayed after the very long time in which the application appears unresponsive.</p>
<h3 id="solution">Splash-window solution</h3>
<p><span class="alignright"><img loading="lazy" decoding="async" title="splash wrapper for deployed Matlab applications" src="https://undocumentedmatlab.com/images/splash_animated.gif" alt="splash wrapper for deployed Matlab applications" width="400" height="400"/></span> I developed a splash-screen wrapper application for deployed applications that solves this problem on Windows platforms.</p>
<p>The idea is to create a stand-alone non-Matlab application that displays a user-specified image (splash) in a dedicated window at the center of the screen, and then immediately launches the requested deployed Matlab application in the background. The wrapper then waits for the Matlab application to load, and automatically closes the splash window when it detects that the target Matlab GUI has finally displayed.</p>
<p>The effect is that upon launching the application, the user sees immediate feedback in the form of a dedicated splash window (for example, displaying an image of the application or company logo with the message &#8220;Loading &#8211; please wait&#8230;&#8221;). This does not make the target application load any faster, but at least users are given immediate feedback that prevents them from re-launching the application. Users also report that this makes the application appear to load faster. This is due to a psychological effect called &#8220;<i>perceived performance</i>&#8221; &#8211; we also see this effect with phone ringtones and elevator floor-level indicators.</p>
<p>My splash wrapper application receives the following input parameters:</p>
<ol>
<li>splash image filepath (BMP/JPG/GIF &#8211; note that other formats are NOT supported)</li>
<li>splash image width in pixels (will resize the image if necessary)</li>
<li>splash image height in pixels (will resize the image if necessary)</li>
<li>target window title string</li>
<li>title matching mode:
<ul>
<li>1 = match at the beginning of the title (case sensitive)</li>
<li>2 = match at any place within the title (case sensitive)</li>
<li>3 = match the entire title (case sensitive)</li>
<li>-1, -2, -3 = case-insensitive version of the above values</li>
</ul>
</li>
<li>target application filepath (this is the actual deployed application executable)</li>
<li>optional input parameters for the target application</li>
</ol>
<p><span id="more-3028"></span><br />
Sample usage:</p>
<pre lang='c'>splash SplashImage.jpg 600 450 "My GUI Title" 1 "C:\Program Files\MyApps\MyApplication.exe" param1 param2</pre>
<p>This will display the <i>SplashImage.jpg</i> image in a 600&#215;450 window (resizing the image if needed) until a window with a title of &#8220;My GUI Title&#8221; appears. In the example above, the parameters to the splash application were as follows:</p>
<ol>
<li>image filepath &#8211; SplashImage.jpg</li>
<li>image width &#8211; 600 pixels</li>
<li>image height &#8211; 450 pixels</li>
<li>target window title string &#8211; &#8220;My GUI Title&#8221;</li>
<li>title matching mode &#8211; 1 (=match at the beginning of the title)</li>
<li>target application &#8211; &#8220;C:\Program Files\MyApps\MyApplication.exe&#8221;</li>
<li>optional input parameters to your target application &#8211; param1, param2</li>
</ol>
<p>You can create a shortcut that runs the above command-line and then when the user clicks the shortcut it will automatically launch splash.exe, which will automatically display the splash screen and then load your main application.</p>
<p>I&#8217;ve contributed dozens of useful open-source utilities to the Matlab user community over the years (look at <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/authors/27420">my File Exchange profile</a>). This utility is different because it directly targets compiled applications, which are typically used commercially. For this reason, my splash wrapper application is not free (but also not very expensive). You can purchase a copy <a target="_blank" rel="nofollow" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&#038;business=altmany@gmail.com&#038;currency_code=USD&#038;amount=180&#038;return=&#038;item_name=Splash+wrapper+application">here</a>, and I will email it to you.</p>
<p>Please note: this is a Windows application. It will not work on Macs or Linux.</p>
<p>Interested in learning more about actual and perceived performance improvement tricks in Matlab? Consider attending my <a target="_blank" href="/courses/Matlab_Performance_Tuning_Course.pdf">Matlab Performance Tuning</a> course – <a href="mailto:%20altmany%20@gmail.com?subject=Matlab%20courses&amp;body=Hi%20Yair,%20&amp;cc=;&amp;bcc=" rel="nofollow" target="_blank" onclick="var n='altmany'; var d='gmail.com'; window.open('mailto:'+n+'@'+d+'?subject=Matlab seminars&amp;body=Hi Yair, '); return false;">email me</a> (altmany at gmail dot com) for details. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/splash-window-for-deployed-applications">Splash window for deployed applications</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps" rel="bookmark" title="Using pure Java GUI in deployed Matlab apps">Using pure Java GUI in deployed Matlab apps </a> <small>Using pure-Java GUI in deployed Matlab apps requires a special yet simple adaptation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/removing-user-preferences-from-deployed-apps" rel="bookmark" title="Removing user preferences from deployed apps">Removing user preferences from deployed apps </a> <small>An unsupported MathWorks Technical Solution explains how to remove private information from deployed (compiled) matlab applications. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications" rel="bookmark" title="Docking figures in compiled applications">Docking figures in compiled applications </a> <small>Figures in compiled applications cannot officially be docked since R2008a, but this can be done using a simple undocumented trick....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures" rel="bookmark" title="Disabling menu entries in deployed docked figures">Disabling menu entries in deployed docked figures </a> <small>Matlab's standard menu items can and should be removed from deployed docked figures. This article explains how. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Unique computer ID</title>
		<link>https://undocumentedmatlab.com/articles/unique-computer-id?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=unique-computer-id</link>
					<comments>https://undocumentedmatlab.com/articles/unique-computer-id#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 22 Jun 2011 21:38:28 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2353</guid>

					<description><![CDATA[<p>There are several alternatives for getting unique computer identifiers in Matlab</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/unique-computer-id">Unique computer ID</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/controlling-callback-re-entrancy" rel="bookmark" title="Controlling callback re-entrancy">Controlling callback re-entrancy </a> <small>Callback reentrancy is a major problem for frequently-fired events. Luckily, it can easily be solved....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/matlab-compiler-bug-and-workaround" rel="bookmark" title="Matlab compiler bug and workaround">Matlab compiler bug and workaround </a> <small>Both the Matlab compiler and the publish function have errors when parsing block-comments in Matlab m-code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects" rel="bookmark" title="Types of undocumented Matlab aspects">Types of undocumented Matlab aspects </a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>It is sometimes beneficial to have a unique identifier for the system on which we are currently running. For example, if you sell software, you may wish to verify that the computer is licensed or activated. A <a target="_blank" rel="nofollow" href="https://www.mathworks.com/matlabcentral/newsreader/view_thread/309603">question</a> on CSSM today reminded me of this issue.<br />
A trivial solution to this question is to use the built-in <i><b>license</b></i> function. Unfortunately, this does not work with a multi-system network/floating Matlab license, nor on deployed (compiled) systems.<br />
While Matlab doesn&#8217;t have a built-in solution, we can use simple Java to access system information. There are several possible approaches. Here are several alternatives:</p>
<h3 id="Win-SID">Windows SID</h3>
<p>A Windows-specific approach is to return the Window Domain Controller&#8217;s <a target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Security_Identifier">SID</a> (Security ID). This is a unique identifier that changes with each computer/user. Java enables direct access to this identifier, and we can run this directly in Matlab:</p>
<pre lang='matlab'>
>> sid = get(com.sun.security.auth.module.NTSystem,'DomainSID')
sid =
S-1-5-21-292311649-1610625687-3346456317
</pre>
<p>The exact same value can also be gotten directly from the Windows Registry:</p>
<pre lang='matlab'>
% Note: scanning HKEY_-- USERS node names is better, but Matlab's winqueryreg() can't do that...
rootkey = 'HKEY_CURRENT_-- USER';
subkey = 'Software\Microsoft\Windows\CurrentVersion\Group Policy\GroupMembership';
count = winqueryreg(rootkey,subkey,'Count');
for idx = 0 : double(count)-1   % Note: double() is needed for Matlab 6 compatibility
    val = winqueryreg(rootkey,subkey,['Group' char('0'+idx)]);
    dashes = find(val=='-');
    if length(dashes) > 4,  sid = val(1:dashes(end)-1);  break;  end   % short ids are phoney
end
</pre>
<p>This later version, although less simple than the first alternative (Java-based approach) above, has the benefit of working on old Matlab releases such as R12 (6.0) where the Java approach fails. For this reason, my <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/15718"><i><b>getsid</b></i> utility</a> on the File Exchange uses the registry approach.</p>
<h3 id="Non-Windows">Other platforms</h3>
<p>For non-Windows systems, both of the above approaches fail. For such platforms, we can use the Ethernet addresses of the computer&#8217;s network cards. This should be pretty unique for any practical effect:</p>
<pre lang='matlab'>
sid = '';
ni = java.net.NetworkInterface.getNetworkInterfaces;
while ni.hasMoreElements
    addr = ni.nextElement.getHardwareAddress;
    if ~isempty(addr)
        addrStr = dec2hex(int16(addr)+128);
        sid = [sid, '.', reshape(addrStr,1,2*length(addr))];
    end
end
>> sid
sid =
.89ECC872C85C.8AFE928FEDB4.8262278A1CCA.899919B50FC9
</pre>
<p>This again uses Java (which unfortunately fails on R12 aka 6.0). The benefit is that it is entirely cross-platform, working wherever Matlab runs (including Windows), on any Matlab release that supports Java (I think R13 aka 6.5 should be the earliest).<br />
Do you have another way to generate unique identifiers? If so, please share your experience in a <a target="_blank" href="/articles/unique-computer-id/#respond">comment</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/unique-computer-id">Unique computer ID</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/controlling-callback-re-entrancy" rel="bookmark" title="Controlling callback re-entrancy">Controlling callback re-entrancy </a> <small>Callback reentrancy is a major problem for frequently-fired events. Luckily, it can easily be solved....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/matlab-compiler-bug-and-workaround" rel="bookmark" title="Matlab compiler bug and workaround">Matlab compiler bug and workaround </a> <small>Both the Matlab compiler and the publish function have errors when parsing block-comments in Matlab m-code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects" rel="bookmark" title="Types of undocumented Matlab aspects">Types of undocumented Matlab aspects </a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/unique-computer-id/feed</wfw:commentRss>
			<slash:comments>27</slash:comments>
		
		
			</item>
		<item>
		<title>Docking figures in compiled applications</title>
		<link>https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=docking-figures-in-compiled-applications</link>
					<comments>https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 15 Jun 2011 18:00:56 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Docking]]></category>
		<category><![CDATA[Figure]]></category>
		<category><![CDATA[JavaFrame]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2341</guid>

					<description><![CDATA[<p>Figures in compiled applications cannot officially be docked since R2008a, but this can be done using a simple undocumented trick.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications">Docking figures in compiled applications</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures" rel="bookmark" title="Disabling menu entries in deployed docked figures">Disabling menu entries in deployed docked figures </a> <small>Matlab's standard menu items can and should be removed from deployed docked figures. This article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/speeding-up-compiled-apps-startup" rel="bookmark" title="Speeding up compiled apps startup">Speeding up compiled apps startup </a> <small>The MCR_CACHE_ROOT environment variable can reportedly help to speed-up deployed Matlab executables....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/quirks-with-compiled-matlab-dlls" rel="bookmark" title="Quirks with compiled Matlab DLLs">Quirks with compiled Matlab DLLs </a> <small>Several quirks with Matlab-compiled DLLs are discussed and workarounds suggested. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Up until Matlab release R2008a, the Matlab compiler enabled compiled Matlab applications to have dockable figure windows, which docked into a &#8220;Figures&#8221; container. <a target="_blank" rel="nofollow" href="https://www.mathworks.com/matlabcentral/answers/100942-why-is-it-no-longer-possible-to-dock-figures-generated-by-a-compiled-application-created-with-matlab">Starting with R2008a</a>, the compiler removed the figure&#8217;s docking capability and figures can no longer be docked.</p>
<p>Well, at least not officially 🙂</p>
<p><span style="background-color:#ffff00;"><b><u>Note</u>: If you are using Matlab R2013a or newer, the workarounds below will not work. Fortunately, I discovered a solution for this annoying limitation that I will NOT publish here &#8211; email me (<a href="mailto: altmany @gmail.com?subject=Deployed Matlab docking&amp;body=Hi Yair, &amp;cc=;&amp;bcc=" rel="nofollow" target="_blank" onclick="var n='altmany'; var d='gmail.com'; window.open('mailto:'+n+'@'+d+'?subject=Deployed Matlab docking&amp;body=Hi Yair, '); return false;"><img loading="lazy" decoding="async" style="vertical-align: middle; border: 0;" src="https://undocumentedmatlab.com/images/email-icon.png" alt="" width="16" height="11"> altmany at gmail</a>) if you need this</b></span>.</p>
<p>The following trick restores the docking controls to figures in R2008a-compiled applications, enabling figure docking. Simply add one or both of the following alternatives in your application, after the figure has been created:</p>
<pre lang='matlab'>
% Alternative #1 - uses pure Matlab
set(hFig, 'DockControls', 'on');

% Alternative #2 - uses the underlying Java frame
jFrame = get(handle(hFig), 'JavaFrame');
try
   % This works up to R2011a
   jFrame.fFigureClient.setClientDockable(true);
catch
   % This works from R2008b and up
   jFrame.fHG1Client.setClientDockable(true);
end
</pre>
<p>where <code>hFig</code> is the figure handle. This will have no effect for the regular interpreted (non-compiled) run of the application, where these controls are &#8216;on&#8217; by default. But in the compiled application, although it may erroneously report that the controls are &#8216;on&#8217;, they are in fact &#8216;off&#8217;, so turning them &#8216;on&#8217; fixes the problem.</p>
<p><center><figure style="width: 468px" class="wp-caption aligncenter"><img decoding="async" alt="Matlab figure docking control" src="https://undocumentedmatlab.com/images/dock4.png" title="Matlab figure docking control" width="468" /><figcaption class="wp-caption-text">Matlab figure docking control</figcaption></figure></center></p>
<p><u>Note</u>: the two variants in alternative #2 above are actually identical, it is simply that the relevant field name has changed: Up to R2008a, only the <code>fFigureClient</code> existed; in R2008b, the <code>fHG1Client</code> field was added, which was simply an alias for <code>fFigureClient</code>, holding the same reference handle, so either of these fields could be used (a corresponding fHG2Client was also added &#8211; more on HG1 and HG2 <a target="_blank" href="/articles/tag/hg2/">here</a>). In R2011b (at least the pre-release), the <code>fFigureClient</code> alias field was dropped and only <code>fHG1Client</code> remained. While the field name has changed, the underlying docking functionality appears to have remained stable over all these releases. For the record, in answer to a <a href="/articles/docking-figures-in-compiled-applications/#comment-48632">user question below</a>, these fields can be listed using the built in <i><b>fieldnames</b></i> function:</p>
<pre lang='matlab'>
% R2008b - R2011a:
>> fieldnames(jFrame)
ans =
    'fFigureClient'
    'fHG2Client'
    'fHG1Client'
    'fUseHG2'
    'UICONTROLBACKGROUND_OS'
    'UICONTROLBACKGROUND_COMPATIBLE'
</pre>
<p>I was reminded of this trick by <a target="_blank" href="/articles/tab-panels-uitab-and-relatives/#comment-47197">Aurélien&#8217;s recent comment</a>, where he mentions MathWorks so-called workaround for this problem, which (IMHO) is really not a work-around at all: MathWorks advises to modify our application to use &#8211; would you believe this &#8211; tabbed panels to &#8220;dock&#8221; the separate figures contents onto separate panels. Not to mention the fact that this so-called &#8220;solution&#8221; relies on undocumented and unsupported Matlab functionality (that of <a target="_blank" href="/articles/tab-panels-uitab-and-relatives/">tabbed-panels</a>) and requires major rework of existing applications, it also results in far inferior look-and-feel than simple docking as G-d intended&#8230;</p>
<p>Since I have demonstrated above that the docking functionality actually exists in compiled apps just as in the interpreted m-file apps, I do not understand why MathWorks took such great pains to prevent this important functionality in the compiler. There must be some important reason for this, but I cannot think of any. Perhaps if there is enough public demand, MathWorks will agree to return the docking functionality.</p>
<p>Unfortunately, I recently discovered that in the most recent compiler, that ships with R2011a, alternative #1 above (which uses pure Matlab) no longer works. Sometime between R2008a and R2011a MathWorks discovered my first back-door and closed it. Such a pity&#8230;<br />
Luckily, alternative #2 (which uses the underlying Java frame object) seems to still work, even on R2011a.</p>
<p>I still haven&#8217;t tested this on R2011b&#8217;s compiler (whose pre-release has become available for download yesterday), but hopefully the trick above will continue to work on R2011b and on subsequent releases &#8211; please <a href="/articles/docking-figures-in-compiled-applications/#respond">tell me</a> if you find out otherwise.</p>
<p><u><b>Addendum</b></u>: Since 2013, possibly as a direct result of this post, MathWorks have prevented this workaround. MathWorks <a href="https://www.mathworks.com/matlabcentral/answers/100942-why-is-it-no-longer-possible-to-dock-figures-generated-by-a-compiled-application-created-with-matlab" rel="nofollow" target="_blank">officially states</a> that docking figures is not possible in deployed applications. </p>
<p><span style="background-color:#ffff00;"><b>Fortunately, I discovered a solution for this annoying limitation that I will NOT publish here &#8211; email me (<a href="mailto: altmany @gmail.com?subject=Deployed Matlab docking&amp;body=Hi Yair, &amp;cc=;&amp;bcc=" rel="nofollow" target="_blank" onclick="var n='altmany'; var d='gmail.com'; window.open('mailto:'+n+'@'+d+'?subject=Deployed Matlab docking&amp;body=Hi Yair, '); return false;"><img loading="lazy" decoding="async" style="vertical-align: middle; border: 0;" src="https://undocumentedmatlab.com/images/email-icon.png" alt="" width="16" height="11"> altmany at gmail</a>) if you need this</b></span>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications">Docking figures in compiled applications</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures" rel="bookmark" title="Disabling menu entries in deployed docked figures">Disabling menu entries in deployed docked figures </a> <small>Matlab's standard menu items can and should be removed from deployed docked figures. This article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/speeding-up-compiled-apps-startup" rel="bookmark" title="Speeding up compiled apps startup">Speeding up compiled apps startup </a> <small>The MCR_CACHE_ROOT environment variable can reportedly help to speed-up deployed Matlab executables....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/quirks-with-compiled-matlab-dlls" rel="bookmark" title="Quirks with compiled Matlab DLLs">Quirks with compiled Matlab DLLs </a> <small>Several quirks with Matlab-compiled DLLs are discussed and workarounds suggested. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications/feed</wfw:commentRss>
			<slash:comments>30</slash:comments>
		
		
			</item>
	</channel>
</rss>
