<?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>Oleg Komarov &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/oleg-komarov/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Wed, 11 Sep 2013 10:35:27 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>Variables Editor scrolling</title>
		<link>https://undocumentedmatlab.com/articles/variables-editor-scrolling?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=variables-editor-scrolling</link>
					<comments>https://undocumentedmatlab.com/articles/variables-editor-scrolling#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 11 Sep 2013 10:35:27 +0000</pubDate>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[FindJObj]]></category>
		<category><![CDATA[Internal component]]></category>
		<category><![CDATA[Oleg Komarov]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4161</guid>

					<description><![CDATA[<p>The Matlab Variables Editor can be accessed to provide immediate scrolling to a specified cell location. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/variables-editor-scrolling">Variables Editor scrolling</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/spicing-up-the-matlab-editor" rel="bookmark" title="Spicing up the Matlab Editor">Spicing up the Matlab Editor </a> <small>Matlab's Editor and Workspace can be enhanced quite significantly using an open-source utility. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/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>
</div>
]]></description>
										<content:encoded><![CDATA[<p><i>I would like to introduce guest blogger <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/authors/61651">Oleg Komarov</a>. Oleg has gathered a reputation as a Matlab veteran with plenty of insight into the arcane corners of Matlab, originally on the <a target="_blank" rel="nofollow" href="https://www.mathworks.com/matlabcentral/newsreader/author/123159">CSSM newsgroup</a>, and more recently on <a target="_blank" rel="nofollow" href="http://stackoverflow.com/users/2180721/oleg-komarov">StackExchange</a> (which admittedly contains more Q&#038;As of the challenging type). Today Oleg discusses a non-trivial solution to a deceivingly-simple problem.</i></p>
<h3 id="Introduction">Introduction</h3>
<p>I often work with datasets that in one way or another have millions of rows and several columns. Although the dimensions preclude visual-based approaches, e.g. imagine keeping all data on a spreadsheet, especially during the development and testing phases, I need to inspect the dataset at given &#8216;coordinates&#8217;.<br />
As a concrete example, in my work on financial time-series I sometimes encounter a subtle irregularity that might arise from a bug in the code when you stack different series of prices. Then you calculate returns and wonder why you get -99% or +2000%.<br />
Thankfully, the <a target="_blank" rel="nofollow" href="http://blogs.mathworks.com/community/2008/04/21/variable-editor/">Variables Editor</a> (VE) is very much what I would need for such inspection tasks, if it wasn&#8217;t that its usefulness is inversely proportional to the size of the data. An example would better clarify what I mean. Suppose you need to scroll to position 5677545 of your 1e8-by-1 data variable, the following screenshot illustrates how the scrollbar, pgdown/pgup or the arrow keys are not fine-tuned for such task.<br />
<span id="more-4161"></span><br />
<center><figure style="width: 376px" class="wp-caption aligncenter"><img fetchpriority="high" decoding="async" alt="Problematic Variables Editor scrolling (and the scrollto solution)" src="https://undocumentedmatlab.com/images/scrollto_animated.gif" title="Problematic Variables Editor scrolling (and the scrollto solution)" width="376" height="500" /><figcaption class="wp-caption-text">Problematic Variables Editor scrolling (and the scrollto solution)</figcaption></figure></center><br />
You can easily waste minutes to mindless scrolling!<br />
We could argue that the task is painlessly accomplished through basic logical indexing in the Command Window (CW), as the following image shows (note that I already set the display to <i><b>format compact</b></i>):<br />
<center><figure style="width: 450px" class="wp-caption aligncenter"><img decoding="async" alt="Sub-indexing into a large data matrix" src="https://undocumentedmatlab.com/images/scrollto_2.jpg" title="Sub-indexing into a large data matrix" width="450" height="373" /><figcaption class="wp-caption-text">Sub-indexing into a large data matrix</figcaption></figure></center><br />
However, this approach is limited because:</p>
<ol>
<li>it displays a static snapshot of the selected region, and often precludes interaction with the data, e.g. copy-paste</li>
<li>it requires more visual space than the VE, which either means that you need to modify your layout or scroll the CW</li>
<li>typing in the CW can be even more tedious than scrolling the VE&#8230;</li>
</ol>
<h3 id="openvar">Why <i>openvar</i> is not a good-enough solution</h3>
<p>In search of a solution that would retain the VE interactivity and would speed up the task of scrolling to the point of interest, I first checked if there was an API for the VE similar to that for the documents Editor. As a side-note: to date, the Editor&#8217;s API remains undocumented/unsupported, although having been <a target="_blank" rel="nofollow" href="http://blogs.mathworks.com/community/2011/05/09/r2011a-matlab-editor-api/">published</a> in what was then called the Desktop Blog (renamed <i>MATLAB Spoken Here</i> since <a target="_blank" href="/articles/customizing-menu-items-part-3/#ken_mike">Ken and Mike have left MathWorks</a> and the blog focus has changed).<br />
In any case, it turns out that there is a stub of an API for the Variables Editor, <i><b>matlab.desktop.vareditor</b></i>, which however comes much short of my expectations. In fact, it only goes as far as visualizing the data of some variable within a completely separate basic version of the VE. This could prove to be a valid embeddable alternative to the <i><b>uitable</b></i>, but it does not help us with the need to scroll to a specific data cell.<br />
Unlucky with the VE API, I looked into <i><b>openvar</b></i> for an undocumented option that would let me open some variable in the VE and then scroll to the desired &#8216;coordinates&#8217; &#8211; this effort proved fruitless.</p>
<h3 id="solution">A description of the solution that <i>did</i> work</h3>
<p>Since the standard Matlab did not offer a programmatic solution, I started digging into the underlying Java components.<br />
I found that one simple direct way is to open our variable of interest in the VE with <i><b>openvar</b></i>, retrieve the Java <code>Desktop</code> instance (in the same manner as we do to get the Desktop instance to <a target="_blank" href="/articles/accessing-the-matlab-editor/">customize the Editor</a>). From this instance we can find the handle to the appropriate <code>com.mathworks.mlwidgets.array.ArrayTable</code> with <a target="_blank" href="/articles/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i></a> (remember that there could be multiple ArrayTables, one for each inspected variable). Finally, we scroll to the desired position using the <i>scrollCellToVisible()</i> method.<br />
Note that the Matlab object builds on the <a target="_blank" href="/articles/customizing-listbox-editbox-scrollbars/"><code>javax.swing.JViewport</code></a> and provides a convenient interface as in the case of the scrolling function, since its java counterpart <i>scrollRectToVisible()</i> requires as input a <code>java.awt.rectangle</code> object rather than scalar doubles.<br />
A succinct usage example would be:</p>
<pre lang='matlab'>
% Create example variable
a = randn(1e8,1);
name = 'a';
% Open 'a' in the VE or grab focus
openvar(name)
% Retrieve Desktop instance and handle to client
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
varclient = desktop.getClient(name);
% Retrieve handle to scrollable table with findjobj
jVarTable = findjobj(varclient,'property',{'name','VariableTable'});
% Select, scroll and update the UI (note the zero-indexing, i.e. row-1)
row = 5677545;
col = 1;
jVarTable.setRowSelectionInterval(row-1, row-1)
jVarTable.setColumnSelectionInterval(col-1, col-1)
jVarTable.scrollCellToVisible(row-1, col-1)
jVarTable.updateUI
</pre>
<h3 id="FEX">The <i>scrollto</i> utility</h3>
<p>As most of the lines of the usage example above should already be familiar and/or self-explanatory to the followers of this blog, the next logical step is to encapsulate the snippet into a function which you can already find on the File Exchange: <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/42795-programmatically-scroll-variables-editor"><i><b>scrollto</b></i></a>.<br />
The <i><b>scrollto</b></i> function provides the following features:</p>
<ul>
<li>Accepts subs or indices</li>
<li>You can scroll indexed variables, i.e. &#8216;a{1}&#8217; or &#8216;a.field{2,3}&#8217; etc.</li>
<li>Scrolling is also supported in debug mode (from ver2.00), i.e. on variables of the &#8216;caller&#8217; workspace</li>
<li>Handles asynchronous rendering of the VE (see below)</li>
</ul>
<p>and supports the following classes (should be 2D arrays):</p>
<ul>
<li>Numeric and logical data – &#8216;VariableTable&#8217;</li>
<li>Cell arrays – &#8216;CellTable&#8217;</li>
<li>Timeseries – &#8216;TimeSeriesArrayEditorTablePanel:fDataTable&#8217;</li>
<li>Datasets (Statistics Toolbox) &#8211; &#8216;DatasetVariableTable&#8217;</li>
</ul>
<p>Matlab handles different classes in the VE through different interfaces. For this reason, for each supported class I reported the &#8216;name&#8217; property to use with <i><b>findjobj</b></i>.</p>
<h3 id="synchronization">Synchronization issues</h3>
<p>Asynchronous rendering of the VE can happen in either of two cases:</p>
<ol>
<li>if the variable has never been opened in the VE, or the variable was opened but it does not exist in the workspace anymore</li>
<li>in code that opens and interacts with the VE</li>
</ol>
<p>Writing <i><b>scrollto</b></i> proved to be more than a simple wrapping effort and it is worth mentioning the workaround implemented to allow a smooth workflow. The biggest issue I faced is the asynchronous rendering of the VE. As Yair reports in his <a target="_blank" href="/matlab-java-book/">book</a> Undocumented Secrets of Matlab-Java Programming, p. 538:</p>
<blockquote><p>&#8220;The tight-coupling of the Variable Editor to the Desktop Workspace variables is unfortunate in some respects. &#8230; Matlab only has a single computational thread, so Matlab code has to finish before the JMI request can be handled. This means that the Variables Editor contents cannot be displayed synchronously by the Matlab code that invokes it.&#8221;
</p></blockquote>
<p>In other words, we cannot retrieve the handle to e.g. the <code>VariableTable</code> until the function has finished executing.<br />
A workaround is to call <i><b>openvar</b></i>, wait until the focus is back to the CW, and then call <i><b>scrollto</b></i>. I cannot tell you how this workflow made me feel so close and yet so far from a satisfactory implementation.<br />
The ideal flowchart of a basic wrapper around the example (see above) would have been:<br />
<center><figure style="width: 450px" class="wp-caption aligncenter"><img decoding="async" alt="Ideal scrollto workflow" src="https://undocumentedmatlab.com/images/scrollto_3.jpg" title="Ideal scrollto workflow" width="450" height="52" /><figcaption class="wp-caption-text">Ideal scrollto workflow</figcaption></figure></center><br />
Now, since we cannot retrieve the handle to the <code>VariableTable</code> if the VE has not rendered yet, I implemented an asynchronous second call to <i><b>scrollto</b></i> through a timer object that fires after the first call to <i><b>scrollto</b></i> has finished executing:<br />
<center><figure style="width: 450px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Asynchronous scrollto workflow" src="https://undocumentedmatlab.com/images/scrollto_4.jpg" title="Asynchronous scrollto workflow" width="450" height="236" /><figcaption class="wp-caption-text">Asynchronous scrollto workflow</figcaption></figure></center><br />
The result is a better experience without unexpected interruptions or breaks in the workflow.<br />
I would like to thank Yair for his comments and guidance. Without his support, <i><b>scrollto</b></i> would not exist and humankind would be doomed to scroll the VE manually!</p>
<h3 id="announcement">Public-service announcement</h3>
<p><i>(This is Yair again): </p>
<p />I wish to invite you to join my online presentation (webinar) on &#8220;A Real-Time Trading System in MATLAB&#8221; in the upcoming <a target="_blank" rel="nofollow" href="http://engage.vevent.com/index.jsp?eid=1972&#038;seid=900">MATLAB Computational Finance Virtual Conference</a> next Thursday, September 19, 2013. I will be speaking at 2pm EST (8pm CEST). <a target="_blank" rel="nofollow" href="http://engage.vevent.com/registr_form.jsp?eid=1972&#038;seid=900&#038;language-code=en&#038;country-code=US&#038;page=1&#038;no-login=false">Registration</a> is free and it&#8217;s a virtual conference, so there&#8217;s no need for a tie and jacket&#8230; Following a half-hour presentation, I will be answering audience questions online. </p>
<p /> I gave an <a target="_blank" href="/articles/real-time-trading-system-demo/">earlier version of this presentation</a> at the Computational Finance Conference in New York on May 23, and you are welcome to look there for a preview. The presentation slides can be <a target="_blank" href="/files/Matlab%20real-time%20trading%20presentation.pdf">downloaded here</a>. Even if you&#8217;re not interested in real-time financial trading with Matlab, you might find it interesting to see the neat things that Matlab can do using a Java API interface and a few undocumented GUI tricks.</i><i><br />
<center><a target="_blank" rel="nofollow" href="http://engage.vevent.com/index.jsp?eid=1972&#038;seid=900"><img loading="lazy" decoding="async" alt="MATLAB Computational Finance Conference 2013" src="https://undocumentedmatlab.com/images/MATLAB_Computational_Finance_Conference_2013.jpg" title="MATLAB Computational Finance Conference 2013" width="600" height="112" /></a></center><br />
</i></p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/variables-editor-scrolling">Variables Editor scrolling</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/spicing-up-the-matlab-editor" rel="bookmark" title="Spicing up the Matlab Editor">Spicing up the Matlab Editor </a> <small>Matlab's Editor and Workspace can be enhanced quite significantly using an open-source utility. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/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>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/variables-editor-scrolling/feed</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
	</channel>
</rss>
