<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Undocumented Matlab &#187; Desktop</title> <atom:link href="http://undocumentedmatlab.com/blog/category/desktop/feed/" rel="self" type="application/rss+xml" /><link>http://undocumentedmatlab.com</link> <description>Charting Matlab's unsupported hidden underbelly</description> <lastBuildDate>Thu, 17 May 2012 12:01:26 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.1</generator> <item><title>Recovering previous editor state</title><link>http://undocumentedmatlab.com/blog/recovering-previous-editor-state/</link> <comments>http://undocumentedmatlab.com/blog/recovering-previous-editor-state/#comments</comments> <pubDate>Wed, 11 Jan 2012 18:00:49 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Desktop]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Editor]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2656</guid> <description><![CDATA[Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/accessing-the-matlab-editor/' rel='bookmark' title='Accessing the Matlab Editor'>Accessing the Matlab Editor</a> <small>The Matlab Editor can be accessed programmatically, for a wide variety of possible uses - this article shows how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/non-textual-editor-actions/' rel='bookmark' title='Non-textual editor actions'>Non-textual editor actions</a> <small>The UIINSPECT utility can be used to expand EditorMacro capabilities to non-text-insertion actions. This is how:...</small></li><li><a
href='http://undocumentedmatlab.com/blog/editormacro-assign-a-keyboard-macro-in-the-matlab-editor/' rel='bookmark' title='EditorMacro &#8211; assign a keyboard macro in the Matlab editor'>EditorMacro &#8211; assign a keyboard macro in the Matlab editor</a> <small>EditorMacro is a new utility that enables setting keyboard macros in the Matlab editor. this post details its inner workings....</small></li><li><a
href='http://undocumentedmatlab.com/blog/tri-state-checkbox/' rel='bookmark' title='Tri-state checkbox'>Tri-state checkbox</a> <small>Matlab checkboxes can easily be made to support tri-state functionality....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><span
class="alignright"><img
title="Editor with multiple loaded documents" src="http://undocumentedmatlab.com/images/editor.png" alt="Editor with multiple loaded documents" width="300" height="380"/></span> I find it very useful to use the Matlab editor&#8217;s ability to load multiple files to help me remember which files I still need to work on. I typically have a few dozen files loaded in the editor. I guess you could say that the editor&#8217;s list of files is a simple reflection of my open tasks. It would be extremely inconvenient if I ever lost this list.</p><p>Which is, unfortunately, exactly what happened to me yesterday.</p><p>I was about to close a figure window and accidentally closed the editor window that was behind it.</p><p>I was now in quite a jam: reopening the editor would not load all my files, but start with a blank (empty) editor. The Matlab editor, unlike modern browsers, does not have a &#8216;reopen last session&#8217; option, and using the most-recently-used (MRU) files list would at best return the latest 9 files (the default Matlab preference is to have an MRU depth of only 4 files, but this is one of the very first things that I change whenever I install Matlab, along with a few other very questionable [IMHO] default preferences).</p><p>Luckily for me, there is an escape hatch: Matlab stores its Desktop windows state in a file called <i>MATLABDesktop.xml</i> that is located in the user&#8217;s <i><b>prefdir</b></i> folder. This file includes information about the position and state (docked/undocked etc.) of every Desktop window. Since the editor files are considered Desktop documents (you can see them under the Desktop&#8217;s main menu&#8217;s Window menu), they are also included in this file. When I closed the Editor, these documents were simply removed from the <i>MATLABDesktop.xml</i> file.</p><p>It turns out that Matlab automatically stores a backup version of this file in another file called <i>MATLABDesktop.xml.prev</i>, in the same <i><b>prefdir</b></i> folder. We can inspect the folder using our system&#8217;s file browser. For example, on Windows we could use:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">winopen<span style="color: #080;">&#40;</span>prefdir<span style="color: #080;">&#41;</span></pre></div></div><p>So before doing anything else, I closed Matlab (I actually crashed it to prevent any cleanup tasks to accidentally erase this backup file, but that turned out to be an unnecessary precaution), deleted the latest <i>MATLABDesktop.xml</i> file, replaced it with a copy of the <i>MATLABDesktop.xml.prev</i> file (which I renamed <i>MATLABDesktop.xml</i>), and only then did I restart Matlab.</p><p>Problem solved &#8211; everything back to normal. Resume breathing. Once again I have my never-ending virtual task list in front of me as I write this.</p><p>Lessons learned:</p><ol><li>don&#8217;t be too quick on the close button trigger</li><li>always keep a relatively recent backup copy of your important config files (BTW, I use the same advice with my FireFox browser, where I normally have dozens of open tabs &#8211; I keep a backup copy of the sessionstore.js file)</li><li>if you do get into a jam, don&#8217;t do anything hasty that might make recovery impossible. Calm down, look around, and maybe you&#8217;ll find an automatic backup somewhere</li></ol><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/accessing-the-matlab-editor/' rel='bookmark' title='Accessing the Matlab Editor'>Accessing the Matlab Editor</a> <small>The Matlab Editor can be accessed programmatically, for a wide variety of possible uses - this article shows how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/non-textual-editor-actions/' rel='bookmark' title='Non-textual editor actions'>Non-textual editor actions</a> <small>The UIINSPECT utility can be used to expand EditorMacro capabilities to non-text-insertion actions. This is how:...</small></li><li><a
href='http://undocumentedmatlab.com/blog/editormacro-assign-a-keyboard-macro-in-the-matlab-editor/' rel='bookmark' title='EditorMacro &#8211; assign a keyboard macro in the Matlab editor'>EditorMacro &#8211; assign a keyboard macro in the Matlab editor</a> <small>EditorMacro is a new utility that enables setting keyboard macros in the Matlab editor. this post details its inner workings....</small></li><li><a
href='http://undocumentedmatlab.com/blog/tri-state-checkbox/' rel='bookmark' title='Tri-state checkbox'>Tri-state checkbox</a> <small>Matlab checkboxes can easily be made to support tri-state functionality....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/recovering-previous-editor-state/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> <item><title>Command Window text manipulation</title><link>http://undocumentedmatlab.com/blog/command-window-text-manipulation/</link> <comments>http://undocumentedmatlab.com/blog/command-window-text-manipulation/#comments</comments> <pubDate>Wed, 28 Dec 2011 18:00:49 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Desktop]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2645</guid> <description><![CDATA[Special control characters can be used to format text output in Matlab's Command Window.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/' rel='bookmark' title='cprintf &#8211; display formatted color text in the Command Window'>cprintf &#8211; display formatted color text in the Command Window</a> <small>cprintf is a utility that utilized undocumented Matlab desktop functionalities to display color and underline-styled formatted text in the Command Window...</small></li><li><a
href='http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors-part2/' rel='bookmark' title='Changing Matlab&#8217;s Command Window colors &#8211; part 2'>Changing Matlab&#8217;s Command Window colors &#8211; part 2</a> <small>The Matlab Command Window enables a limited degree of inline color customization - this post describes how to use it...</small></li><li><a
href='http://undocumentedmatlab.com/blog/editormacro-v2-setting-command-window-key-bindings/' rel='bookmark' title='EditorMacro v2 &#8211; setting Command Window key-bindings'>EditorMacro v2 &#8211; setting Command Window key-bindings</a> <small>The EditorMacro utility was extended to support built-in Matlab Editor and Command-Window actions and key-bindings. This post describes the changes and the implementation details....</small></li><li><a
href='http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/' rel='bookmark' title='Changing Matlab&#8217;s Command Window colors'>Changing Matlab&#8217;s Command Window colors</a> <small>Matlab's Command Window foreground and background colors can be modified programmatically, using some of Matlab's undocumented internal Java classes. Here's how....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Sometimes the most obscure problems have simple solutions.</p><p>A few days ago, a Matlab user <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/315437">asked</a> whether it is possible to solve the flashing effect whenever he cleared the Command Window. It turns out that this user runs a long process, and wanted to display interim information (progress etc.) in the Command Window. The obvious solution that he employed was to display the data in the Command Window, and then erase the window (using <i><b>clc</b></i>) and rewrite the data periodically.</p><h3 id="Solution">Using control characters</h3><p>Now, there are obviously other ways of doing this, the most obvious being a dedicated GUI window that displays the information and updates periodically. But to solve the user&#8217;s immediate issue, the solution I <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/315437#862184">provided</a> (acting on the 80-20 Pareto principle &#8211; a dedicated GUI window is better but would take longer to set up) was to use control characters to erase old data when displaying new data, rather than clearing the window. Here is the basic pseudo-code that I suggested (slightly modified):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">reverseStr = <span style="color:#A020F0;">''</span>;
<span style="color: #0000FF;">for</span> idx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> someLargeNumber
&nbsp;
   <span style="color: #228B22;">% Do some computation here...</span>
&nbsp;
   <span style="color: #228B22;">% Display the progress</span>
   percentDone = <span style="color: #33f;">100</span> * idx / someLargeNumber;
   msg = <span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Percent done: %3.1f'</span>, percentDone<span style="color: #080;">&#41;</span>
   <span style="color: #0000FF;">fprintf</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span>reverseStr, msg<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
   reverseStr = <span style="color: #0000FF;">repmat</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'\b'</span><span style="color: #080;">&#41;</span>, <span style="color: #33f;">1</span>, <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>msg<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>The idea is to use the backspace control-character (<a
target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Backspace">BS</a>, or <i><b>sprintf</b></i>(&#8216;\b&#8217;), or <i><b>char</b></i>(8)) repeatedly in order to erase the preceding characters from the Command Window, then print the new data.</p><p>(see related comments by Helge <a
target="_blank" href="http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/#comment-56187">here</a>)</p><h3 id="Compatibility">Compatibility aspects</h3><p>Back in the good-ol&#8217;-days, when I was hacking away c-shell scripts, ages ago when I created command-prompt C apps, and aeons ago on Vax (R.I.P.), I would have used the carriage-return control-char (<a
target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Carriage_return">CR</a>, or <i><b>sprintf</b></i>(&#8216;\r&#8217;), or <i><b>char</b></i>(13)) to completely erase everything up to the beginning of the current line. This would be much simpler than the repeated backspaces (using <i><b>repmat</b></i> in the pseudo-code above). Unfortunately, CRs behave differently in Matlab&#8217;s Command Window, causing \r to jump to the next line (similarly to \n), rather than erase to the beginning of the line. This is probably due to Java&#8217;s JTextArea&#8217;s implementation (on which the Command Window is based), and not due to Matlab itself. In any case, it shows that not all control characters behave the same way on different environments, which is actually not very surprising.</p><p>Another widely-used control character, the bell control-char (<a
target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Bell_character">BEL</a>, or <i><b>sprintf</b></i>(&#8216;\g&#8217;), or <i><b>char</b></i>(7)) is not accepted at all by Matlab&#8217;s implementation of <i><b>sprintf</b></i> and its variants. Matlab users can always use the <i><b>beep</b></i> function of course, I&#8217;m just pointing this out as another inconsistency between Matlab&#8217;s <i><b>*printf</b></i>() implementation and that of other environments. And don&#8217;t even think of using raw device escape sequences (ah, the good-ol&#8217;-days, long gone now&#8230;).</p><p>Control characters that <i>are</i> accepted in Matlab and behave as expected include: FF (\f), NL (\n) and TAB (\t). These can be used in <i><b>fprintf</b></i> to modify the text spacing. The <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/ref/sprintf.html"><i><b>sprintf</b></i> documentation</a> mentions which of the control characters are accepted, so this is not, strictly speaking, an undocumented aspect. However, note that there is a discrepancy between the list of accepted control chars in the online/<i><b>doc</b></i> page compared to the <i><b>help</b></i> section (\a and \v are mentioned in the former but not in the latter).</p><p>For another type of Command Window text manipulation, namely colors, refer to my <a
target="_blank" href="http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/">cprintf utility</a>. For anyone who hasn&#8217;t noticed, I recently updated the utility on the Matlab File Exchange. Due to some internal modifications by MathWorks to the Command Window implementation in R2011b, <i><b>cprintf</b></i> no longer needs to pad color segments with spaces, and separate color segments can now be directly adjacent to each other (in R2011a and earlier, the spaces are unfortunately needed).</p><p>Have you used control characters in an innovative manner in your work? If so, please share your experience in a <a
target="_blank" href="http://UndocumentedMatlab.com/blog/command-window-text-manipulation/#respond">comment</a>.</p><p>Happy New Year everyone!</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/' rel='bookmark' title='cprintf &#8211; display formatted color text in the Command Window'>cprintf &#8211; display formatted color text in the Command Window</a> <small>cprintf is a utility that utilized undocumented Matlab desktop functionalities to display color and underline-styled formatted text in the Command Window...</small></li><li><a
href='http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors-part2/' rel='bookmark' title='Changing Matlab&#8217;s Command Window colors &#8211; part 2'>Changing Matlab&#8217;s Command Window colors &#8211; part 2</a> <small>The Matlab Command Window enables a limited degree of inline color customization - this post describes how to use it...</small></li><li><a
href='http://undocumentedmatlab.com/blog/editormacro-v2-setting-command-window-key-bindings/' rel='bookmark' title='EditorMacro v2 &#8211; setting Command Window key-bindings'>EditorMacro v2 &#8211; setting Command Window key-bindings</a> <small>The EditorMacro utility was extended to support built-in Matlab Editor and Command-Window actions and key-bindings. This post describes the changes and the implementation details....</small></li><li><a
href='http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/' rel='bookmark' title='Changing Matlab&#8217;s Command Window colors'>Changing Matlab&#8217;s Command Window colors</a> <small>Matlab's Command Window foreground and background colors can be modified programmatically, using some of Matlab's undocumented internal Java classes. Here's how....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/command-window-text-manipulation/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>uisplittool &amp; uitogglesplittool</title><link>http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool/</link> <comments>http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool/#comments</comments> <pubDate>Thu, 09 Dec 2010 00:06:33 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Desktop]]></category> <category><![CDATA[Figure window]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[Toolbar]]></category> <category><![CDATA[uitools]]></category> <category><![CDATA[uiundo]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1994</guid> <description><![CDATA[Matlab's undocumented uisplittool and uitogglesplittool are powerful controls that can easily be added to Matlab toolbars - this article explains how<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool-callbacks/' rel='bookmark' title='uisplittool &amp; uitogglesplittool callbacks'>uisplittool &#038; uitogglesplittool callbacks</a> <small>Matlab's undocumented uisplittool and uitogglesplittool are powerful toolbar controls - this article explains how to customize their behavior...</small></li><li><a
href='http://undocumentedmatlab.com/blog/figure-toolbar-components/' rel='bookmark' title='Figure toolbar components'>Figure toolbar components</a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....</small></li><li><a
href='http://undocumentedmatlab.com/blog/uiundo-matlab-undocumented-undo-redo-manager/' rel='bookmark' title='uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager'>uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager</a> <small>The built-in uiundo function provides easy yet undocumented access to Matlab's powerful undo/redo functionality. This article explains its usage....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uiundo/' rel='bookmark' title='Customizing uiundo'>Customizing uiundo</a> <small>This article describes how Matlab's undocumented uiundo undo/redo manager can be customized...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Matlab 7.6 (R2008a) and onward contain a reference to <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> in the javacomponent.m and %matlabroot%/bin/registry/hg.xml files. These are reported as built-in functions by the <i><b>which</b></i> function, although they have no corresponding m-file as other similar built-in functions (note the double &#8216;t&#8217;, as in <i>split-tool</i>):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">which</span> uisplittool
built-in <span style="color: #080;">&#40;</span>C<span style="color: #F0F;">:</span>\Matlab\R2010b\toolbox\matlab\uitools\uisplittool<span style="color: #080;">&#41;</span></pre></div></div><p>These uitools are entirely undocumented, even today (R2010b). They puzzled me for a very long time. An acute reader (Jeremy Raymonds) suggested they are related to toolbars, like other uitools such as the <i><b>uipushtool</b></i> and <i><b>uitoggletool</b></i>. This turned out to be the missing clue that unveiled these useful tools:</p><h3 id="Intro">So what are <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i>?</h3><p>Both <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> are basic Handle-Graphics building blocks used in Matlab toolbars, similarly to the well-documented <i><b>uipushtool</b></i> and <i><b>uitoggletool</b></i>.</p><p><i><b>uisplittool</b></i> presents a simple drop-down, whereas <i><b>uitogglesplittool</b></i> presents a drop-down that is also selectable.</p><p>The Publish and Run controls on the Matlab Editor&#8217;s toolbar are examples of <i><b>uisplittool</b></i>, and so are the Brush / Select-Data control on the figure toolbar, and the plot-selection drop-down on the Matlab Desktop&#8217;s Workspace toolbar:<br
/><center><div
class="wp-caption aligncenter" style="width: 371px"><img
alt="uisplittool in action in the Matlab Desktop" src="http://UndocumentedMatlab.com/images/uisplittool.png" title="uisplittool in action in the Matlab Desktop" width="361" height="221" /><p
class="wp-caption-text"><i><b>uisplittool</b></i> in action in the Matlab Desktop</p></div></center></p><h3 id="Appearance">Adding <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> to a toolbar</h3><p>Adding a <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> to a toolbar is done in a similar manner to adding <i><b>uipushtool</b></i>s and <i><b>uitoggletool</b></i>s:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hToolbar = findall<span style="color: #080;">&#40;</span><span style="color: #0000FF;">gcf</span>,<span style="color:#A020F0;">'tag'</span>,<span style="color:#A020F0;">'FigureToolBar'</span><span style="color: #080;">&#41;</span>;
hUndo=uisplittool<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hToolbar<span style="color: #080;">&#41;</span>;       <span style="color: #228B22;">% uisplittool</span>
hRedo=uitogglesplittool<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hToolbar<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% uitogglesplittool</span></pre></div></div><p>Like <i><b>uipushtool</b></i> and <i><b>uitoggletool</b></i>, <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> also have unique <strong>Type</strong> property values, &#8216;uisplittool&#8217; and &#8216;uitogglesplittool&#8217; respectively. The handles can also be tested using the built-in <i><b>isa</b></i> function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">isa</span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>hUndo<span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'uisplittool'</span><span style="color: #080;">&#41;</span>   <span style="color: #228B22;">% or: 'uitogglesplittool'</span>
<span style="color: #0000FF;">ans</span> =
     <span style="color: #33f;">1</span>
&gt;&gt; <span style="color: #0000FF;">class</span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>hUndo<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
uisplittool</pre></div></div><p>Just as with <i><b>uipushtool</b></i>s and <i><b>uitoggletool</b></i>s, the new buttons have an empty button-face appearance, until we fix their <strong>CData</strong>, <strong>Tooltip</strong> and similar settable properties:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Load the Redo icon</span>
icon = <span style="color: #0000FF;">fullfile</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">matlabroot</span>,<span style="color:#A020F0;">'/toolbox/matlab/icons/greenarrowicon.gif'</span><span style="color: #080;">&#41;</span>;
<span style="color: #080;">&#91;</span>cdata,map<span style="color: #080;">&#93;</span> = <span style="color: #0000FF;">imread</span><span style="color: #080;">&#40;</span>icon<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Convert white pixels into a transparent background</span>
map<span style="color: #080;">&#40;</span><span style="color: #0000FF;">find</span><span style="color: #080;">&#40;</span>map<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>+map<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span>,<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>+map<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span>,<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>==<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> = <span style="color: #0000FF;">NaN</span>;
&nbsp;
<span style="color: #228B22;">% Convert into 3D RGB-space</span>
cdataRedo = ind2rgb<span style="color: #080;">&#40;</span>cdata,map<span style="color: #080;">&#41;</span>;
cdataUndo = cdataRedo<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">16</span><span style="color: #F0F;">:</span>-<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>,<span style="color: #F0F;">:</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Add the icon (and its mirror image = undo) to latest toolbar</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hUndo, <span style="color:#A020F0;">'cdata'</span>,cdataUndo, <span style="color:#A020F0;">'tooltip'</span>,<span style="color:#A020F0;">'undo'</span>,<span style="color:#A020F0;">'Separator'</span>,<span style="color:#A020F0;">'on'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'ClickedCallback'</span>,<span style="color:#A020F0;">'uiundo(gcbf,'</span><span style="color:#A020F0;">'execUndo'</span><span style="color:#A020F0;">')'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>hRedo, <span style="color:#A020F0;">'cdata'</span>,cdataRedo, <span style="color:#A020F0;">'tooltip'</span>,<span style="color:#A020F0;">'redo'</span>, <span style="color: #F0F;">...</span>
           <span style="color:#A020F0;">'ClickedCallback'</span>,<span style="color:#A020F0;">'uiundo(gcbf,'</span><span style="color:#A020F0;">'execRedo'</span><span style="color:#A020F0;">')'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="User-created uisplittool &amp; uitogglesplittool toolbar buttons" src="http://UndocumentedMatlab.com/images/uisplittool2b2.png" title="User-created uisplittool &amp; uitogglesplittool toolbar buttons" width="450" height="107" /><p
class="wp-caption-text">User-created <i><b>uisplittool</b></i> &amp; <i><b>uitogglesplittool</b></i> toolbar buttons</p></div></center></p><p>Note that the controls can be created with these properties in a single command:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hUndo = uisplittool<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'parent'</span>,hToolbar, <span style="color:#A020F0;">'cdata'</span>,cdataRedo, <span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span>;</pre></div></div><h3 id="Arranging">Re-arranging the toolbar controls placement</h3><p>Let us now re-arrange our toolbar buttons. Unfortunately, a bug causes <i><b>uisplittool</b></i>s and <i><b>uitogglesplittool</b></i>s to always be placed flush-left when the toolbar&#8217;s children are re-arranged (anyone at TMW reading this in time for the R2011a bug-parade selection?).</p><p>So, we can&#8217;t re-arrange the buttons at the HG-children level. Luckily, we can re-arrange directly at the Java level (note that until now, the entire discussion of <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> was purely Matlab-based):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jToolbar = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hToolbar,<span style="color:#A020F0;">'JavaContainer'</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'ComponentPeer'</span><span style="color: #080;">&#41;</span>;
jButtons = jToolbar.<span style="">getComponents</span>;
<span style="color: #0000FF;">for</span> buttonId = <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>jButtons<span style="color: #080;">&#41;</span>-<span style="color: #33f;">3</span> <span style="color: #F0F;">:</span> -<span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #33f;">7</span>  <span style="color: #228B22;">% end-to-front</span>
   jToolbar.<span style="">setComponentZOrder</span><span style="color: #080;">&#40;</span>jButtons<span style="color: #080;">&#40;</span>buttonId<span style="color: #080;">&#41;</span>, buttonId+<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span>
jToolbar.<span style="">setComponentZOrder</span><span style="color: #080;">&#40;</span>jButtons<span style="color: #080;">&#40;</span>end-<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>, <span style="color: #33f;">5</span><span style="color: #080;">&#41;</span>;   <span style="color: #228B22;">% Separator</span>
jToolbar.<span style="">setComponentZOrder</span><span style="color: #080;">&#40;</span>jButtons<span style="color: #080;">&#40;</span>end-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>, <span style="color: #33f;">6</span><span style="color: #080;">&#41;</span>;   <span style="color: #228B22;">% Undo</span>
jToolbar.<span style="">setComponentZOrder</span><span style="color: #080;">&#40;</span>jButtons<span style="color: #080;">&#40;</span><span style="color: #0000FF;">end</span><span style="color: #080;">&#41;</span>, <span style="color: #33f;">7</span><span style="color: #080;">&#41;</span>;     <span style="color: #228B22;">% Redo</span>
jToolbar.<span style="">revalidate</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="Re-arranged uisplittool &amp; uitogglesplittool toolbar buttons" src="http://UndocumentedMatlab.com/images/uisplittool2c2.png" title="Re-arranged uisplittool &amp; uitogglesplittool toolbar buttons" width="450" height="106" /><p
class="wp-caption-text">Re-arranged <i><b>uisplittool</b></i> &amp; <i><b>uitogglesplittool</b></i> toolbar buttons<br/>(not as simple as it may sound)</p></div></center></p><p>Next week, I will combine the information in this article, with <a
target="_blank" href="http://undocumentedmatlab.com/blog/tag/uiundo/">last year&#8217;s articles</a> about <i><b>uiundo</b></i>, and show how we can create a dynamic figure toolbar drop-down of undo/redo events. Here is a preview to whet your appetite:</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="undo/redo buttons implemented using uisplittool" src="http://UndocumentedMatlab.com/images/uisplittool4.png" title="undo/redo buttons implemented using uisplittool" width="450" height="226" /><p
class="wp-caption-text">undo/redo buttons implemented using <i><b>uisplittool</b></i></p></div></center></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool-callbacks/' rel='bookmark' title='uisplittool &amp; uitogglesplittool callbacks'>uisplittool &#038; uitogglesplittool callbacks</a> <small>Matlab's undocumented uisplittool and uitogglesplittool are powerful toolbar controls - this article explains how to customize their behavior...</small></li><li><a
href='http://undocumentedmatlab.com/blog/figure-toolbar-components/' rel='bookmark' title='Figure toolbar components'>Figure toolbar components</a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....</small></li><li><a
href='http://undocumentedmatlab.com/blog/uiundo-matlab-undocumented-undo-redo-manager/' rel='bookmark' title='uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager'>uiundo &#8211; Matlab&#8217;s undocumented undo/redo manager</a> <small>The built-in uiundo function provides easy yet undocumented access to Matlab's powerful undo/redo functionality. This article explains its usage....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uiundo/' rel='bookmark' title='Customizing uiundo'>Customizing uiundo</a> <small>This article describes how Matlab's undocumented uiundo undo/redo manager can be customized...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/uisplittool-uitogglesplittool/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Customizing Workspace context-menu</title><link>http://undocumentedmatlab.com/blog/customizing-workspace-context-menu/</link> <comments>http://undocumentedmatlab.com/blog/customizing-workspace-context-menu/#comments</comments> <pubDate>Wed, 03 Nov 2010 18:00:17 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Desktop]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Internal component]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1943</guid> <description><![CDATA[Matlab's Workspace table context-menu can be configured with user-defined actions - this article explains how.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/' rel='bookmark' title='Customizing Matlab&#8217;s Workspace table'>Customizing Matlab&#8217;s Workspace table</a> <small>The Matlab Desktop's Workspace pane table can be customized, as described here...</small></li><li><a
href='http://undocumentedmatlab.com/blog/adding-context-menu-to-uitree/' rel='bookmark' title='Adding a context-menu to a uitree'>Adding a context-menu to a uitree</a> <small>uitree is an undocumented Matlab function, which does not easily enable setting a context-menu. Here's how to do it....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-2/' rel='bookmark' title='Customizing menu items part 2'>Customizing menu items part 2</a> <small>Matlab menu items can be customized in a variety of useful ways using their underlying Java object. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-3/' rel='bookmark' title='Customizing menu items part 3'>Customizing menu items part 3</a> <small>Matlab menu items can easily display custom icons, using just a tiny bit of Java magic powder. ...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>My first article of 2010 described <a
target="_blank" href="http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/">customizing Matlab&#8217;s Workspace table</a>, in particular the values presented in the Bytes column.</p><p>Last week, a reader of that article <a
target="_blank" href="http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/#comment-21170">posted a comment</a> asking how to customize the context (right-click) menu with some user-defined actions. The user has found that modifying the menu via the regular table handles gets reset automatically.</p><p>Today&#8217;s article will describe an easy and effective way to add user-defined actions to the Workspace table, for specific object types.</p><p>The trick is not to modify the context-menu directly. As the reader above has noticed, this menu is automatically recreated on the fly, so no change will be persistent. Instead, we modify Matlab&#8217;s internal registry of class-specific context-menus. This is done in several prefspanel.m files in the Matlab codebase (For example: \toolbox\matlab\audiovideo\prefspanel.m and \toolbox\signal\signal\prefspanel.m).</p><p>The mechanism relies on the following Java method, which is unsupported and undocumented, yet has existed in the present form for the past several releases:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">classes = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'double'</span>, <span style="color:#A020F0;">'java.lang.Object'</span><span style="color: #080;">&#125;</span>;
menuName = <span style="color:#A020F0;">'My context-menu'</span>;
menuItems = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'Inspect'</span>, <span style="color:#A020F0;">'Properties'</span>, <span style="color:#A020F0;">'-'</span>, <span style="color:#A020F0;">'class name'</span><span style="color: #080;">&#125;</span>;
menuActions = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'inspect($1)'</span>, <span style="color:#A020F0;">'get($1)'</span>, <span style="color:#A020F0;">''</span>, <span style="color:#A020F0;">'class($1)'</span><span style="color: #080;">&#125;</span>;
com.<span style="">mathworks</span>.<span style="">mlwidgets</span>.<span style="color: #0000FF;">workspace</span>.<span style="">MatlabCustomClassRegistry</span>.<span style="">registerClassCallbacks</span><span style="color: #080;">&#40;</span>classes,menuName,menuItems,menuActions<span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 366px"><img
alt="Customizable Workspace table context-menu" src="http://UndocumentedMatlab.com/images/Workspace_ContextMenu.png" title="Customizable Workspace table context-menu" width="356" height="329" /><p
class="wp-caption-text">Customizable Workspace table context-menu</p></div></center></p><p>Once you have found your particular context-menu configuration useful, place the short customization code in your <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/loren/2009/03/03/whats-in-your-startupm/">startup.m file</a> so that the change becomes permanent in all future Matlab sessions.</p><p>A few other supporting static methods are available in the com.mathworks.mlwidgets.workspace.MatlabCustomClassRegistry class: <i>getClassCallbacksInformation (className)</i>, <i>registerSimilarClassCallbacks(newClassNames,definedClassName)</i> and <i>unregisterClassCallbacks(definedClassName)</i>. For example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">com.<span style="">mathworks</span>.<span style="">mlwidgets</span>.<span style="color: #0000FF;">workspace</span>.<span style="">MatlabCustomClassRegistry</span>.<span style="">getClassCallbacksInformation</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'double'</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">ans</span> =
java.<span style="">lang</span>.<span style="">Object</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #F0F;">:</span>
    <span style="color:#A020F0;">'My context-menu'</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">4</span> element array<span style="color: #080;">&#93;</span>        <span style="color: #228B22;">% = menuItems</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">4</span> element array<span style="color: #080;">&#93;</span>        <span style="color: #228B22;">% = menuActions</span></pre></div></div><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/' rel='bookmark' title='Customizing Matlab&#8217;s Workspace table'>Customizing Matlab&#8217;s Workspace table</a> <small>The Matlab Desktop's Workspace pane table can be customized, as described here...</small></li><li><a
href='http://undocumentedmatlab.com/blog/adding-context-menu-to-uitree/' rel='bookmark' title='Adding a context-menu to a uitree'>Adding a context-menu to a uitree</a> <small>uitree is an undocumented Matlab function, which does not easily enable setting a context-menu. Here's how to do it....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-2/' rel='bookmark' title='Customizing menu items part 2'>Customizing menu items part 2</a> <small>Matlab menu items can be customized in a variety of useful ways using their underlying Java object. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-menu-items-part-3/' rel='bookmark' title='Customizing menu items part 3'>Customizing menu items part 3</a> <small>Matlab menu items can easily display custom icons, using just a tiny bit of Java magic powder. ...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/customizing-workspace-context-menu/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Modifying Matlab&#8217;s Look-and-Feel</title><link>http://undocumentedmatlab.com/blog/modifying-matlab-look-and-feel/</link> <comments>http://undocumentedmatlab.com/blog/modifying-matlab-look-and-feel/#comments</comments> <pubDate>Wed, 07 Jul 2010 16:42:00 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Desktop]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[JIDE]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1721</guid> <description><![CDATA[Matlab's entire Look-and-Feel (PLAF, or L&#038;F) can be modified at the control or application level - this article shows how<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/modifying-default-toolbar-menubar-actions/' rel='bookmark' title='Modifying default toolbar/menubar actions'>Modifying default toolbar/menubar actions</a> <small>The default Matlab figure toolbar and menu actions can easily be modified using simple pure-Matlab code. This article explains how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-line-position-in-edit-box-uicontrol/' rel='bookmark' title='Setting line position in an edit-box uicontrol'>Setting line position in an edit-box uicontrol</a> <small>Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-system-tray-icons/' rel='bookmark' title='Setting system tray icons'>Setting system tray icons</a> <small>System-tray icons can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-status-bar-text/' rel='bookmark' title='Setting status-bar text'>Setting status-bar text</a> <small>The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A couple of days ago, a reader of this blog <a
target="_blank" href="http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/#comment-12043">posted a comment</a>, asking whether it is possible to change Matlab&#8217;s Desktop color scheme, and its general UI look. Instead of providing a short answer, I will use the opportunity to answer in a full article. So this is for you, Egon <img
src='http://undocumentedmatlab.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p><h3 id="PLAF">Matlab&#8217;s underlying Look-and-Feel</h3><p>One of Matlab&#8217;s great advantages is cross-platform compatibility. Generally speaking, Matlab applications written on Windows will work as-is on Macintosh and Linux.</p><p>Java has similar cross-platform compatibilities, but enables much greater control over the <em>look-and-feel</em> (L&#038;F or <em><a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/plaf.html">PLAF</a></em>) of application GUI. In a nutshell, L&#038;Fs affect the appearance and behavior of menus, controls, color schemes etc., using a <a
target="_blank" rel="nofollow" href="http://java.sun.com/products/jfc/tsc/articles/architecture/#pluggable">properties plug-in architecture</a>. Java programmers can choose to use either a platform-independent L&#038;F (called the <em>Metal L&#038;F</em>), or a platform-specific L&#038;F. The benefit of using Metal is that the application looks essentially the same on all Java-supported platforms; the drawback is that they do not look like native applications on any platform&#8230;</p><p><center><div
class="wp-caption aligncenter" style="width: 380px"><img
alt="Metal L&#038;F" src="http://UndocumentedMatlab.com/images/plaf_metal2.png" title="Metal L&#038;F" width="122" height="42" /> &nbsp; <img
alt="Motif L&#038;F" src="http://UndocumentedMatlab.com/images/plaf_motif2.png" title="Motif L&#038;F" width="100" height="48" /> &nbsp; <img
alt="Windows L&#038;F" src="http://UndocumentedMatlab.com/images/plaf_windows2.png" title="Windows L&#038;F" width="101" height="44" /><p
class="wp-caption-text">Metal, Motif &#038; Windows L&#038;F</p></div></center></p><p>Matlab, whose GUI is based on Java (not surprising to readers of this website), has chosen to use a platform-specific L&#038;F on each of the platforms on which it is supported. So, Matlab on Windows looks like a native Windows application, whereas on Macs it looks similar to Mac apps (notwithstanding the well-known <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/desktop/2009/08/31/pouncing-on-snow-leopard/">X11 migration issues</a>). Of course, this means that Windows Matlab looks and behaves differently from Mac/Linux Matlabs. Note that the differences only affect the Desktop, tools/utilities (Editor etc.) and the general L&#038;F &#8211; it does not affect the displayed plots. In practice, the differences are visible but easily understandable.</p><h3 id="Change_PLAF">Changing the L&#038;F</h3><p>We can get the list of available L&#038;Fs on our system as follows (below is the list on my Windows system):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; lafs = javax.<span style="">swing</span>.<span style="">UIManager</span>.<span style="">getInstalledLookAndFeels</span>
lafs =
javax.<span style="">swing</span>.<span style="">UIManager</span>$LookAndFeelInfo<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #F0F;">:</span>
    <span style="color: #080;">&#91;</span>javax.<span style="">swing</span>.<span style="">UIManager</span>$LookAndFeelInfo<span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>javax.<span style="">swing</span>.<span style="">UIManager</span>$LookAndFeelInfo<span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>javax.<span style="">swing</span>.<span style="">UIManager</span>$LookAndFeelInfo<span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>javax.<span style="">swing</span>.<span style="">UIManager</span>$LookAndFeelInfo<span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>javax.<span style="">swing</span>.<span style="">UIManager</span>$LookAndFeelInfo<span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">for</span> lafIdx = <span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>lafs<span style="color: #080;">&#41;</span>,  <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span>lafs<span style="color: #080;">&#40;</span>lafIdx<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;  <span style="color: #0000FF;">end</span>
javax.<span style="">swing</span>.<span style="">UIManager</span>$LookAndFeelInfo<span style="color: #080;">&#91;</span>Metal javax.<span style="">swing</span>.<span style="">plaf</span>.<span style="">metal</span>.<span style="">MetalLookAndFeel</span><span style="color: #080;">&#93;</span>
javax.<span style="">swing</span>.<span style="">UIManager</span>$LookAndFeelInfo<span style="color: #080;">&#91;</span>Nimbus com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">plaf</span>.<span style="">nimbus</span>.<span style="">NimbusLookAndFeel</span><span style="color: #080;">&#93;</span>
javax.<span style="">swing</span>.<span style="">UIManager</span>$LookAndFeelInfo<span style="color: #080;">&#91;</span>CDE/Motif com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">plaf</span>.<span style="">motif</span>.<span style="">MotifLookAndFeel</span><span style="color: #080;">&#93;</span>
javax.<span style="">swing</span>.<span style="">UIManager</span>$LookAndFeelInfo<span style="color: #080;">&#91;</span>Windows com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">plaf</span>.<span style="">windows</span>.<span style="">WindowsLookAndFeel</span><span style="color: #080;">&#93;</span>
javax.<span style="">swing</span>.<span style="">UIManager</span>$LookAndFeelInfo<span style="color: #080;">&#91;</span>Windows Classic com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">plaf</span>.<span style="">windows</span>.<span style="">WindowsClassicLookAndFeel</span><span style="color: #080;">&#93;</span></pre></div></div><p>We can change the L&#038;F to any of the installed L&#038;Fs, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">javax.<span style="">swing</span>.<span style="">UIManager</span>.<span style="">setLookAndFeel</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'javax.swing.plaf.metal.MetalLookAndFeel'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Although not listed in the installed L&#038;Fs, Matlab also enables access to a 3rd-party L&#038;F called <a
target="_blank" rel="nofollow" href="http://www.jgoodies.com/freeware/looks/index.html">Plastic3D</a> by <a
target="_blank" rel="nofollow" href="http://www.jgoodies.com">www.jgoodies.com</a>. Plastic3D L&#038;F generates slick stylish GUI:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">javax.<span style="">swing</span>.<span style="">UIManager</span>.<span style="">setLookAndFeel</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'com.jgoodies.looks.plastic.Plastic3DLookAndFeel'</span><span style="color: #080;">&#41;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 116px"><img
alt="Plastic3D L&#038;F" src="http://UndocumentedMatlab.com/images/plaf_plastic2.png" title="Plastic3D L&#038;F" width="106" height="44" /><p
class="wp-caption-text">Plastic3D L&#038;F</p></div></center></p><p>The <a
target="_blank" href="http://UndocumentedMatlab.com/blog/tag/jide/">JIDE class library</a> by <a
target="_blank" rel="nofollow" href="http://www.jidesoft.com">www.jidesoft.com</a>, which is bundled with Matlab, and specifically its jide-common.jar file, contains a separate set of 3rd-party L&#038;Fs: Aqua, Eclipse (Metal &#038; Windows variants), Office2003, VSNet (Metal &#038; Windows variants) and Xerto. Unfortunately, in Matlab releases starting around 2009, JIDE stopped including full L&#038;F classes in jide-common.jar, and started using L&#038;F extensions using their <a
target="_blank" rel="nofollow" href="http://www.jidesoft.com/javadoc/com/jidesoft/plaf/LookAndFeelFactory.html">com.jidesoft.plaf.LookAndFeelFactory</a> class. I have not been able to use this class properly, but readers are welcome to try (please tell me if you succeed).</p><p>External L&#038;Fs can also be downloaded and then used in Matlab. For example: <a
target="_blank" rel="nofollow" href="http://lookandfeel.incors.com/">Alloy</a>, <a
target="_blank" rel="nofollow" href="http://www.javasoft.de/synthetica/">Synthetica</a> and many others.</p><p>The current and standard L&#038;Fs can be retrieved by using the respective static methods javax.swing.UIManager.<em>getLookAndFeel()</em> and <em>getSystemLookAndFeelClassName()</em>.</p><p>Matlab also has a utility class com.mathworks.mwswing.plaf.PlafUtils that contains static methods that query the current L&#038;F: <em>isPlasticLookAndFeel(), isAquaLookAndFeel(),  isMetalLookAndFeel(), isMotifLookAndFeel()</em> and <em>isWindowsLookAndFeel()</em>. For some reason there is no method for the new (R2010a+) <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/nimbus.html">Nimbus L&#038;F</a>.</p><p>Nimbus L&#038;F offers great potential for a cross-platform vectorized appearance, the ability to customize/skin pretty much every aspect of the visual appearance and component behavior, replacing Swing&#8217;s <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/synth.html">Synth L&#038;F</a> which was used for such customizations in earlier Matlab/Java releases. Nimbus is pre-installed as a non-default L&#038;F in Matlab R2010a (7.10) onward, because it seems that most designers who target a single platform still <a
target="_blank" rel="nofollow" href="http://www.jasperpotts.com/blog/2009/03/breakdown-of-what-should-be-default-laf-for-java-7/">prefer the native L&#038;F</a>.</p><h3 id="Component_PLAF">Component-specific L&#038;F</h3><p>You can also modify the L&#038;F of specific components, not just the entire Matlab. To do this, simply modify the L&#038;F immediately before creating your GUI component, and restore the original L&#038;F afterward (note how you can use either the L&#038;F class name or its class reference):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Update the current L&amp;F</span>
originalLnF = javax.<span style="">swing</span>.<span style="">UIManager</span>.<span style="">getLookAndFeel</span>;  <span style="color: #228B22;">%class</span>
newLnF = <span style="color:#A020F0;">'javax.swing.plaf.metal.MetalLookAndFeel'</span>;  <span style="color: #228B22;">%string</span>
javax.<span style="">swing</span>.<span style="">UIManager</span>.<span style="">setLookAndFeel</span><span style="color: #080;">&#40;</span>newLnF<span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Create GUI using the modified L&amp;F</span>
hFig = <span style="color: #0000FF;">figure</span><span style="color: #080;">&#40;</span><span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span>;
hComponent = <span style="color: #0000FF;">uicontrol</span><span style="color: #080;">&#40;</span><span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span>;
jComponent = javacomponent<span style="color: #080;">&#40;</span><span style="color: #F0F;">...</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Ensure that the controls are fully-rendered before restoring the L&amp;F</span>
<span style="color: #0000FF;">drawnow</span>; <span style="color: #0000FF;">pause</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0.05</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Restore the original L&amp;F</span>
javax.<span style="">swing</span>.<span style="">UIManager</span>.<span style="">setLookAndFeel</span><span style="color: #080;">&#40;</span>originalLnF<span style="color: #080;">&#41;</span>;</pre></div></div><p>Components can update their L&#038;F to the current L&#038;F using their jComponent.<em>updateUI()</em> method. Components that are not specifically updated by invoking their <em>updateUI()</em> method will retain their existing (original) L&#038;F – the L&#038;F which was active when the components were created or last updated.</p><h3 id="Properties">Fine-grained L&#038;F property control</h3><p>The default settings for the L&#038;F can be retrieved using the static method javax.swing.UIManager.<em>getDefaults()</em>, which returns an enumeration of the thousand or so default settings:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; defaults = javax.<span style="">swing</span>.<span style="">UIManager</span>.<span style="">getDefaults</span>;
&gt;&gt; propValues = defaults.<span style="">elements</span>; propKeys = defaults.<span style="">keys</span>;
&gt;&gt; <span style="color: #0000FF;">while</span> propKeys.<span style="">hasMoreElements</span>
     key = propKeys.<span style="">nextElement</span>;
     value = propValues.<span style="">nextElement</span>;
     <span style="color: #0000FF;">disp</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>key<span style="color: #080;">&#41;</span> <span style="color:#A020F0;">' = '</span> <span style="color: #0000FF;">evalc</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'disp(value)'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">end</span>
&nbsp;
com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">plaf</span>.<span style="">windows</span>.<span style="">WindowsLabelUI</span> = <span style="color: #0000FF;">class</span> com.<span style="">sun</span>.<span style="">java</span>.<span style="">swing</span>.<span style="">plaf</span>.<span style="">windows</span>.<span style="">WindowsLabelUI</span>
<span style="color: #F0F;">...</span> <span style="color: #080;">&#40;</span>~<span style="color: #33f;">1000</span> other property settings<span style="color: #080;">&#41;</span>
SplitPane.<span style="">dividerSize</span> =      <span style="color: #33f;">5</span>
DockableFrameTitlePane.<span style="">stopAutohideIcon</span> = javax.<span style="">swing</span>.<span style="">ImageIcon</span>@1f4e4c0
FormattedTextField.<span style="">caretBlinkRate</span> =    <span style="color: #33f;">500</span>
Table.<span style="">gridColor</span> = javax.<span style="">swing</span>.<span style="">plaf</span>.<span style="">ColorUIResource</span><span style="color: #080;">&#91;</span>r=<span style="color: #33f;">128</span>,g=<span style="color: #33f;">128</span>,b=<span style="color: #33f;">128</span><span style="color: #080;">&#93;</span></pre></div></div><p>Specific settings can be modified by using javax.swing.UIManager.<em>put(key,newValue)</em>.</p><p>Have you found any useful L&#038;F or property that you are using in your application? If so, please share them in the <a
href="#respond">comments</a> section below.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/modifying-default-toolbar-menubar-actions/' rel='bookmark' title='Modifying default toolbar/menubar actions'>Modifying default toolbar/menubar actions</a> <small>The default Matlab figure toolbar and menu actions can easily be modified using simple pure-Matlab code. This article explains how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-line-position-in-edit-box-uicontrol/' rel='bookmark' title='Setting line position in an edit-box uicontrol'>Setting line position in an edit-box uicontrol</a> <small>Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-system-tray-icons/' rel='bookmark' title='Setting system tray icons'>Setting system tray icons</a> <small>System-tray icons can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-status-bar-text/' rel='bookmark' title='Setting status-bar text'>Setting status-bar text</a> <small>The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/modifying-matlab-look-and-feel/feed/</wfw:commentRss> <slash:comments>42</slash:comments> </item> <item><title>Setting desktop tab completions</title><link>http://undocumentedmatlab.com/blog/setting-desktop-tab-completions/</link> <comments>http://undocumentedmatlab.com/blog/setting-desktop-tab-completions/#comments</comments> <pubDate>Wed, 03 Mar 2010 16:03:09 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Desktop]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1175</guid> <description><![CDATA[The Matlab desktop's Command-Window tab-completion can be customized for user-defined functions<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/' rel='bookmark' title='setPrompt &#8211; Setting the Matlab Desktop prompt'>setPrompt &#8211; Setting the Matlab Desktop prompt</a> <small>The Matlab Desktop's Command-Window prompt can easily be modified using some undocumented features...</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-the-matlab-desktop-layout-programmatically/' rel='bookmark' title='Setting the Matlab desktop layout programmatically'>Setting the Matlab desktop layout programmatically</a> <small>The Matlab desktop enables saving and switching layouts using the main menu. This post shows how to do so programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-status-bar-text/' rel='bookmark' title='Setting status-bar text'>Setting status-bar text</a> <small>The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-system-tray-icons/' rel='bookmark' title='Setting system tray icons'>Setting system tray icons</a> <small>System-tray icons can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>This site has lately focused on quite detailed Java-related topics. Next week I will present the promised EDT article, which will dive into even deeper Java territory. So I thought to take a short break and present an entirely pure-Matlab non-Java undocumented feature, which is simple and yet quite useful.</p><p>A few months ago, a CSSM reader <a
target=_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/264550">asked</a> whether it is possible to customize <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/desktop/2007/04/27/tab-completion-will-save-your-fingers/">Matlab tab-completion</a> for user-defined functions (see <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/desktop/2009/08/10/tab-to-narrow-completions/">related</a>). A similar question on StackOverflow provided the necessary <a
target=_blank" rel="nofollow" href="http://stackoverflow.com/questions/1842804/tab-completion-of-filenames-as-arguments-for-matlab-scripts">solution lead</a>:</p><p>Apparently, Matlab has a file called TC.xml in its [matlabroot '/toolbox/local/'] folder that contains the definitions of the tab-completable functions and their arguments. In order for a user-defined function&#8217;s arguments to support tab-completion, a new entry needs to be added to this XML file.</p><h3 id="TC.xml">TC.xml &amp; TC.xsd</h3><p>The full syntax of the TC.xml file can be found in the TC.xsd file, which is located in the same folder as TC.xml. Here are some sample definitions from my TC.xml file (which might vary across Matlab releases):</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;addpath&quot;</span> <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;DIR&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;help&quot;</span>    <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;FUN SUBFUN&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;clear&quot;</span>   <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;FUN VAR&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;whos&quot;</span>    <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;VAR&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arg</span> <span style="color: #000066;">previous</span>=<span style="color: #ff0000;">&quot;-file&quot;</span> <span style="color: #000066;">ctype</span>= <span style="color: #ff0000;">&quot;MATFILE&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;open&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arg</span> <span style="color: #000066;">argn</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;VAR MATFILE FIGFILE MFILE MDLFILE FILE&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;openfig&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arg</span> <span style="color: #000066;">argn</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;FIGFILE&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arg</span> <span style="color: #000066;">argn</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;VAR&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;new visible invisible reuse&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;mlint&quot;</span>   <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;FUN&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arg</span> <span style="color: #000066;">argn</span>=<span style="color: #ff0000;">&quot;2:10&quot;</span> <span style="color: #000066;">ctype</span>=<span style="color: #ff0000;">&quot;VAR&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;-struct -string -id&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p>The first example defines that an unlimited number of <i><b>addpath</b></i> arguments are all of type DIR. Therefore, when completing any argument of this function in the Command-Window, Matlab will present only relevant DIR (=folder) elements in the pop-up window (lexically sorted):<br
/><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="Tab-completion of type DIR" src="http://UndocumentedMatlab.com/images/tabcomplete1.png" title="Tab-completion of type DIR" width="450" height="156" /><p
class="wp-caption-text">Tab-completion of type DIR</p></div></center></p><p>Similarly, <b><i>help</i></b> defines all its arguments to be a function or sub-function type, so the popup-up will only be populated with the function names currently visible in the desktop:<br
/><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="Tab-completion of types FUN &#038; SUBFUN" src="http://UndocumentedMatlab.com/images/tabcomplete2.png" title="Tab-completion of types FUN &#038; SUBFUN" width="450" height="174" /><p
class="wp-caption-text">Tab-completion of types FUN &#038; SUBFUN</p></div></center></p><p>Similarly, <b><i>clear</i></b> defines all its arguments as function names or variables. Note that the list of available functions and variables may change depending on the current execution stack position. The full list of supported types is defined in the TC.xsd file. It is: VAR, FUN, SUBFUN, DIR, FILE, MFILE, MATFILE, FIGFILE and MDLFILE. <b><u>Addendum:</u></b> Matlab release 7.10 (R2010a) added the MCOSPCG and MCOSCLASS types; release 7.13 (R2011b) added the MESSAGEID type.</p><p>The <b><i>whos</i></b> function defines all its arguments as VAR, except the single MATFILE argument that follows a &#8216;-file&#8217; argument (look at <b><i>whos</i></b>&#8216;s <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/whos.html">help page</a> to understand why).</p><p>The <b><i>open</i></b> function defines tab completion only for its first argument (with plenty of possible types&#8230;). Likewise, <b><i>openfig</i></b> defines its first argument as a FIGFILE, and its second as VAR with a few extra <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/openfig.html">special-purpose strings</a> that are added to the popup-up menu.</p><p>Finally, the <b><i>mlint</i></b> example shows that multiple arguments can be defined using a single XML definition element. In this case, args #2-10 are defined as VAR (with three extra special-purpose strings), while args #1 and #11+ are defined as FUN.</p><p>The careful user can edit the TC.xml file using any text editor (I strongly suggest saving a backup first):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">edit</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">fullfile</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">matlabroot</span>,<span style="color:#A020F0;">'toolbox/local/TC.xml'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></div></div><p>User-defined functions can easily be added to TC.xml, and we can even add/modify the built-in Matlab functions that are already defined. Note that changes to TC.xml only take effect after a Matlab restart. From then on, all future Matlab sessions will use the modification, so a really simple one-time edit can improve our workflow for a long time &#8211; at least until we upgrade Matlab, when we&#8217;ll need to redo our edits&#8230;</p><h3 id="TabComplete">TabComplete utility</h3><p>In order to facilitate TC.xml editing, I have created a utility called TabComplete, which is now <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/26830-tabcomplete">available on the Matlab File Exchange</a>. The use of this utility is very simple. For example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">tabcomplete test file <span style="color:#A020F0;">'DIR +data -data no_data'</span> <span style="color: #0000FF;">VAR</span></pre></div></div><p>defines a user-defined function <b><i>test</i></b> that accepts a FILE argument, followed by a DIR argument with three special-purpose strings, followed by any number of VAR arguments. If I wished to define specific argument types without any default type, I would use:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">tabcomplete test file <span style="color:#A020F0;">'DIR +data -data no_data'</span> <span style="color:#A020F0;">''</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 418px"><img
alt="Using TabComplete for user-defined functions" src="http://UndocumentedMatlab.com/images/tabcomplete.png" title="Using TabComplete for user-defined functions" width="408" height="147" /><p
class="wp-caption-text">Using TabComplete for user-defined functions</p></div></center></p><p
style="padding-left: 30px;"><b><u>Addendum:</u></b> Matlab releases 7.10 (R2010a) through 7.13 (R2011b) have a bug that causes Matlab to enter an endless loop (full CPU load) whenever tab-completion is requested for an argument that has possible values that are not simple terms. In the case above, &#8220;+data&#8221; and &#8220;-data&#8221; both cause this abnormal behavior. In such cases, the only remedy is to kill the Matlab process via the OS&#8217;s task manager. In Matlab R2010a-R2011b, I therefore suggest to use only simple values. This bug is apparently fixed in R2012a, making non-simple terms safe to use again. I take at least partial credit for this, having reported this bug to Mathworks in August 2011 (1-FCGDEI).</p><p></p><p>TabComplete can also be used to retrieve the current list of tab-completion definitions:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; definitions = tabcomplete;
&gt;&gt; definitions<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
    functionName<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'addpath'</span>
     defaultType<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'DIR'</span>
     extraValues<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
        platform<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
    functionArgs<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; definitions<span style="color: #080;">&#40;</span><span style="color: #33f;">54</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
    functionName<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'openfig'</span>
     defaultType<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
     extraValues<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
        platform<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
    functionArgs<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x2 <span style="color: #0000FF;">struct</span><span style="color: #080;">&#93;</span>
&gt;&gt; definitions<span style="color: #080;">&#40;</span><span style="color: #33f;">54</span><span style="color: #080;">&#41;</span>.<span style="">functionArgs</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
    previousArg<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
        argType<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'FIGFILE'</span>
    extraValues<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
&gt;&gt; definitions<span style="color: #080;">&#40;</span><span style="color: #33f;">54</span><span style="color: #080;">&#41;</span>.<span style="">functionArgs</span><span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = 
    previousArg<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
        argType<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'VAR'</span>
    extraValues<span style="color: #F0F;">:</span> <span style="color:#A020F0;">'new visible invisible reuse'</span></pre></div></div><p>TabComplete has a few limitations: it does not support the -previous option described above (you can do this by manually editing TC.xml). There are also some inherent limitations in Matlab&#8217;s TC functionality: changes take effect only after a Matlab restart (there might be a way to reload the definitions in the current Matlab session, but I do not know of any); the list of standard types cannot be modified; and the default type does not support extra special-purpose strings as do the numbered arguments.</p><p>There is another very annoying limitation: by default, TC.xml only supports lowercase function names. This is stupid, since Matlab has many function names with UPPERCASE characters, and certainly user-defined function names also do. Luckily, this last limitation can easily be overcome by editing the TC.xsd file (note that this is the TC.XSD file, not the TC.XML file). Instead of:</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:simpletype</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;tcBindingNameType&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:restriction</span> <span style="color: #000066;">base</span>=<span style="color: #ff0000;">&quot;xsd:token&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:pattern</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">'[A-Za-z_0-9]+(/[a-z_0-9]+)?'</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsd:restriction<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsd:simpletype<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p>Change the <b>xsd:pattern</b> definition element to:</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">&lt;!-- Yair 21/2/2010: added A-Z --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:pattern</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">'[A-Za-z_0-9]+(/[A-Za-z_0-9]+)?'</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div><p>(note the way comments can be added to the XSD/XML files)</p><p
style="padding-left: 30px;"><b><u>Addendum:</u></b> Matlab release 7.11 (R2011a) has fixed this XSD definition; perhaps due to this article <img
src='http://undocumentedmatlab.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p><p></p><p>P.S. an entirely different customization, for user-defined class members, was <a
target="_blank' rel="nofollow" href="http://www.tim.cz/en/nfaq/matlab-simulink/tab-completion.php">presented by Michal Kutil</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/' rel='bookmark' title='setPrompt &#8211; Setting the Matlab Desktop prompt'>setPrompt &#8211; Setting the Matlab Desktop prompt</a> <small>The Matlab Desktop's Command-Window prompt can easily be modified using some undocumented features...</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-the-matlab-desktop-layout-programmatically/' rel='bookmark' title='Setting the Matlab desktop layout programmatically'>Setting the Matlab desktop layout programmatically</a> <small>The Matlab desktop enables saving and switching layouts using the main menu. This post shows how to do so programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-status-bar-text/' rel='bookmark' title='Setting status-bar text'>Setting status-bar text</a> <small>The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-system-tray-icons/' rel='bookmark' title='Setting system tray icons'>Setting system tray icons</a> <small>System-tray icons can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/setting-desktop-tab-completions/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> <item><title>setPrompt &#8211; Setting the Matlab Desktop prompt</title><link>http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/</link> <comments>http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/#comments</comments> <pubDate>Mon, 25 Jan 2010 15:30:54 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Desktop]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Listeners]]></category> <category><![CDATA[Semi-documented function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[Listener]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=965</guid> <description><![CDATA[The Matlab Desktop's Command-Window prompt can easily be modified using some undocumented features<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/setting-the-matlab-desktop-layout-programmatically/' rel='bookmark' title='Setting the Matlab desktop layout programmatically'>Setting the Matlab desktop layout programmatically</a> <small>The Matlab desktop enables saving and switching layouts using the main menu. This post shows how to do so programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-desktop-tab-completions/' rel='bookmark' title='Setting desktop tab completions'>Setting desktop tab completions</a> <small>The Matlab desktop's Command-Window tab-completion can be customized for user-defined functions...</small></li><li><a
href='http://undocumentedmatlab.com/blog/editormacro-v2-setting-command-window-key-bindings/' rel='bookmark' title='EditorMacro v2 &#8211; setting Command Window key-bindings'>EditorMacro v2 &#8211; setting Command Window key-bindings</a> <small>The EditorMacro utility was extended to support built-in Matlab Editor and Command-Window actions and key-bindings. This post describes the changes and the implementation details....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-status-bar-text/' rel='bookmark' title='Setting status-bar text'>Setting status-bar text</a> <small>The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A few days ago, a reader emailed me with a challenge to modify the standard matlab Command-Window prompt from &#8220;&gt;&gt; &#8221; to some other string, preferably a dynamic prompt with the current timestamp. At first thought this cannot be done: The Command-Window prompts are hard-coded and to the best of my knowledge cannot be modified via <a
target="_blank" href="http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/">properties</a> or <a
target="_blank" href="http://undocumentedmatlab.com/blog/changing-system-preferences-programmatically/">system preferences</a>.</p><p>So the prompt can (probably) not be modified in advance, but what if it could be modified after being displayed? It is true that my <a
target="_blank" href="http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/"><b<i>cprintf</i></b> utility</a> modifies the Command-Window contents in order to display formatted text in a variety of font colors. But this case is different since <b<i>cprintf</i></b> runs once synchronously (user-invoked), whereas the prompt appears asynchronously multiple times.</p><p>There are two methods of handling multiple asynchronous events in Matlab: setting a callback on the object, and setting a PostSet <b><i>handle.listener</i></b> (or <b><i>schema.listener</i></b>) on the relevant object property. The first of these methods is a well-known Matlab practice, although we shall see that it uses an undocumented callback and functionality; the PostSet method is entirely undocumented and not well-known and shall be described in some later article. I decided to use the callback method to set the prompt &#8211; interested readers can try the PostSet method.</p><h3>Setting the Command Window&#8217;s callback</h3><p>The solution involved finding the Command-Window reference handle, and setting one of its <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">many callbacks</a>, in our case <b>CaretUpdateCallback</b>. This callback is fired whenever the desktop text is modified, which is an event we trap to replace the displayed prompt:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Get the reference handle to the Command Window text area</span>
jDesktop = com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLDesktop</span>.<span style="">getInstance</span>;
<span style="color: #0000FF;">try</span>
  cmdWin = jDesktop.<span style="">getClient</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Command Window'</span><span style="color: #080;">&#41;</span>;
  jTextArea = cmdWin.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>.<span style="">getViewport</span>.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">catch</span>
  commandwindow;
  jTextArea = jDesktop.<span style="">getMainFrame</span>.<span style="">getFocusOwner</span>;
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Instrument the text area's callback</span>
<span style="color: #0000FF;">if</span> <span style="color: #0000FF;">nargin</span> <span style="color: #F0F;">&amp;&amp;</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span> <span style="color: #F0F;">&amp;&amp;</span> ~<span style="color: #0000FF;">strcmp</span><span style="color: #080;">&#40;</span>newPrompt,<span style="color:#A020F0;">'&gt;&gt; '</span><span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'CaretUpdateCallback'</span>,<span style="color: #080;">&#123;</span>@setPromptFcn,newPrompt<span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">else</span>
  <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'CaretUpdateCallback'</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>Now that we have the Command-Window object callback set, we need to set the logic of prompt replacement &#8211; this is done in the internal Matlab function <i>setPromptFcn</i>. Here is its core code:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Does the displayed text end with the default prompt?</span>
<span style="color: #228B22;">% Note: catch a possible trailing newline</span>
<span style="color: #0000FF;">try</span> jTextArea = jTextArea.<span style="">java</span>;  <span style="color: #0000FF;">catch</span>,  <span style="color: #0000FF;">end</span>  <span style="color: #228B22;">%#ok</span>
cwText = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'Text'</span><span style="color: #080;">&#41;</span>;
pos = strfind<span style="color: #080;">&#40;</span>cwText<span style="color: #080;">&#40;</span><span style="color: #0000FF;">max</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,end-<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span><span style="color: #F0F;">:</span><span style="color: #0000FF;">end</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'&gt;&gt; '</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>pos<span style="color: #080;">&#41;</span>
  <span style="color: #228B22;">% Short prompts need to be space-padded</span>
  newLen = jTextArea.<span style="">getCaretPosition</span>;
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span>&lt;<span style="color: #33f;">3</span>
    newPrompt<span style="color: #080;">&#40;</span><span style="color: #0000FF;">end</span>+<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span> = <span style="color:#A020F0;">' '</span>;
  <span style="color: #0000FF;">elseif</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span>&gt;<span style="color: #33f;">3</span>
    fprintfStr = newPrompt<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #F0F;">:</span>end-<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">fprintf</span><span style="color: #080;">&#40;</span>fprintfStr<span style="color: #080;">&#41;</span>;
    newLen = newLen + <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>fprintfStr<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% The Command-Window text should be modified on the EDT</span>
  <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'replaceRange(Ljava.lang.String;II)'</span>,<span style="color: #F0F;">...</span>
            <span style="">newPrompt</span><span style="color: #080;">&#40;</span>end-<span style="color: #33f;">2</span><span style="color: #F0F;">:</span><span style="color: #0000FF;">end</span><span style="color: #080;">&#41;</span>, newLen-<span style="color: #33f;">3</span>, newLen<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'repaint()'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>In this code snippet, note that we space-pad prompt string that are shorter than 3 characters: this is done to prevent an internal-Matlab mixup when displaying additional text &#8211; Matlab &#8220;knows&#8221; the Command-Window&#8217;s text position and it gets mixed up if it turns out to be shorter than expected.</p><p>Also note that I use the semi-documented <b><i>awtinvoke</i></b> function to replace the default prompt (and an automatically-appended space) on the Event-Dispatch Thread (more on this in a future article). Since Matlab R2008a, I could use the more convenient <b><i>javaMethodEDT</i></b> function, but I wanted my code to work on all prior Matlab 7 versions, where <b><i>javaMethodEDT</i></b> was not yet available.</p><h3>Preventing callback re-entry</h3><p>The callback snippet above would enter an endless loop if not changed: whenever the prompt is modified the callback would have been re-fired, the prompt re-modified and so on endlessly. There are many methods of preventing callback re-entry &#8211; here&#8217;s the one I chose:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> setPromptFcn<span style="color: #080;">&#40;</span>jTextArea,eventData,newPrompt<span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #228B22;">% Prevent overlapping reentry due to prompt replacement</span>
  <span style="color: #0000FF;">persistent</span> inProgress
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>inProgress<span style="color: #080;">&#41;</span>
    inProgress = <span style="color: #33f;">1</span>;  <span style="color: #228B22;">%#ok unused</span>
  <span style="color: #0000FF;">else</span>
    <span style="color: #0000FF;">return</span>;
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #0000FF;">try</span>
    <span style="color: #228B22;">% *** Prompt modification code goes here ***</span>
&nbsp;
    <span style="color: #228B22;">% force prompt-change callback to fizzle-out...</span>
    <span style="color: #0000FF;">pause</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0.02</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Never mind - ignore errors...</span>
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% Enable new callbacks now that the prompt has been modified</span>
  inProgress = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>;
&nbsp;
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% setPromptFcn</span></pre></div></div><h3>Handling multiple prompt types</h3><p>I now wanted my function to handle both static prompt strings (like: &#8216;[Yair] &#8216;) and dynamic prompts (like: &#8216;[25-Jan-2010 01:00:51] &#8216;). This is done by accepting string-evaluable strings/functions:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Try to evaluate the new prompt as a function</span>
<span style="color: #0000FF;">try</span>
  origNewPrompt = newPrompt;
  newPrompt = <span style="color: #0000FF;">feval</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">catch</span>
  <span style="color: #0000FF;">try</span>
    newPrompt = <span style="color: #0000FF;">eval</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Never mind - probably a string...</span>
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">ischar</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span> <span style="color: #F0F;">&amp;&amp;</span> <span style="color: #0000FF;">ischar</span><span style="color: #080;">&#40;</span>origNewPrompt<span style="color: #080;">&#41;</span>
  newPrompt = origNewPrompt;
<span style="color: #0000FF;">end</span></pre></div></div><h3>File Exchange submission</h3><p>I then added some edge-case error handling and wrapped everything in a single utility called <b><i>setPrompt</i></b> that is now <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/26471">available on the File Exchange</a>.</p><p>In the future, if I find time, energy and interest, maybe I&#8217;ll combine <b><i>cprintf</i></b>&#8216;s font-styling capabilities, to enable setting colored prompts.</p><h3>Setting a continuously-updated timestamp prompt</h3><p>Using the code above, we can now display a dynamic timestamp prompt, as follows:<br
/><center><div
class="wp-caption aligncenter" style="width: 438px"><img
alt="setPrompt usage examples" src="http://UndocumentedMatlab.com/images/setPrompt.png" title="setPrompt usage examples" width="428" height="224" /><p
class="wp-caption-text"><b><i>setPrompt</i></b> usage examples</p></div></center></p><p>However, the displayed timestamp is somewhat problematic in the sense that it indicates the time of prompt <u>creation</u> rather than the time that the associated Command-Window command was <u>executed</u>. In the screenshot above, [25-Jan-2010 01:29:42] is the time that the <b><i>234</i></b> command was executed, not the time that the <b><i>setPrompt</i></b> command was executed. This is somewhat misleading. It would be better if the last (current) timestamp was continuously updated and would therefore always display the latest command&#8217;s execution time. This can be done using a Matlab <b><i>timer</i></b> as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% This is entered in the main function before setting the prompt:</span>
stopPromptTimers;
<span style="color: #0000FF;">if</span> <span style="color: #0000FF;">nargin</span> <span style="color: #F0F;">&amp;&amp;</span> <span style="color: #0000FF;">strcmpi</span><span style="color: #080;">&#40;</span>newPrompt,<span style="color:#A020F0;">'timestamp'</span><span style="color: #080;">&#41;</span>
  <span style="color: #228B22;">% Update initial prompt &amp; prepare a timer to continuously update it</span>
  newPrompt = @<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color:#A020F0;">'['</span>,<span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">now</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'] '</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
  start<span style="color: #080;">&#40;</span>timer<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Tag'</span>,<span style="color:#A020F0;">'setPromptTimer'</span>, <span style="color:#A020F0;">'Name'</span>,<span style="color:#A020F0;">'setPromptTimer'</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'ExecutionMode'</span>,<span style="color:#A020F0;">'fixedDelay'</span>, <span style="color:#A020F0;">'ObjectVisibility'</span>,<span style="color:#A020F0;">'off'</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'Period'</span>,<span style="color: #33f;">0.99</span>, <span style="color:#A020F0;">'StartDelay'</span>,<span style="color: #33f;">0.5</span>, <span style="color: #F0F;">...</span>
              <span style="color:#A020F0;">'TimerFcn'</span>,<span style="color: #080;">&#123;</span>@setPromptTimerFcn,jTextArea<span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Stop &amp; delete any existing prompt timer(s)</span>
<span style="color: #0000FF;">function</span> stopPromptTimers
  <span style="color: #0000FF;">try</span>
    timers = timerfindall<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'tag'</span>,<span style="color:#A020F0;">'setPromptTimer'</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>timers<span style="color: #080;">&#41;</span>
      stop<span style="color: #080;">&#40;</span>timers<span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">delete</span><span style="color: #080;">&#40;</span>timers<span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Never mind...</span>
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% stopPromptTimers</span>
&nbsp;
<span style="color: #228B22;">% Internal timer callback function</span>
<span style="color: #0000FF;">function</span> setPromptTimerFcn<span style="color: #080;">&#40;</span>timerObj,eventData,jTextArea<span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">try</span>
    <span style="color: #0000FF;">try</span> jTextArea = jTextArea.<span style="">java</span>;  <span style="color: #0000FF;">catch</span>,  <span style="color: #0000FF;">end</span>  <span style="color: #228B22;">%#ok</span>
    pos = <span style="color: #0000FF;">getappdata</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'setPromptPos'</span><span style="color: #080;">&#41;</span>;
    newPrompt = <span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">now</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'replaceRange(Ljava.lang.String;II)'</span>,<span style="color: #F0F;">...</span>
              <span style="">newPrompt</span>, pos, pos+<span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>newPrompt<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jTextArea,<span style="color:#A020F0;">'repaint()'</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Never mind...</span>
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% setPromptTimerFcn</span></pre></div></div><p>Can you come up with some innovative prompts? If so, please share them in a comment below.</p><p><u>Update 2010-Jan-26</u>: The code in this article was updated since it was first published yesterday.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/setting-the-matlab-desktop-layout-programmatically/' rel='bookmark' title='Setting the Matlab desktop layout programmatically'>Setting the Matlab desktop layout programmatically</a> <small>The Matlab desktop enables saving and switching layouts using the main menu. This post shows how to do so programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-desktop-tab-completions/' rel='bookmark' title='Setting desktop tab completions'>Setting desktop tab completions</a> <small>The Matlab desktop's Command-Window tab-completion can be customized for user-defined functions...</small></li><li><a
href='http://undocumentedmatlab.com/blog/editormacro-v2-setting-command-window-key-bindings/' rel='bookmark' title='EditorMacro v2 &#8211; setting Command Window key-bindings'>EditorMacro v2 &#8211; setting Command Window key-bindings</a> <small>The EditorMacro utility was extended to support built-in Matlab Editor and Command-Window actions and key-bindings. This post describes the changes and the implementation details....</small></li><li><a
href='http://undocumentedmatlab.com/blog/setting-status-bar-text/' rel='bookmark' title='Setting status-bar text'>Setting status-bar text</a> <small>The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/setprompt-setting-matlab-desktop-prompt/feed/</wfw:commentRss> <slash:comments>9</slash:comments> </item> <item><title>Customizing Matlab&#8217;s Workspace table</title><link>http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/</link> <comments>http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/#comments</comments> <pubDate>Sun, 03 Jan 2010 00:34:29 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Desktop]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=882</guid> <description><![CDATA[The Matlab Desktop's Workspace pane table can be customized, as described here<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-workspace-context-menu/' rel='bookmark' title='Customizing Workspace context-menu'>Customizing Workspace context-menu</a> <small>Matlab's Workspace table context-menu can be configured with user-defined actions - this article explains how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-help-popup-contents/' rel='bookmark' title='Customizing help popup contents'>Customizing help popup contents</a> <small>The built-in HelpPopup, available since Matlab R2007b, has a back-door that enables displaying arbitrary text, HTML and URL web-pages....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree/' rel='bookmark' title='Customizing uitree'>Customizing uitree</a> <small>This article describes how to customize Matlab GUI tree controls created using the undocumented uitree function...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A few days ago, a CSSM user <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/269361">asked</a> whether it is possible to modify the appearance of the Bytes column in the Workspace pane, so that it will present data in KBytes rather than in Bytes. Although I promised that my next post will explain FindJObj and its uses, I couldn&#8217;t resist the challenge. Here&#8217;s the solution to the request:</p><p>In this post I will assume Matlab release R2008a (7.6) &#8211; the adaptations for other releases should be minor at worst. First, we need to retrieve the Workspace table&#8217;s Java reference handle. In the past I&#8217;ve already shown <a
target="_blank" href="http://undocumentedmatlab.com/?s=MLDesktop">several uses for the Matlab Desktop&#8217;s Java handle</a>. Today we&#8217;ll use this handle to get the Workspace pane&#8217;s handle:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; jDesktop = com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLDesktop</span>.<span style="">getInstance</span>
jDesktop =
com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLDesktop</span>@42d390
&nbsp;
&gt;&gt; jWSBrowser = jDesktop.<span style="">getClient</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Workspace'</span><span style="color: #080;">&#41;</span>
jWSBrowser =
com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="color: #0000FF;">workspace</span>.<span style="">WorkspaceBrowser</span><span style="color: #080;">&#91;</span><span style="color: #0000FF;">Workspace</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">24</span>,355x707,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; jWSTable = jWSBrowser.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>
jWSTable =
com.<span style="">mathworks</span>.<span style="">mlwidgets</span>.<span style="color: #0000FF;">workspace</span>.<span style="">WorkspaceTable</span><span style="color: #080;">&#91;</span>WorkspaceTable,<span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,352x102,<span style="color: #F0F;">...</span><span style="color: #080;">&#93;</span></pre></div></div><p>Next, we note that jWSTable is a simple java Swing <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/table.html">JTable</a>, and as such we can easily modify its column header:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jWSTable.<span style="">getColumn</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Bytes'</span><span style="color: #080;">&#41;</span>.<span style="">setHeaderValue</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'KBytes'</span><span style="color: #080;">&#41;</span>;
jWSBrowser.<span style="">repaint</span>;</pre></div></div><p>Modifying the column&#8217;s behavior to display 1/1024 of the initial values is more tricky. We can use a simple <a
target="_blank" rel="nofollow" href="http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#renderer">TableCellRenderer</a>, replacing WorkspaceTable&#8217;s DefaultTableCellRenderer. We first create the following <a
target="_blank" href="http://UndocumentedMatlab.com/files/KBytesCellRenderer.java">KBytesCellRenderer.java</a> file:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.SwingConstants.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.table.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> KBytesCellRenderer <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">DefaultTableCellRenderer</span>
                                <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">TableCellRenderer</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Component</span> getTableCellRendererComponent<span style="color: #009900;">&#40;</span><span style="color: #003399;">JTable</span> table, <span style="color: #003399;">Object</span> value, <span style="color: #000066; font-weight: bold;">boolean</span> isSelected, <span style="color: #000066; font-weight: bold;">boolean</span> hasFocus, <span style="color: #000066; font-weight: bold;">int</span> row, <span style="color: #000066; font-weight: bold;">int</span> column<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">Component</span> cell <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">getTableCellRendererComponent</span><span style="color: #009900;">&#40;</span>table, value, isSelected, hasFocus, row, column<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//System.out.println(row + &quot;,&quot; + column + &quot; =&gt; &quot; + value);</span>
    <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>KBytesCellRenderer<span style="color: #009900;">&#41;</span>cell<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setHorizontalAlignment</span><span style="color: #009900;">&#40;</span>TRAILING<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// TRAILING = right</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">int</span> bytes <span style="color: #339933;">=</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>value.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>KBytesCellRenderer<span style="color: #009900;">&#41;</span>cell<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>bytes<span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>          <span style="color: #666666; font-style: italic;">// Bytes =&gt; KBytes</span>
    <span style="color: #000000; font-weight: bold;">return</span> cell<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> KBytesCellRenderer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>Next, we find out our Matlab&#8217;s Java version:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">version</span> -java
<span style="color: #0000FF;">ans</span> =
Java 1.6.0 with Sun Microsystems Inc. <span style="">Java</span> HotSpot<span style="color: #080;">&#40;</span>TM<span style="color: #080;">&#41;</span> Client VM mixed mode</pre></div></div><p>Next, we download and install a JDK version compatible with our Matlab&#8217;s Java version. You can download the latest JDK from <a
target="_blank" rel="nofollow" href="http://www.java.com/en/download/manual.jsp">here</a> or <a
target="_blank" rel="nofollow" href="http://java.sun.com/javase/downloads/index.jsp">here</a>. Previous JDK versions can be downloaded from <a
target="_blank" rel="nofollow" href="http://java.sun.com/javase/downloads/previous.jsp">here</a> or <a
target="_blank" rel="nofollow" href="http://java.sun.com/products/archive/">here</a> (which even lets you select your requested update number). Development versions, usually fixing reported bugs, are described and can be downloaded from <a
target="_blank" rel="nofollow" href="https://jdk6.dev.java.net/">here</a>. Note that you need the full JDK, not just the JVM/JRE runtime versions.</p><p>Next, we compile this file using the JDK&#8217;s Java compiler (<i>javac</i>) utility: In your system&#8217;s command-line (outside Matlab), type the following in the folder containing your KBytesCellRenderer.java file:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">javac KBytesCellRenderer.<span style="">java</span></pre></div></div><p>If all goes well, <i>javac</i> will report no error and will create a KBytesCellRenderer.class file in the current folder (or you can download it directly from <a
target=_blank" href="http://UndocumentedMatlab.com/files/KBytesCellRenderer.class">here</a>).</p><p>Now copy this KBytesCellRenderer.class file to one of the folders in your Matlab&#8217;s <b><i>javaclasspath</i></b> (for example, C:\Program Files\Matlab\R2008a\java\patch\) and restart Matlab. Don&#8217;t worry &#8211; all this is only a one-time operation.</p><p>After restarting Matlab, we have all the chips in place, so we can place the following code in our startup.m script:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jDesktop = com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLDesktop</span>.<span style="">getInstance</span>;
jWSBrowser = jDesktop.<span style="">getClient</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Workspace'</span><span style="color: #080;">&#41;</span>;
jWSTable = jWSBrowser.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
jWSTable.<span style="">getColumn</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Bytes'</span><span style="color: #080;">&#41;</span>.<span style="">setHeaderValue</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'KBytes'</span><span style="color: #080;">&#41;</span>;
jWSTable.<span style="">getColumn</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Bytes'</span><span style="color: #080;">&#41;</span>.<span style="">setCellRenderer</span><span style="color: #080;">&#40;</span>KBytesCellRenderer<span style="color: #080;">&#41;</span>;
jWSBrowser.<span style="">repaint</span>;</pre></div></div><p><center><br
/><div
class="wp-caption aligncenter" style="width: 369px"><img
alt="Before - bytes" src="http://UndocumentedMatlab.com/images/Workspace_Bytes.png" title="Before - bytes" width="359" height="150" /><p
class="wp-caption-text">Before - bytes</p></div><br
/><div
class="wp-caption aligncenter" style="width: 369px"><img
alt="After - KBytes" src="http://UndocumentedMatlab.com/images/Workspace_KBytes2.png" title="After - KBytes" width="359" height="194" /><p
class="wp-caption-text">After - KBytes</p></div><br
/></center></p><p>We can use a slightly more complex CellRenderer to highlight cells with too high a value or to present <a
target="_blank" rel="nofollow" href="http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html">thousands (comma) separator</a> by simply modifying and recompiling KBytesCellRenderer.java, updating the class file in our <b><i>javaclasspath</i></b> folder and restarting Matlab. Here&#8217;s the version for the thousands separator (American locale):</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.SwingConstants.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.table.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.text.NumberFormat</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> KBytesCellRenderer <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">DefaultTableCellRenderer</span>
                                <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">TableCellRenderer</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Component</span> getTableCellRendererComponent<span style="color: #009900;">&#40;</span><span style="color: #003399;">JTable</span> table, <span style="color: #003399;">Object</span> value, <span style="color: #000066; font-weight: bold;">boolean</span> isSelected, <span style="color: #000066; font-weight: bold;">boolean</span> hasFocus, <span style="color: #000066; font-weight: bold;">int</span> row, <span style="color: #000066; font-weight: bold;">int</span> column<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">Component</span> cell <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">getTableCellRendererComponent</span><span style="color: #009900;">&#40;</span>table, value, isSelected, hasFocus, row, column<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//System.out.println(row + &quot;,&quot; + column + &quot; =&gt; &quot; + value);</span>
    <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>KBytesCellRenderer<span style="color: #009900;">&#41;</span>cell<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setHorizontalAlignment</span><span style="color: #009900;">&#40;</span>TRAILING<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// TRAILING = Right</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">int</span> bytes <span style="color: #339933;">=</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>value.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">NumberFormat</span> nf <span style="color: #339933;">=</span> <span style="color: #003399;">NumberFormat</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>KBytesCellRenderer<span style="color: #009900;">&#41;</span>cell<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>nf.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>bytes<span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">// Bytes =&gt; KBytes</span>
    <span style="color: #000000; font-weight: bold;">return</span> cell<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> KBytesCellRenderer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 369px"><img
alt="After - formatted KBytes" src="http://UndocumentedMatlab.com/images/Workspace_KBytes.png" title="After - formatted KBytes" width="359" height="196" /><p
class="wp-caption-text">After - formatted KBytes</p></div></center></p><p>If you have created an interesting CellRenderer, I will be happy to hear about it in the comments section below.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/customizing-workspace-context-menu/' rel='bookmark' title='Customizing Workspace context-menu'>Customizing Workspace context-menu</a> <small>Matlab's Workspace table context-menu can be configured with user-defined actions - this article explains how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-help-popup-contents/' rel='bookmark' title='Customizing help popup contents'>Customizing help popup contents</a> <small>The built-in HelpPopup, available since Matlab R2007b, has a back-door that enables displaying arbitrary text, HTML and URL web-pages....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-uitree/' rel='bookmark' title='Customizing uitree'>Customizing uitree</a> <small>This article describes how to customize Matlab GUI tree controls created using the undocumented uitree function...</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlab-labels/' rel='bookmark' title='Customizing Matlab labels'>Customizing Matlab labels</a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/feed/</wfw:commentRss> <slash:comments>17</slash:comments> </item> <item><title>Customizing help popup contents</title><link>http://undocumentedmatlab.com/blog/customizing-help-popup-contents/</link> <comments>http://undocumentedmatlab.com/blog/customizing-help-popup-contents/#comments</comments> <pubDate>Mon, 30 Nov 2009 16:37:30 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Desktop]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[HTML]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=819</guid> <description><![CDATA[The built-in HelpPopup, available since Matlab R2007b, has a back-door that enables displaying arbitrary text, HTML and URL web-pages.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/setting-system-tray-popup-messages/' rel='bookmark' title='Setting system tray popup messages'>Setting system tray popup messages</a> <small>System-tray icons and messages can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-workspace-context-menu/' rel='bookmark' title='Customizing Workspace context-menu'>Customizing Workspace context-menu</a> <small>Matlab's Workspace table context-menu can be configured with user-defined actions - this article explains how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/' rel='bookmark' title='Rich Matlab editbox contents'>Rich Matlab editbox contents</a> <small>The Matlab editbox uicontrol does not handle HTML contents as do other uicontrols. In this article I show how this limitation can be removed....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/' rel='bookmark' title='Customizing Matlab&#8217;s Workspace table'>Customizing Matlab&#8217;s Workspace table</a> <small>The Matlab Desktop's Workspace pane table can be customized, as described here...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A few days ago, I was <a
target="_blank" href="http://undocumentedmatlab.com/blog/context-sensitive-help/#comment-4691">asked by a reader</a> how to programmatically display the popup help window and customize it with arbitrary contents. This help window displays the <b><i>doc</i></b>-page associated with the current Command Window or Editor text.</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><img
alt="Help popup" src="http://UndocumentedMatlab.com/images/HelpPopup2.png" title="Help popup" width="450" height="334" /><p
class="wp-caption-text">Help popup</p></div></center></p><p>To programmatically display this help popup, a <a
target="_blank" rel="nofollow" href="http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/modality/">modeless MJDialog Java window</a>, we need to run the following on Matlab releases that support these windows (R2007b onward):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jDesktop = com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLDesktop</span>.<span style="">getInstance</span>;
jTextArea = jDesktop.<span style="">getMainFrame</span>.<span style="">getFocusOwner</span>;
jClassName = <span style="color:#A020F0;">'com.mathworks.mlwidgets.help.HelpPopup'</span>;
jPosition = java.<span style="">awt</span>.<span style="color: #0000FF;">Rectangle</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">0</span>,<span style="color: #33f;">400</span>,<span style="color: #33f;">300</span><span style="color: #080;">&#41;</span>;
helpTopic = <span style="color:#A020F0;">'surf'</span>;
javaMethodEDT<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'showHelp'</span>,jClassName,jTextArea,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,jPosition,helpTopic<span style="color: #080;">&#41;</span>;</pre></div></div><p>Where:<br
/> 1) jPosition sets popup’s pixel size and position (X,Y,Width,Height). Remember that Java counts from the top down (contrary to Matlab) and is 0-based. Therefore, Rectangle(0,0,400,300) is a 400×300 window at the screen’s top-left corner.<br
/> 2) helpTopic is the help topic of your choice (the output of the <b><i>doc</i></b> function). To display arbitrary text, you can create a simple .m file that only has a main help comment with your arbitrary text, which will be presented in the popup.<br
/> 3) on R2011b, we could simplify the above code snippet with the following replacement:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">helpUtils.<span style="">errorDocCallback</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'surf'</span><span style="color: #080;">&#41;</span></pre></div></div><p>4) on R2007b, we need to use the equivalent but more cumbersome <b><i>awtinvoke</i></b> function instead of <b><i>javaMethodEDT</i></b>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">jniSig = <span style="color:#A020F0;">'showHelp(Ljavax.swing.JComponent;Lcom.mathworks.mwswing.binding.KeyStrokeList;Ljava.awt.Rectangle;Ljava.lang.String;)'</span>;
<span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jClassName,jniSig,jTextArea,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>,jPosition,helpTopic<span style="color: #080;">&#41;</span>;</pre></div></div><p>For example, if we had a sample.m file with the following contents:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> sample
<span style="color: #228B22;">% The text in this function's main comment will be presented in the</span>
<span style="color: #228B22;">% help popup. &lt;a href=&quot;http://UndocumentedMatlab.com&quot;&gt;Hyperlinks&lt;/a&gt;</span>
<span style="color: #228B22;">% are supported, but unfortunately not full-fledged HTML.</span></pre></div></div><p>Then we would get this result:<br
/><center><div
class="wp-caption aligncenter" style="width: 410px"><img
alt="User-created arbitrary text" src="http://UndocumentedMatlab.com/images/HelpPopup3.png" title="User-created arbitrary text" width="400" height="194" /><p
class="wp-caption-text">User-created arbitrary text</p></div></center></p><p>Well, it does get the message across, but looks rather dull. It would be nice if this could be improved to provide full-scale HTML support. Unfortunately, <a
rel="nofollow" target="_blank" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/doc.html">Matlab documentation</a> says this cannot be done:</p><p><q>The <b>doc</b> function is intended only for reference pages supplied by The MathWorks. The exception is the <b>doc</b> UserCreatedClassName syntax. <b>doc</b> does not display HTML files you create yourself. To display HTML files for functions you create, use the web function.</q></p><p>Luckily for us, there is an undocumented back-door to do this: The idea is to search all visible Java windows for the HelpPopup, then move down its component hierarchy to the internal web browser (a com.mathworks.mlwidgets.html.HTMLBrowserPanel object), then update the content with our arbitrary HTML or a webpage URL:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Find the Help popup window</span>
jWindows = com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">MJDialog</span>.<span style="">getWindows</span>;
jPopup = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>;
<span style="color: #0000FF;">for</span> idx=<span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>jWindows<span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">strcmp</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>jWindows<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'Name'</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'HelpPopup'</span><span style="color: #080;">&#41;</span>
    <span style="color: #0000FF;">if</span> jWindows<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span>.<span style="">isVisible</span>
      jPopup = jWindows<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span>;
      <span style="color: #0000FF;">break</span>;
    <span style="color: #0000FF;">end</span>
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Update the popup with selected HTML</span>
html=<span style="color: #080;">&#91;</span><span style="color:#A020F0;">'Full HTML support: &lt;b&gt;&lt;font color=red&gt;bold&lt;/font&gt;&lt;/b&gt;, '</span><span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'&lt;i&gt;italic&lt;/i&gt;, &lt;a href=&quot;matlab:dir&quot;&gt;hyperlink&lt;/a&gt;, '</span> <span style="color: #F0F;">...</span>
      <span style="color:#A020F0;">'symbols (&amp;#8704;&amp;#946;) etc.'</span><span style="color: #080;">&#93;</span>;
<span style="color: #0000FF;">if</span> ~<span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>jPopup<span style="color: #080;">&#41;</span>
  browser = jPopup.<span style="">getContentPane</span>.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>.<span style="">getComponent</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
  browser.<span style="">setHtmlText</span><span style="color: #080;">&#40;</span>html<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 237px"><img
alt="Help popup with HTML content" src="http://UndocumentedMatlab.com/images/HelpPopup4.png" title="Help popup with HTML content" width="227" height="117" /><p
class="wp-caption-text">Help popup with HTML content</p></div></center></p><p>We can display HTML content and highlight certain keywords using the <i>setHtmlTextAndHighlightKeywords</i> method:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">browser.<span style="">setHtmlTextAndHighlightKeywords</span><span style="color: #080;">&#40;</span>html,<span style="color: #080;">&#123;</span><span style="color:#A020F0;">'support'</span>,<span style="color:#A020F0;">'symbols'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 237px"><img
alt="HTML content with highlighting" src="http://UndocumentedMatlab.com/images/HelpPopup5.png" title="HTML content with highlighting" width="227" height="117" /><p
class="wp-caption-text">HTML content with highlighting</p></div></center></p><p>Instead of specifying the HTML content, we can point this browser to a web-page URL (no need for the &#8220;http://&#8221; prefix):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">browser.<span style="">setCurrentLocation</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'UndocumentedMatlab.com'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 460px"><a
href="http://UndocumentedMatlab.com/images/HelpPopup6.png"><img
alt="Help popup browser displaying a URL web-page" src="http://UndocumentedMatlab.com/images/HelpPopup6.png" title="Help popup browser displaying a URL web-page" width="450" height="380" /></a><p
class="wp-caption-text">Help popup browser displaying a URL web-page</p></div></center></p><p>The HTMLBrowserPanel includes a full-fledged browser (which may be different across Matlab releases/platforms). This browser supports HTML, CSS, JavaScript and other web-rendering aspect that we would expect from a modern browser. Being a full-fledged browser, we have some control over its appearance e.g., <i>addAddressBox(1,1)</i> and other internal methods. Interested readers may use my <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/17935-uiinspect-display-methodspropertiescallbacks-of-an-object">UiInspect utility</a> to explore these options.</p><p>As a technical note, HTMLBrowserPanel is actually only a JPanel that contains the actual Mozilla browser. Luckily for us, MathWorks extended this panel class with the useful methods presented above, that forward the user requests to the actual internal browser. This way, we don&#8217;t need to get the actual browser reference (although you can, of course).</p><p>I have created a utility function that encapsulates all the above, and enables display of Matlab doc pages, as well as arbitrary text, HTML or webpages. This <b><i> popupPanel</i></b> utility can now be <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/25975-popuppanel-displays-a-popup-panel-with-specified-help-topic-html-or-webpage">downloaded from the Matlab File Exchange</a>.</p><p>An interesting exercise left for the readers, is adapting the main heavy-weight documentation window to display user-created HTML help pages. This can be achieved by means very similar to those shown in this article.</p><p>Of course, as the official documentation states, we could always use the fully-supported <b><i>web</i></b> function to display our HTML or URLs. Under the hood, <b><i>web</i></b> uses exactly the same HTMLBrowserPanel as out HelpPopup. The benefit of using the methods shown here is the use of a lightweight undecorated popup window which looks well-integrated with the existing Matlab help.</p><p>Please note that the HelpPopup implementation might change without warning between Matlab releases. An entirely separate, although related, implementation is Matlab&#8217;s built-in <a
target="_blank" href="http://undocumentedmatlab.com/blog/context-sensitive-help/">context-sensitive help system</a>, which I described some months ago. That implementation did not rely on Java and worked on much earlier Matlab releases.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/setting-system-tray-popup-messages/' rel='bookmark' title='Setting system tray popup messages'>Setting system tray popup messages</a> <small>System-tray icons and messages can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-workspace-context-menu/' rel='bookmark' title='Customizing Workspace context-menu'>Customizing Workspace context-menu</a> <small>Matlab's Workspace table context-menu can be configured with user-defined actions - this article explains how....</small></li><li><a
href='http://undocumentedmatlab.com/blog/rich-matlab-editbox-contents/' rel='bookmark' title='Rich Matlab editbox contents'>Rich Matlab editbox contents</a> <small>The Matlab editbox uicontrol does not handle HTML contents as do other uicontrols. In this article I show how this limitation can be removed....</small></li><li><a
href='http://undocumentedmatlab.com/blog/customizing-matlabs-workspace-table/' rel='bookmark' title='Customizing Matlab&#8217;s Workspace table'>Customizing Matlab&#8217;s Workspace table</a> <small>The Matlab Desktop's Workspace pane table can be customized, as described here...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/customizing-help-popup-contents/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>Accessing the Matlab Editor</title><link>http://undocumentedmatlab.com/blog/accessing-the-matlab-editor/</link> <comments>http://undocumentedmatlab.com/blog/accessing-the-matlab-editor/#comments</comments> <pubDate>Mon, 19 Oct 2009 18:00:20 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Desktop]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Editor]]></category> <category><![CDATA[FindJObj]]></category> <category><![CDATA[UIInspect]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=671</guid> <description><![CDATA[The Matlab Editor can be accessed programmatically, for a wide variety of possible uses - this article shows how.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/editormacro-assign-a-keyboard-macro-in-the-matlab-editor/' rel='bookmark' title='EditorMacro &#8211; assign a keyboard macro in the Matlab editor'>EditorMacro &#8211; assign a keyboard macro in the Matlab editor</a> <small>EditorMacro is a new utility that enables setting keyboard macros in the Matlab editor. this post details its inner workings....</small></li><li><a
href='http://undocumentedmatlab.com/blog/non-textual-editor-actions/' rel='bookmark' title='Non-textual editor actions'>Non-textual editor actions</a> <small>The UIINSPECT utility can be used to expand EditorMacro capabilities to non-text-insertion actions. This is how:...</small></li><li><a
href='http://undocumentedmatlab.com/blog/accessing-plot-brushed-data/' rel='bookmark' title='Accessing plot brushed data'>Accessing plot brushed data</a> <small>Plot data brushing can be accessed programmatically using very simple pure-Matlab code...</small></li><li><a
href='http://undocumentedmatlab.com/blog/recovering-previous-editor-state/' rel='bookmark' title='Recovering previous editor state'>Recovering previous editor state</a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Matlab&#8217;s built-in editor, like most other Matlab GUI, is Java-based. As such, it can easily be accessed programmatically. ImageAnalyst, a well-respected member of the Matlab community and a frequent CSSM (newsgroup) and FEX (File Exchange) contributor, <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/263329#687602">recently asked</a> whether it is possible to retrieve the name of the Editor&#8217;s currently edited file. The answer is that this is very easy, but I decided to use this opportunity to show how other interesting things can be done with the Editor.</p><p>Before we start, it should be made clear that this entire article relies on MathWorks internal implementation of the Editor and Desktop, which may change without prior notice in future Matlab releases. The code below appears to work under Matlab 6 &#038; 7, but users who rely on forward compatibility should be aware of this warning.</p><p>We start by retrieving the Editor handle. This can be done in a number of ways. The easiest is via the Matlab desktop:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">try</span>
    <span style="color: #228B22;">% Matlab 7</span>
    desktop = com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLDesktop</span>.<span style="">getInstance</span>;
    jEditor = desktop.<span style="">getGroupContainer</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Editor'</span><span style="color: #080;">&#41;</span>.<span style="">getTopLevelAncestor</span>;
    <span style="color: #228B22;">% we get a com.mathworks.mde.desk.MLMultipleClientFrame object</span>
<span style="color: #0000FF;">catch</span>
    <span style="color: #228B22;">% Matlab 6</span>
    <span style="color: #228B22;">% Unfortunately, we can't get the Editor handle from the Desktop handle in Matlab 6:</span>
    <span style="color: #228B22;">%desktop = com.mathworks.ide.desktop.MLDesktop.getMLDesktop;</span>
&nbsp;
    <span style="color: #228B22;">% So here's the workaround for Matlab 6:</span>
    openDocs = com.<span style="">mathworks</span>.<span style="">ide</span>.<span style="">editor</span>.<span style="">EditorApplication</span>.<span style="">getOpenDocuments</span>;  <span style="color: #228B22;">% a java.util.Vector</span>
    firstDoc = openDocs.<span style="">elementAt</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% a com.mathworks.ide.editor.EditorViewContainer object</span>
    jEditor = firstDoc.<span style="">getParent</span>.<span style="">getParent</span>.<span style="">getParent</span>;
    <span style="color: #228B22;">% we get a com.mathworks.mwt.MWTabPanel or com.mathworks.ide.desktop.DTContainer object</span>
<span style="color: #0000FF;">end</span></pre></div></div><p>Now that we have the Editor handle, let&#8217;s retrieve its currently open (active) file name from the Editor&#8217;s title:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">title</span> = jEditor.<span style="">getTitle</span>;
currentFilename = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">title</span>.<span style="">replaceFirst</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Editor - '</span>,<span style="color:#A020F0;">''</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>The entire list of open file names can be retrieved in several ways:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Alternative #1:</span>
edhandle = com.<span style="">mathworks</span>.<span style="">mlservices</span>.<span style="">MLEditorServices</span>;
allEditorFilenames = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>edhandle.<span style="">builtinGetOpenDocumentNames</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Alternative #2:</span>
openFiles = desktop.<span style="">getWindowRegistry</span>.<span style="">getClosers</span>.<span style="">toArray</span>.<span style="color: #0000FF;">cell</span>;
allEditorFilenames = <span style="color: #0000FF;">cellfun</span><span style="color: #080;">&#40;</span>@<span style="color: #080;">&#40;</span>c<span style="color: #080;">&#41;</span>c.<span style="">getTitle</span>.<span style="color: #0000FF;">char</span>,openFiles,<span style="color:#A020F0;">'un'</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>At the top-level Editor-window level, we can prevent its resizing, update its status bar, modify its toolbar/menu-bar, control docking and do other similar fun things:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Actions via built-in methods:</span>
jEditor.<span style="">setResizable</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
jEditor.<span style="">setStatusText</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'testing 123...'</span><span style="color: #080;">&#41;</span>;
jEditor.<span style="">setTitle</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'This is the Matlab Editor'</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Equivalent actions via properties:</span>
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jEditor, <span style="color:#A020F0;">'Resizable'</span>, <span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jEditor, <span style="color:#A020F0;">'StatusText'</span>, <span style="color:#A020F0;">'testing 123...'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span>jEditor, <span style="color:#A020F0;">'Title'</span>, <span style="color:#A020F0;">'This is the Matlab Editor'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Actually, the jEditor handle has over 300 invokable methods and close to 200 properties that we can get/set. Perhaps the easiest way to find interesting things we can programmatically do with the Editor handle, is to use my <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/17935">UIInspect</a> utility on the File Exchange:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">uiinspect<span style="color: #080;">&#40;</span>jEditor<span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% or: jEditor.uiinspect</span></pre></div></div><div
class="wp-caption aligncenter" style="width: 460px"><a
href="http://undocumentedmatlab.com/images/Editor_uiinspect.png" target="_blank"><img
alt="Matlab Editor methods, callbacks and properties as seen by uiinspect (click to zoom)" src="http://undocumentedmatlab.com/images/Editor_uiinspect.png" title="Matlab Editor methods, callbacks and properties as seen by uiinspect (click to zoom)" width="450" height="403" /></a><p
class="wp-caption-text">Matlab Editor methods, callbacks and properties as seen by <b><i>uiinspect</i></b><br
/>(click to zoom)</p></div><p>The Editor handle is actually a container for many internal panels (toolbars etc.) and documents. The entire object hierarchy can be seen with another of my File Exchange utilities, <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/14317">FindJObj</a>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">findjobj<span style="color: #080;">&#40;</span>jEditor<span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% or: jEditor.findjobj</span></pre></div></div><div
class="wp-caption aligncenter" style="width: 460px"><a
href="http://undocumentedmatlab.com/images/Editor_findjobj.png" target="_blank"><img
alt="Matlab Editor object hierarchy as seen by findjboj (click to zoom)" src="http://undocumentedmatlab.com/images/Editor_findjobj.png" title="Matlab Editor object hierarchy as seen by findjboj (click to zoom)" width="450" height="391" /></a><p
class="wp-caption-text">Matlab Editor object hierarchy as seen by <b><i>findjboj</i></b> (click to zoom)</p></div><p>We can modify text within the open Editor documents, and instrument these document to handle event callbacks. To see how, I refer users to my <a
target="_blank" href="http://undocumentedmatlab.com/blog/editormacro-assign-a-keyboard-macro-in-the-matlab-editor/">EditorMacro</a> utility on the Matlab File Exchange.</p><p>If you find some other nifty and/or useful things that can be done using the Editor handle, please post them in the comments section below.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/editormacro-assign-a-keyboard-macro-in-the-matlab-editor/' rel='bookmark' title='EditorMacro &#8211; assign a keyboard macro in the Matlab editor'>EditorMacro &#8211; assign a keyboard macro in the Matlab editor</a> <small>EditorMacro is a new utility that enables setting keyboard macros in the Matlab editor. this post details its inner workings....</small></li><li><a
href='http://undocumentedmatlab.com/blog/non-textual-editor-actions/' rel='bookmark' title='Non-textual editor actions'>Non-textual editor actions</a> <small>The UIINSPECT utility can be used to expand EditorMacro capabilities to non-text-insertion actions. This is how:...</small></li><li><a
href='http://undocumentedmatlab.com/blog/accessing-plot-brushed-data/' rel='bookmark' title='Accessing plot brushed data'>Accessing plot brushed data</a> <small>Plot data brushing can be accessed programmatically using very simple pure-Matlab code...</small></li><li><a
href='http://undocumentedmatlab.com/blog/recovering-previous-editor-state/' rel='bookmark' title='Recovering previous editor state'>Recovering previous editor state</a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/accessing-the-matlab-editor/feed/</wfw:commentRss> <slash:comments>23</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           category
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-05-18 21:21:08 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/category/desktop/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      2.692s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1337401266; expires=Sun, 19-May-2013 04:21:06 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Sat, 19 May 2012 04:21:08 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Sat, 19 May 2012 05:21:08 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               d4d6e377cbcdbc0dfa3ba7bf8d842c76
Content-Encoding:   gzip
-->
