<?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; Editor</title> <atom:link href="http://undocumentedmatlab.com/blog/tag/editor/feed/" rel="self" type="application/rss+xml" /><link>http://undocumentedmatlab.com</link> <description>Charting Matlab's unsupported hidden underbelly</description> <lastBuildDate>Thu, 02 Feb 2012 00:24:18 +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>MLintFailureFiles or: Why can&#8217;t I save my m-file?!</title><link>http://undocumentedmatlab.com/blog/mlintfailurefiles/</link> <comments>http://undocumentedmatlab.com/blog/mlintfailurefiles/#comments</comments> <pubDate>Thu, 02 Feb 2012 00:14:15 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Editor]]></category> <category><![CDATA[Mlint]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2688</guid> <description><![CDATA[Sometimes Matlab gets into a state where it cannot use a valid m-file. This article explains what can be done.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/recovering-previous-editor-state/' rel='bookmark' title='Recovering previous editor state'>Recovering previous editor state</a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/more-undocumented-timing-features/' rel='bookmark' title='More undocumented timing features'>More undocumented timing features</a> <small>There are several undocumented ways in Matlab to get CPU and clock data...</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-scatter-plot-behavior/' rel='bookmark' title='Undocumented scatter plot behavior'>Undocumented scatter plot behavior</a> <small>The scatter plot function has an undocumented behavior when plotting more than 100 points: it returns a single unified patch object handle, rather than a patch handle for each specific...</small></li><li><a
href='http://undocumentedmatlab.com/blog/udd-events-and-listeners/' rel='bookmark' title='UDD Events and Listeners'>UDD Events and Listeners</a> <small>UDD event listeners can be used to listen to property value changes and other important events of Matlab objects...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A short while ago, I was working at a client&#8217;s location on some project, when a very strange thing happened: when editing an m-file in the Matlab editor, the mlint process (the one that reports errors and warnings on the right-hand side of the editor) suddenly decided that it has had enough with this specific file. It labeled the entire file as red, meaning that it has errors that prevent it from being run, although as far as we could both see the file was valid and had no syntax errors and actually ran fine.</p><p>Here&#8217;s a screenshot of the message that mlint reported in the editor:</p><p><center><div
class="wp-caption aligncenter" style="width: 542px"><img
alt="erroneous mlint error report" src="http://UndocumentedMatlab.com/images/MLintFailureFiles_mlint_msg.png" title="erroneous mlint error report" width="532" height="44"/><p
class="wp-caption-text">erroneous mlint error report</p></div></center></p><p>Mlint&#8217;s confusion would not have bothered us if it were not for a seemingly-related issue: When we tried to save the file, the Matlab editor would only allow us to save the file under a different name. When we attempted to restart Matlab, the same behavior repeated for this file, and we could not proceed with our actual work, modifying and saving our m-file.</p><p>Not easily intimidated, I tried saving the file under a different name in the editor, then renaming it back outside Matlab. No good, same problem.</p><h3 id="MLintFailureFiles">MLintFailureFiles or: yet another use for <i>prefdir</i></h3><p>The clue that eventually led to the fix was that somehow Matlab remembered the &#8220;faulty&#8221; state of the m-file even after I restarted Matlab. Matlab likes to store persistent cross-session information in a single folder, which is the user&#8217;s <i><b>prefdir</b></i> folder. I have used this to my advantage only a few weeks ago, when I <a
target="_blank" href="http://undocumentedmatlab.com/blog/recovering-previous-editor-state/">explained</a> how to use the stored editor state file that is stored in that folder.</p><p>My hunch was that Matlab might store the mlint&#8217;s &#8220;faulty&#8221; m-file state somewhere in there. I didn&#8217;t have to look very far: one of the files most recently modified had the tell-tale name of <code>MLintFailureFiles</code>. This turns out to be an empty file that contains the full pathname of the &#8220;faulty&#8221; m-files, one file per line.</p><p>The fix turned out to be very easy: simply remove my file from the MLintFailureFiles file (or delete MLintFailureFiles entirely) and restart Matlab. Problem solved – everything back to normal. Resume breathing.</p><h3 id="undocumented">Undocumented? well, not exactly</h3><p>I then went back to the suggestion in the mlint message and true enough I found related information in the documentation, at the bottom of the doc-page for using mlint. This is from R2008a&#8217;s doc page:</p><blockquote><p> <b>About M-Lint and Unexpected MATLAB&reg; Termination</b></p><p>Under some circumstances, when you are editing an M-file, M-Lint can cause the MATLAB session to terminate unexpectedly. The next time you start MATLAB, it displays the following message and disables M-Lint for the M-file that was open in the editor when MATLAB terminated.</p><pre>M-Lint caused your previous MATLAB session to terminate unexpectedly.
Please send this message and file name to The MathWorks.
See "About M-Lint and Unexpected MATLAB Termination" in the MATLAB documentation for details.
</pre><p></p><p>If you want, while waiting for a response from The MathWorks&trade;, you can attempt to reenable M-Lint for the file by following these steps:</p><ol><li>In the Editor, reopen the file that you were editing when MATLAB terminated.</li><li>Remove the lines of code that you believe M-Lint could not handle.</li><li>In a text editor, open the MLintFailureFiles file in your preferences directory. (This is the directory that MATLAB returns when you run prefdir.)</li><li>Save and reopen the M-file.</li></ol></blockquote><p>As you can see, this is not very helpful because it&#8217;s missing the step of actually editing (or deleting) the MLintFailureFiles file.</p><p>Additional documentation of this issue and workaround can be found in two separate Mathworks technical notes: <a
target="_blank" rel="nofollow" href="www.mathworks.com/support/solutions/en/data/1-74JVT3/">here</a> and <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-6ZFY8F/">here</a>.</p><p>A Matlab user has even created a <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/29319">utility</a> on the File Exchange that simply deletes this file, basing his work on the first note above.</p><p>The second note indicates that the problem (and workaround) occur in Matlab releases R2008a-R2010b, and indeed the above-mentioned issue (and workaround) can no longer be found in the latest docs (as far as I could see). However, even if this issue (and MLintFailureFiles) may no longer be relevant in the latest Matlab releases, we may still see MLintFailureFiles in our <i><b>prefdir</b></i>, because the <i><b>prefdir</b></i> contents are automatically copied from the previous release whenever we install a newer Matlab release.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/recovering-previous-editor-state/' rel='bookmark' title='Recovering previous editor state'>Recovering previous editor state</a> <small>Recovering the previous state of the Matlab editor and its loaded documents is possible using a built-in backup config file. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/more-undocumented-timing-features/' rel='bookmark' title='More undocumented timing features'>More undocumented timing features</a> <small>There are several undocumented ways in Matlab to get CPU and clock data...</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-scatter-plot-behavior/' rel='bookmark' title='Undocumented scatter plot behavior'>Undocumented scatter plot behavior</a> <small>The scatter plot function has an undocumented behavior when plotting more than 100 points: it returns a single unified patch object handle, rather than a patch handle for each specific...</small></li><li><a
href='http://undocumentedmatlab.com/blog/udd-events-and-listeners/' rel='bookmark' title='UDD Events and Listeners'>UDD Events and Listeners</a> <small>UDD event listeners can be used to listen to property value changes and other important events of Matlab objects...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/mlintfailurefiles/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>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/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/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/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/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>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>21</slash:comments> </item> <item><title>R2009b keyboard bindings</title><link>http://undocumentedmatlab.com/blog/r2009b-keyboard-bindings/</link> <comments>http://undocumentedmatlab.com/blog/r2009b-keyboard-bindings/#comments</comments> <pubDate>Sun, 06 Sep 2009 15:56:12 +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[keyboard]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=578</guid> <description><![CDATA[The new Matlab release R2009b includes the ability to customize keyboard bindings for the editor and Command Window. However, there are still some uses for the EditorMacro utility and its variants.<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/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/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-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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>This weekend, Kenn Orr, Matlab&#8217;s desktop development team leader, announced an important and long-awaited feature that becomes available in the upcoming R2009b release: the ability to <a
rel="nofollow" target="_blank" href="http://blogs.mathworks.com/desktop/2009/09/04/r2009b-is-here/">customize keyboard bindings in the system preferences</a>.</p><p>This is indeed an important preference customization, which has often been requested. At first look, it appears that the desktop design team has done a good job of enabling easy keyboard shortcuts customization, saving and loading sets of shortcuts etc. &#8211; all in a new easy-to-use preference page.</p><p>This need has been addressed in the past by my <a
target="_blank" href="http://undocumentedmatlab.com/blog/EditorMacro">EditorMacro utility</a>, its expansions (<a
target="_blank" href="http://undocumentedmatlab.com/blog/non-textual-editor-actions/">here</a> and <a
target="_blank" href="http://undocumentedmatlab.com/blog/editormacro-v2-setting-command-window-key-bindings/">here</a>), and Perttu Ranta-aho&#8217;s <a
target="_blank" href="http://www.mathworks.com/matlabcentral/fileexchange/25089" rel="nofollow">KeyBindings</a> derivative utility.</p><p>Kudos for a job apparently well-done for the Matlab dev team aside, there may still be reasons for using EditorMacro and/or KeyBindings:</p><ul><li><b>Earlier Matlab versions</b> &#8211; those who have a Matlab release earlier than R2009b have no option but to use these utilities. The first version of EditorMacro even supported Matlab 6.0, now almost a decade old!</li><li><b>Programmatic access</b> &#8211; some users may wish to have programmatic access to the keyboard bindings. For example, by saving sets of bindings in an m-file and accessing any of these sets via GUI or the desktop shortcuts toolbar. Note that in R2009b, these preferences can probably be accessed programmatically via the <a
target="_blank" href="http://undocumentedmatlab.com/blog/changing-system-preferences-programmatically/">preferences interface, explained here</a>.</li><li><b>Understanding the underlying workings</b> of the Matlab desktop and editor, for those interested in exploring and using these undocumented subjects.</li></ul><p>User-contributed utilities, especially those relying on undocumented features like EditorMacro and KeyBindings, will always pale next to sleek GUI preferences which are well-integrated by design. They have a place in niche usages, as explained above, but the hope is that all these needs will eventually be addressed by well-documented integrated features, exactly as happened in this particular case.</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/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/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-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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/r2009b-keyboard-bindings/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>EditorMacro v2 &#8211; setting Command Window key-bindings</title><link>http://undocumentedmatlab.com/blog/editormacro-v2-setting-command-window-key-bindings/</link> <comments>http://undocumentedmatlab.com/blog/editormacro-v2-setting-command-window-key-bindings/#comments</comments> <pubDate>Thu, 20 Aug 2009 13:02: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[Editor]]></category> <category><![CDATA[keyboard]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=518</guid> <description><![CDATA[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.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/r2009b-keyboard-bindings/' rel='bookmark' title='R2009b keyboard bindings'>R2009b keyboard bindings</a> <small>The new Matlab release R2009b includes the ability to customize keyboard bindings for the editor and Command Window. However, there are still some uses for the EditorMacro utility and its...</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/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><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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Some weeks ago, I introduced my <a
target="_blank" href="http://undocumentedmatlab.com/blog/editormacro-assign-a-keyboard-macro-in-the-matlab-editor/">EditorMacro utility</a> as a means of assigning user-defined text strings and runnable macros (callbacks) to keyboard shortcuts in the Matlab Editor. I later showed how <i><b>EditorMacro</b></i> can be used to <a
target="_blank" href="http://undocumentedmatlab.com/blog/non-textual-editor-actions/"> set non-textual editor macros</a>.</p><p>Since then, I was contacted by several users with questions, enhancement requests and improvement suggestions. Of these, the most notable was from Perttu Ranta-aho who suggested (and provided sample code for) using the editor&#8217;s built-in actions. The next logical step was to extend this to support the editor menus, and then extend again to support Command Window key-bindings. We bounced several increasingly-powerful versions of the utility between us, until arriving at the version I uploaded yesterday, which has the following significant improvements over the first version:</p><ul><li>several fixes: bugs, edge cases, stability (EDT etc.) etc.</li><li>support for built-in (native) Editor actions</li><li>support for built-in (native) Command-Window actions</li></ul><h3>Built-in native actions</h3><p>It turns out that the Editor and Command-Window both have some ~200 built-in (native) available actions, about half of them common. Actions are identified by name, which is a lowercase dash-separated description like &#8216;selection-up&#8217; (this format is familiar to Emacs users). Altogether, there are over 300 unique built-in actions which can be used, but only ~100 of them are assigned a default key-bindings. A few dozen actions have multiple assigned key-bindings. For example, the &#8216;selection-up&#8217; action is assigned to both &#8216;shift pressed UP&#8217; (=&lt;shift&gt;-&lt;up&gt;) and &#8216;shift pressed KP_UP&#8217; (=&lt;shift&gt;-&lt;Keypad-up&gt;):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #080;">&#91;</span>bindings, actions<span style="color: #080;">&#93;</span> = EditorMacro
actions = 
<span style="color: #F0F;">...</span><span style="color: #080;">&#91;</span>snip<span style="color: #080;">&#93;</span>
 <span style="color:#A020F0;">'selection-page-down'</span>     <span style="color:#A020F0;">'shift pressed PAGE_DOWN'</span>     <span style="color:#A020F0;">'editor native action'</span>
 <span style="color:#A020F0;">'selection-page-up'</span>       <span style="color:#A020F0;">'shift pressed PAGE_UP'</span>       <span style="color:#A020F0;">'editor native action'</span>
 <span style="color:#A020F0;">'selection-previous-word'</span>                   <span style="color: #080;">&#123;</span>2x1 <span style="color: #0000FF;">cell</span><span style="color: #080;">&#125;</span>  <span style="color:#A020F0;">'editor native action'</span>
 <span style="color:#A020F0;">'selection-up'</span>                              <span style="color: #080;">&#123;</span>2x1 <span style="color: #0000FF;">cell</span><span style="color: #080;">&#125;</span>  <span style="color:#A020F0;">'editor native action'</span>
 <span style="color:#A020F0;">'set-read-only'</span>                                     <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>  <span style="color:#A020F0;">'editor native action'</span>
 <span style="color:#A020F0;">'set-writable'</span>                                      <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>  <span style="color:#A020F0;">'editor native action'</span>
 <span style="color:#A020F0;">'shift-insert-break'</span>      <span style="color:#A020F0;">'shift pressed ENTER'</span>         <span style="color:#A020F0;">'editor native action'</span>
 <span style="color:#A020F0;">'shift-line-left'</span>         <span style="color:#A020F0;">'ctrl pressed OPEN_BRACKET'</span>   <span style="color:#A020F0;">'editor native action'</span>
 <span style="color:#A020F0;">'shift-line-right'</span>        <span style="color:#A020F0;">'ctrl pressed CLOSE_BRACKET'</span>  <span style="color:#A020F0;">'editor native action'</span>
 <span style="color:#A020F0;">'shift-tab-pressed'</span>       <span style="color:#A020F0;">'shift pressed TAB'</span>           <span style="color:#A020F0;">'editor native action'</span>
<span style="color: #F0F;">...</span><span style="color: #080;">&#91;</span>snip<span style="color: #080;">&#93;</span><span style="color: #F0F;">...</span>
 <span style="color:#A020F0;">'toggle-typing-mode'</span>      <span style="color:#A020F0;">'pressed INSERT'</span>              <span style="color:#A020F0;">'editor native action'</span>
 <span style="color:#A020F0;">'uncomment'</span>               <span style="color:#A020F0;">'ctrl pressed T'</span>              <span style="color:#A020F0;">'editor native action'</span>
 <span style="color:#A020F0;">'undo'</span>                    <span style="color:#A020F0;">'ctrl pressed Z'</span>              <span style="color:#A020F0;">'editor native action'</span>
 <span style="color:#A020F0;">'unselect'</span>                <span style="color:#A020F0;">'pressed ESCAPE'</span>              <span style="color:#A020F0;">'editor native action'</span>
 <span style="color:#A020F0;">'adjust-window-bottom'</span>                              <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>  <span style="color:#A020F0;">'cmdwin native action'</span>
 <span style="color:#A020F0;">'adjust-window-top'</span>                                 <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>  <span style="color:#A020F0;">'cmdwin native action'</span>
 <span style="color:#A020F0;">'beep'</span>                    <span style="color:#A020F0;">'ctrl pressed G'</span>              <span style="color:#A020F0;">'cmdwin native action'</span>
 <span style="color:#A020F0;">'break-interrupt'</span>         <span style="color:#A020F0;">'ctrl pressed CANCEL'</span>         <span style="color:#A020F0;">'cmdwin native action'</span>
<span style="color: #F0F;">...</span><span style="color: #080;">&#91;</span>snip<span style="color: #080;">&#93;</span><span style="color: #F0F;">...</span>
 <span style="color:#A020F0;">'toggle-typing-mode'</span>      <span style="color:#A020F0;">'pressed INSERT'</span>              <span style="color:#A020F0;">'cmdwin native action'</span>
 <span style="color:#A020F0;">'unselect'</span>                <span style="color:#A020F0;">'ctrl pressed BACK_SLASH'</span>     <span style="color:#A020F0;">'cmdwin native action'</span>
 <span style="color:#A020F0;">'new-mfile'</span>               <span style="color:#A020F0;">'ctrl pressed N'</span>              <span style="color:#A020F0;">'editor menu action'</span>  
 <span style="color:#A020F0;">'Figure'</span>                                            <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>  <span style="color:#A020F0;">'editor menu action'</span>  
<span style="color: #F0F;">...</span><span style="color: #080;">&#91;</span>snip<span style="color: #080;">&#93;</span><span style="color: #F0F;">...</span></pre></div></div><p>Even more interesting, apparently some 200 actions do not have any pre-assigned default key-bindings, such as &#8216;set-read-only&#8217; and &#8216;set-writable&#8217; in the snippet above. Let&#8217;s take the &#8216;match-brace&#8217; action for example. This sounded promising so I assigned it an unused key-binding and indeed found that it can be very useful: if your cursor is placed on a beginning or end of some code, clicking the assigned key-binding will jump the cursor to the other end, and then back again. This works nicely for (..), [..], for..end, try..end, if..end, etc.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #228B22;">% Ensure that &lt;Alt&gt;-M is unassigned</span>
&gt;&gt; bindings = EditorMacro<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'alt m'</span><span style="color: #080;">&#41;</span>
bindings = 
   Empty <span style="color: #0000FF;">cell</span> array<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>-by-<span style="color: #33f;">4</span>
&nbsp;
&gt;&gt; <span style="color: #228B22;">% Assign the key-binding and verify</span>
&gt;&gt; EditorMacro<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'alt m'</span>,<span style="color:#A020F0;">'match-brace'</span>,<span style="color:#A020F0;">'run'</span><span style="color: #080;">&#41;</span>;
&gt;&gt; bindings = EditorMacro<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'alt m'</span><span style="color: #080;">&#41;</span>
b = 
  <span style="color:#A020F0;">'alt pressed M'</span>  <span style="color:#A020F0;">'match-brace'</span>  <span style="color:#A020F0;">'run'</span>  <span style="color:#A020F0;">'editor native action'</span></pre></div></div><h3>Implementation details</h3><p>Interested readers of this post are encouraged to look within <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/24615">EditorMacro&#8217;s source code</a> and see how the native actions and keybindings were retrieved and modified for the editor, Command-Window and menus. In a nutshell, the native action names and key-bindings are stored in a Java Map object. Here&#8217;s a code snippet:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">%% Get all available actions even those without any key-binding</span>
<span style="color: #0000FF;">function</span> actionNames = getNativeActions<span style="color: #080;">&#40;</span>hEditorPane<span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">try</span>
    actionNames = <span style="color: #080;">&#123;</span><span style="color: #080;">&#125;</span>;
    actionKeys = hEditorPane.<span style="">getActionMap</span>.<span style="">allKeys</span>;
    actionNames = <span style="color: #0000FF;">cellfun</span><span style="color: #080;">&#40;</span>@<span style="color: #0000FF;">char</span>,<span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>actionKeys<span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'Uniform'</span>,<span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
    actionNames = <span style="color: #0000FF;">sort</span><span style="color: #080;">&#40;</span>actionNames<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>
&nbsp;
<span style="color: #228B22;">%% Get all active native shortcuts (key-bindings)</span>
<span style="color: #0000FF;">function</span> accelerators = getAccelerators<span style="color: #080;">&#40;</span>hEditorPane<span style="color: #080;">&#41;</span>
  <span style="color: #0000FF;">try</span>
    accelerators = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;
    inputMap = hEditorPane.<span style="">getInputMap</span>;
    inputKeys = inputMap.<span style="">allKeys</span>;
    accelerators = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>numel<span style="color: #080;">&#40;</span>inputKeys<span style="color: #080;">&#41;</span>,<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">for</span> ii = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> numel<span style="color: #080;">&#40;</span>inputKeys<span style="color: #080;">&#41;</span>
      thisKey = inputKeys<span style="color: #080;">&#40;</span>ii<span style="color: #080;">&#41;</span>;
      thisAction = inputMap.<span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>thisKey<span style="color: #080;">&#41;</span>;
      accelerators<span style="color: #080;">&#40;</span>ii,<span style="color: #F0F;">:</span><span style="color: #080;">&#41;</span> = <span style="color: #080;">&#123;</span><span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>thisKey<span style="color: #080;">&#41;</span>, <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>thisAction<span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span>;
    <span style="color: #0000FF;">end</span>
    accelerators = <span style="color: #0000FF;">sortrows</span><span style="color: #080;">&#40;</span>accelerators,<span style="color: #33f;">1</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></pre></div></div><p>The menu retrieval was more difficult: while it is possible to directly access the menubar reference (jMainPane.getRootPane.getMenuBar), the menu items themselves are not visible until their main menu item is clicked (displayed). The only way I know to access menu actions/keybindings is to read them from the individual menu items (if anyone knows a better way please tell me &#8211; perhaps some central key-listener repository?). Therefore, a simulation of the menu-click events is done and the menu hierarchy is traveled recuresively to collect all its actions and key-bindings.</p><p>A final note relates to the use of EDT. EDT really deserves a separate post, but in a nutshell it means that any action that affects the GUI needs to be invoked asynchronously (via the EDT) rather than synchronously (on the main Matlab thread). This is no real problem in the editor, but it is indeed an issue in the Command Window: If we do not use EDT there, we get ugly red stack-trace exceptions thrown on the Command Window whenever we run our <i><b>EditorMacro</b></i>-assigned macro. Here&#8217;s the code snippet that solves this:</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>
   <span style="color: #228B22;">%jEditorPane.insert(caretPosition, macro);  % better to use replaceSelection() than insert()</span>
   <span style="color: #0000FF;">try</span>
      <span style="color: #228B22;">% Try to dispatch on EDT</span>
      <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jEditorPane.<span style="">java</span>, <span style="color:#A020F0;">'replaceSelection'</span>, macro<span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">catch</span>
      <span style="color: #228B22;">% no good - try direct invocation</span>
      jEditorPane.<span style="">replaceSelection</span><span style="color: #080;">&#40;</span>macro<span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">catch</span>
   <span style="color: #228B22;">% Matlab 6</span>
   <span style="color: #228B22;">%jEditorPane.insert(macro, caretPosition);  % note the reverse order of input args vs. Matlab 7...</span>
   <span style="color: #0000FF;">try</span>
      <span style="color: #228B22;">% Try to dispatch on EDT</span>
      <span style="color: #0000FF;">awtinvoke</span><span style="color: #080;">&#40;</span>jEditorPane.<span style="">java</span>, <span style="color:#A020F0;">'replaceRange'</span>, macro, jEditorPane.<span style="">getSelStart</span>, jEditorPane.<span style="">getSelEnd</span><span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">catch</span>
      <span style="color: #228B22;">% no good - try direct invocation</span>
      jEditorPane.<span style="">replaceRange</span><span style="color: #080;">&#40;</span>macro, jEditorPane.<span style="">getSelStart</span>, jEditorPane.<span style="">getSelEnd</span><span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span></pre></div></div><h3>Known limitations</h3><p>Some limitations remain in <i><b>EditorMacro</b></i> &#8211; here are the major ones:</p><ul><li>Multi-key bindings are still not reported properly, nor can they be assigned. For example, the editor menu action &#8216;to-lower-case&#8217; has a pre-assigned default key-binding of &lt;Alt&gt;-&lt;U&gt;-&lt;L&gt;, but this is reported as unassigned. Of course, you can always add another (single-key) assignment for this action, for example: &lt;Alt&gt;-&lt;Ctrl&gt;-&lt;L&gt;</li><li>Menus are dynamically recreated whenever the Editor is docked/undocked, or a separate type of file is edited (e.g., switching from an m-file to a c-file). Similarly, whenever the active desktop window changes from the Command Window to another desktop-docked window (e.g., Command History). In all these cases, the dynamically recreated menus override any conflicting key-binding previously done with <i><b>EditorMacro</b></i>.</li><li>Unfortunately, my Matlab 6-installed computer crashed, so while the first version of <i><b>EditorMacro</b></i> works on ML6, the new version might well not. If you have ML6 available, please email me so I can post a working version with your help. This also means I won&#8217;t be able to post much about undocumented ML6 stuff in the future. Perhaps this is Fate&#8217;s way of telling me to progress with the times&#8230;</li><li>Key bindings are sometimes lost when switching between a one-document editor and a two-document one (i.e., adding/closing the second doc)</li><li>Key bindings are not saved between editor sessions</li><li>In split-pane mode, when inserting a text macro on the secondary (right/bottom) editor pane, then both panes (and the actual document) are updated but the secondary pane does not display the inserted macro (the primary pane looks ok).</li></ul><p>The first couple of limitations have a non-perfect workaround that Perttu came up with. He implemented this in his <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/25089">KeyBindings utility</a> on the Matlab File Exchange. Perhaps one day Perttu or me will find the time to merge these utilities into one.</p><p>Have you designed some crafty user-defined macro? or found some important unassigned built-in action? Please share your experience using <i><b>EditorMacro</b></i> in the comments section below.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/r2009b-keyboard-bindings/' rel='bookmark' title='R2009b keyboard bindings'>R2009b keyboard bindings</a> <small>The new Matlab release R2009b includes the ability to customize keyboard bindings for the editor and Command Window. However, there are still some uses for the EditorMacro utility and its...</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/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><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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/editormacro-v2-setting-command-window-key-bindings/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Non-textual editor actions</title><link>http://undocumentedmatlab.com/blog/non-textual-editor-actions/</link> <comments>http://undocumentedmatlab.com/blog/non-textual-editor-actions/#comments</comments> <pubDate>Fri, 17 Jul 2009 13:55:39 +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[keyboard]]></category> <category><![CDATA[UIInspect]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=454</guid> <description><![CDATA[The UIINSPECT utility can be used to expand EditorMacro capabilities to non-text-insertion actions. This is 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/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/setting-listbox-mouse-actions/' rel='bookmark' title='Setting listbox mouse actions'>Setting listbox mouse actions</a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Following my <a
href="http://undocumentedmatlab.com/blog/editormacro-assign-a-keyboard-macro-in-the-matlab-editor/" target="_blank">EditorMacro post</a> a couple of weeks ago, which showed how to assign a keyboard macro to the integrated Matlab Editor, several people have asked me whether it is possible to assign a macro to non-textual actions, in addition to the text insertion/replacement which <b><i>EditorMacro</i></b> supports.</p><p>The quick answer is yes, with some careful programming. Instead of specifying the end result, I will use this opportunity to illustrate how Java objects (not just the editor) can be inspected for their supported actions/properties.</p><p>Our first step is to get the requested Java reference handle. This can be done via the Matlab Command Window (interested readers can look at the EditorMacro.m source code, specifically at its getJEditor() function). However, a much easier way is to assign some macro using <b><i>EditorMacro</i></b> and then simply place a breakpoint in EditorMacro&#8217;s keyPressedCallback() callback function. Then press an arrow key (or any other key) in the editor, and wait for the breakpoint focus to arrive (don&#8217;t forget to clear the breakpoint&#8230;). From here on, all our actions will be done in the Command Window.</p><p>We now have a variable called jEditorPane, which is a reference to a Java object of type javahandle_withcallbacks. com.mathworks.mde.editor.EditorSyntaxTextPane (in Matlab 7 &#8211; it&#8217;s something similar in Matlab 6). This is a Matlab wrapper for the basic Java object, used for <a
href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/" target="_blank">accessing the callback hooks, as explained in a previous post.</a> In our case we are not interested in this wrapper but in its wrapped object, which is retrieved via Matlab&#8217;s built-in <b><i>java</i></b> function (java(jEditorPane) or jEditorPane.java). The inspection itself is done using Matlab&#8217;s standard tools (<b><i>inspect</i></b>, <b><i>methodsview</i></b> etc.) or via my <a
href="http://www.mathworks.com/matlabcentral/fileexchange/17935" target="_blank" rel="nofollow">UIINSPECT utility</a>. I suggest using <b><i>UIINSPECT</i></b>, which displays all the information of the standard tools and lots extra, but I&#8217;m of course biased&#8230;</p><blockquote><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">uiinspect<span style="color: #080;">&#40;</span>jEditorPane.<span style="">java</span><span style="color: #080;">&#41;</span>;</pre></div></div></blockquote><p><center><div
class="wp-caption aligncenter" style="width: 460px"><a
href="http://undocumentedmatlab.com/images/uiinspect_jeditor.png" target="_blank"><img
alt="jEditorPane inspection using UIINSPECT (click to see details)" src="http://undocumentedmatlab.com/images/uiinspect_jeditor.png" title="jEditorPane inspection using UIINSPECT (click to see details)" width="450" height="355" /></a><p
class="wp-caption-text">jEditorPane inspection using <b><i>UIINSPECT</i></b> (click to see details)</p></div></center></p><p>Without diving into all the <b><i>UIINSPECT</i></b> options (I shall do this in a dedicated post), we see the supported methods/actions on the left, and properties on the right. It is true that none of them are documented, but many are self-explanatory. For example, the <i>cut()/copy()/paste()</i> methods or the <i>caretPosition/caretColor</i> properties. Any combination of these methods and properties can be used in a user-defined macro.</p><p>Let&#8217;s do a simple example, setting the &lt;Ctrl-E&gt; combination to a macro moving to the end-of-line (unix-style &#8211; equivalent to &lt;End&gt; on Windows), and &lt;Ctrl-Shift-E&gt; to a similar macro doing the same while also selecting the text (like &lt;Shift-End&gt; on Windows). We shall even use the same macro code, by simply checking in the eventData whether the &lt;Shift&gt; key is depressed:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> EOL_Macro<span style="color: #080;">&#40;</span>hDocument,eventData<span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #228B22;">% Find the position of the next EOL mark</span>
  currentPos = hDocument.<span style="">getCaretPosition</span>;
  docLength = hDocument.<span style="">getLength</span>;
  textToEOF = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>hDocument.<span style="">getTextStartEnd</span><span style="color: #080;">&#40;</span>currentPos,docLength<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
  nextEOLPos = currentPos+<span style="color: #0000FF;">find</span><span style="color: #080;">&#40;</span>textToEOF&lt;=<span style="color: #33f;">13</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>-<span style="color: #33f;">1</span>;  <span style="color: #228B22;">% next CR/LF pos</span>
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>nextEOLPos<span style="color: #080;">&#41;</span>
      <span style="color: #228B22;">% no EOL found (=&gt; move to end-of-file)</span>
      nextEOLPos = docLength;
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #228B22;">% Do action based on whether &lt;Shift&gt; was pressed or not</span>
  <span style="color: #228B22;">%get(eventData);</span>
  <span style="color: #0000FF;">if</span> eventData.<span style="">isShiftDown</span>
      <span style="color: #228B22;">% Select to EOL</span>
      hDocument.<span style="">moveCaretPosition</span><span style="color: #080;">&#40;</span>nextEOLPos<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">else</span>
      <span style="color: #228B22;">% Move to EOL (without selection)</span>
      hDocument.<span style="">setCaretPosition</span><span style="color: #080;">&#40;</span>nextEOLPos<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% EOL_Macro</span></pre></div></div><p>&#8230;and now let&#8217;s activate this macro in the Matlab Command Window:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; macros = EditorMacro<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'ctrl-e'</span>,@EOL_Macro,<span style="color:#A020F0;">'run'</span><span style="color: #080;">&#41;</span>;
&gt;&gt; macros = EditorMacro<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'ctrl-shift-e'</span>,@EOL_Macro,<span style="color:#A020F0;">'run'</span><span style="color: #080;">&#41;</span>
macros = 
    <span style="color:#A020F0;">'ctrl alt pressed T'</span>      @<span style="color: #080;">&#40;</span>a,b<span style="color: #080;">&#41;</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;">'text'</span>
    <span style="color:#A020F0;">'ctrl pressed E'</span>          @EOL_Macro            <span style="color:#A020F0;">'run'</span> 
    <span style="color:#A020F0;">'shift ctrl pressed E'</span>    @EOL_Macro            <span style="color:#A020F0;">'run'</span></pre></div></div><p>Please do explore all the possible actions/properties exposed by the jEditorPane object. Probably the worst that could happen (and very rarely) is that you&#8217;ll crash Matlab and need to restart it &#8211; no biggy. If you find an interesting macro combination, please post it to the File Excahnge, and/or let us all know by placing a comment 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/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/setting-listbox-mouse-actions/' rel='bookmark' title='Setting listbox mouse actions'>Setting listbox mouse actions</a> <small>Matlab listbox uicontrol can be modified to detect mouse events for right-click context menus, dynamic tooltips etc....</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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/non-textual-editor-actions/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>EditorMacro &#8211; assign a keyboard macro in the Matlab editor</title><link>http://undocumentedmatlab.com/blog/editormacro-assign-a-keyboard-macro-in-the-matlab-editor/</link> <comments>http://undocumentedmatlab.com/blog/editormacro-assign-a-keyboard-macro-in-the-matlab-editor/#comments</comments> <pubDate>Wed, 01 Jul 2009 19:17:56 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Desktop]]></category> <category><![CDATA[Hidden property]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[UI controls]]></category> <category><![CDATA[Editor]]></category> <category><![CDATA[keyboard]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=437</guid> <description><![CDATA[EditorMacro is a new utility that enables setting keyboard macros in the Matlab editor. this post details its inner workings.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/r2009b-keyboard-bindings/' rel='bookmark' title='R2009b keyboard bindings'>R2009b keyboard bindings</a> <small>The new Matlab release R2009b includes the ability to customize keyboard bindings for the editor and Command Window. However, there are still some uses for the EditorMacro utility and its...</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/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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Over the past years there have been quite a few requests to enable keyboard macros and keybinding modifications in the Matlab editor. Some posters have even noted this lack as their main reason to use an external editor. Coincidentally, some weeks ago I was approached by a reader (Grant Roch) to help with assigning some textual editor macros, and with joint work we were able to figure out a basic working mechanism. The <a
rel="nofollow" target="_blank" href="http://blogs.mathworks.com/desktop/2009/06/29/what-does-your-matlab-desktop-look-like-again/#comment-6317">latest user comment</a> on this issue was posted on the official Matlab Desktop blog yesterday. All this prompted me to finally post a solution to this need: My <a
rel="nofollow" target="_blank" href="http://www.mathworks.com/matlabcentral/fileexchange/24615">EditorMacro utility is now available for download on the Mathworks File Exchange</a>. In this post I will detail some of its inner workings, which rely on undocumented Matlab features.</p><p>In a nutshell, <em><strong>EditorMacro</strong></em> sets the KeyPressedCallback property (<a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">explained in a previous post</a>) for each of the editor&#8217;s document panes, to an internal function. This internal function then checks each keystroke against a list of registered keybindings. The list itself is persisted in the editor object&#8217;s hidden ApplicationData property (accessible via the <em><strong>getappdata</strong></em>/<em><strong>setappdata</strong></em> built-in functions). If a match is found, then the associated macro is invoked. Depending on the macro type, some text can be inserted at the current editor caret position, or replacing the selected editor text, or a non-text Matlab function/command can be invoked.</p><p>This enables <em><strong>EditorMacro</strong></em> to be used for quickly inserting code templates (header comments, try-catch blocks etc.) or for automating Matlab unit testing.</p><p>Here&#8217;s a typical usage example: start by defining a simple function that returns a dynamic header comment:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> comment = createHeaderComment<span style="color: #080;">&#40;</span>hDocument, eventData<span style="color: #080;">&#41;</span>
  timestamp = <span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">now</span><span style="color: #080;">&#41;</span>;
  username = getenv<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'username'</span><span style="color: #080;">&#41;</span>;
  <span style="color: #228B22;">%computer = getenv('computername');  % unused</span>
  lineStr = <span style="color: #0000FF;">repmat</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'%'</span>,<span style="color: #33f;">1</span>,<span style="color: #33f;">35</span><span style="color: #080;">&#41;</span>;
  comment = <span style="color: #0000FF;">sprintf</span><span style="color: #080;">&#40;</span><span style="color: #F0F;">...</span>
      <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'%s\n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'%% \n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'%% Name:    functionName\n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'%% \n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'%% Desc:    enter description here\n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'%% \n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'%% Inputs:  enter inputs here\n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'%% \n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'%% Outputs: enter outputs here\n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'%% \n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'%% Created: %s\n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'%% \n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'%% Author:  %s\n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'%% \n'</span> <span style="color: #F0F;">...</span>
       <span style="color:#A020F0;">'%s\n'</span><span style="color: #080;">&#93;</span>, <span style="color: #F0F;">...</span>
       <span style="">lineStr</span>, timestamp, username, lineStr<span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% createHeaderComment</span></pre></div></div><p>Now define a macro to use this function, and another simple try-catch template macro:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; EditorMacro<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Alt-Control-h'</span>, @createHeaderComment<span style="color: #080;">&#41;</span>;
&gt;&gt; macroStr = <span style="color:#A020F0;">'try\n  % Main code here\ncatch\n  % Exception handling here\nend'</span>;
&gt;&gt; b = EditorMacro<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Ctrl alt T'</span>, macroStr<span style="color: #080;">&#41;</span>
b = 
    <span style="color:#A020F0;">'ctrl alt pressed H'</span>    @createHeaderComment    <span style="color:#A020F0;">'text'</span>
    <span style="color:#A020F0;">'ctrl alt pressed T'</span>             <span style="color: #080;">&#91;</span>1x60 <span style="color: #0000FF;">char</span><span style="color: #080;">&#93;</span>    <span style="color:#A020F0;">'text'</span></pre></div></div><p>Now start with a blank document and click &lt;Ctrl&gt;-&lt;Alt&gt;-H and &lt;Ctrl&gt;-&lt;Alt&gt;-T. This is the end result:</p><blockquote><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
<span style="color: #228B22;">% </span>
<span style="color: #228B22;">% Name:    functionName</span>
<span style="color: #228B22;">% </span>
<span style="color: #228B22;">% Desc:    enter description here</span>
<span style="color: #228B22;">% </span>
<span style="color: #228B22;">% Inputs:  enter inputs here</span>
<span style="color: #228B22;">% </span>
<span style="color: #228B22;">% Outputs: enter outputs here</span>
<span style="color: #228B22;">% </span>
<span style="color: #228B22;">% Created: 01-Jul-2009 23:31:46</span>
<span style="color: #228B22;">% </span>
<span style="color: #228B22;">% Author:  Yair Altman</span>
<span style="color: #228B22;">% </span>
<span style="color: #228B22;">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
<span style="color: #0000FF;">try</span>
  <span style="color: #228B22;">% Main code here</span>
<span style="color: #0000FF;">catch</span>
  <span style="color: #228B22;">% Exception handling here</span>
<span style="color: #0000FF;">end</span></pre></div></div></blockquote><p>Keybindings are normalized using Java&#8217;s built-in javax.swing.KeyStroke.getKeyStroke() method, to enable the user a very wide range of keystroke naming formats (e.g., &#8216;Alt-Control-T&#8217; or &#8216;ctrl alt t&#8217;).</p><p>I have taken great pains to make <em><strong>EditorMacro</strong></em> compatible with all Matlab versions since 6.0 (R12). This was no easy feat: Matlab 7 made some significant changes to the editor layout. Luckily, once I found out how to get a handle to the Matlab 6 editor object (this took some hours of trial-and-error), listing its display hierarchy was simple and the modifications were generally straight-forward, although non-trivial (different quirks due to missing default type-castings, missing eventData in invoked callbacks etc&#8230;). Please feel free to look at <em><strong>EditorMacro</strong></em>&#8216;s source code for the clearly-marked Matlab 6 segments.</p><p>For the record, here is a snippet showing how to get the editor object in Matlab 6 and 7. Note that the desktop handle is only useful to get the editor handle in Matlab 6 &#8211; we need a totally different way in Matlab 6:</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>
   jDesktop = com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLDesktop</span>.<span style="">getInstance</span>;
   jEditor = jDesktop.<span style="">getGroupContainer</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Editor'</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">catch</span>
   <span style="color: #228B22;">% Matlab 6</span>
   <span style="color: #0000FF;">try</span>
      <span style="color: #228B22;">%desktop = com.mathworks.ide.desktop.MLDesktop.getMLDesktop;</span>
      <span style="color: #228B22;">% no use: can't get to the editor from the desktop handle in 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>;
      firstDoc = openDocs.<span style="">elementAt</span><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
      jEditor = firstDoc.<span style="">getParent</span>.<span style="">getParent</span>.<span style="">getParent</span>;
   <span style="color: #0000FF;">catch</span>
      <span style="color: #0000FF;">error</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Cannot find Matlab editor handle: possibly no open documents'</span><span style="color: #080;">&#41;</span>;
   <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span></pre></div></div><p>Another complication arose due to the different layout used for floating/maximized/tiled document layout in the editor. Yet another was due to the different behavior (at least on Matlab 6) between a one-document and a multi-document editor view.</p><p>Due to the way keyboard events are processed by the Matlab editor, KeyPressedCallback needs to be set separately for all the open document panes and split-panes. Since we also wish newly-opened documents to recognize the macro bindings, we need to set the common container ancestor&#8217;s ComponentAddedCallback to an internal function that will handle the KeyPressedCallback instrumentation for each newly-opened document. Again, this needs to be done somewhat differently for Matlab 6/7.</p><p>Note that <em><strong>EditorMacro</strong></em> relies on the editor&#8217;s internal display layout, which is very sensitive to modification between Matlab releases (as it has between Matlab 6 and 7, for example).</p><p>All-in-all, I believe <em><strong>EditorMacro</strong></em> provides a useful and long-awaited service, which should in fact be part of the built-in editor.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/r2009b-keyboard-bindings/' rel='bookmark' title='R2009b keyboard bindings'>R2009b keyboard bindings</a> <small>The new Matlab release R2009b includes the ability to customize keyboard bindings for the editor and Command Window. However, there are still some uses for the EditorMacro utility and its...</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/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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/editormacro-assign-a-keyboard-macro-in-the-matlab-editor/feed/</wfw:commentRss> <slash:comments>12</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           tag
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-02-04 03:02:15 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/tag/editor/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      1.278s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1328349734; expires=Sun, 03-Feb-2013 10:02:14 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Sat, 04 Feb 2012 10:02:15 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Sat, 04 Feb 2012 11:02:15 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               a56a66c280a0096374b53b28509f2df0
Content-Encoding:   gzip
-->
