<?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>Toolbar &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/toolbar/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Thu, 07 Nov 2019 13:43:36 +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>Improving graphics interactivity</title>
		<link>https://undocumentedmatlab.com/articles/improving-graphics-interactivity?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=improving-graphics-interactivity</link>
					<comments>https://undocumentedmatlab.com/articles/improving-graphics-interactivity#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Sun, 21 Apr 2019 21:03:10 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Handle graphics]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<category><![CDATA[Toolbar]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8723</guid>

					<description><![CDATA[<p>Matlab R2018b added default axes mouse interactivity at the expense of performance. Luckily, we can speed-up the default axes. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/improving-graphics-interactivity">Improving graphics interactivity</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/undocumented-hg2-graphics-events" rel="bookmark" title="Undocumented HG2 graphics events">Undocumented HG2 graphics events </a> <small>Matlab's new HG2 graphics engine includes many new undocumented events that could be used in various ways. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/handle-graphics-behavior" rel="bookmark" title="Handle Graphics Behavior">Handle Graphics Behavior </a> <small>HG behaviors are an important aspect of Matlab graphics that enable custom control of handle functionality. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/improving-save-performance" rel="bookmark" title="Improving save performance">Improving save performance </a> <small>There are many different ways of improving Matlab's standard save function performance. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/waterloo-graphics" rel="bookmark" title="Waterloo graphics">Waterloo graphics </a> <small>Waterloo is an open-source library that can significantly improve Matlab GUI. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Matlab release R2018b added the concept of <a href="https://undocumentedmatlab.com/articles/reverting-axes-controls-in-figure-toolbar" target="_blank">axes-specific toolbars</a> and default <a href="https://www.mathworks.com/help/matlab/creating_plots/control-axes-interactions.html" rel="nofollow" target="_blank">axes mouse interactivity</a>. <span class="alignright"><img decoding="async" src="https://undocumentedmatlab.com/images/speedometer4d_200x200.gif" alt="Accelerating MATLAB Performance" title="Accelerating MATLAB Performance" width="200" height="200" /></span> Plain 2D plot axes have the following default interactions enabled by default: <a href="https://www.mathworks.com/help/matlab/ref/matlab.graphics.interaction.interactions.paninteraction.html" rel="nofollow" target="_blank">PanInteraction</a>, <a href="https://www.mathworks.com/help/matlab/ref/matlab.graphics.interaction.interactions.zoominteraction.html" rel="nofollow" target="_blank">ZoomInteraction</a>, <a href="https://www.mathworks.com/help/matlab/ref/matlab.graphics.interaction.interactions.datatipinteraction.html" rel="nofollow" target="_blank">DataTipInteraction</a> and <a href="https://www.mathworks.com/help/matlab/ref/matlab.graphics.interaction.interactions.rulerpaninteraction.html" rel="nofollow" target="_blank">RulerPanInteraction</a>.</p>
<p>Unfortunately, I find that while the default interactions set is much more useful than the non-interactive default axes behavior in R2018a and earlier, it could still be improved in two important ways:</p>
<ol>
<li><b>Performance</b> &#8211; Matlab&#8217;s builtin Interaction objects are very inefficient. In cases of multiple overlapping axes (which is very common in multi-tab GUIs or cases of various types of axes), instead of processing events for just the top visible axes, they process all the enabled interactions for *all* axes (including non-visible ones!). This is particularly problematic with the default DataTipInteraction &#8211; it includes a <code>Linger</code> object whose apparent purpose is to detect when the mouse lingers for enough time on top of a chart object, and displays a data-tip in such cases. Its internal code is both inefficient and processed multiple times (for each of the axes), as can be seen via a profiling session.</li>
<li><b>Usability</b> &#8211; In my experience, <a href="https://www.mathworks.com/help/matlab/ref/matlab.graphics.interaction.interactions.regionzoominteraction.html" rel="nofollow" target="_blank">RegionZoomInteraction</a> (which enables defining a region zoom-box via click-&#038;-drag) is usually much more useful than PanInteraction for most plot types. ZoomInteraction, which is enabled by default only enables zooming-in and -out using the mouse-wheel, which is much less useful and more cumbersome to use than RegionZoomInteraction. The panning functionality can still be accessed interactively with the mouse by dragging the X and Y rulers (ticks) to each side.</li>
</ol>
<p>For these reasons, I typically use the following function whenever I create new axes, to replace the default sluggish DataTipInteraction and PanInteraction with RegionZoomInteraction:<br />
<span id="more-10306"></span></p>
<pre lang="matlab">
function axDefaultCreateFcn(hAxes, ~)
    try
        hAxes.Interactions = [zoomInteraction regionZoomInteraction rulerPanInteraction];
        hAxes.Toolbar = [];
    catch
        % ignore - old Matlab release
    end
end
</pre>
<p>The purpose of these two axes property changes shall become apparent below.<br />
This function can either be called directly (<code>axDefaultCreateFcn(hAxes</code>), or as part of the containing figure&#8217;s creation script to ensure than any axes created in this figure has this fix applied:</p>
<pre lang="matlab">
set(hFig,'defaultAxesCreateFcn',@axDefaultCreateFcn);
</pre>
<h3 id="testing">Test setup</h3>
<p><figure style="width: 268px" class="wp-caption alignright"><img fetchpriority="high" decoding="async" src="https://undocumentedmatlab.com/images/Axes_Interactivity.png" alt="Figure with default axes toolbar and interactivity" title="Figure with default axes toolbar and interactivity" width="268" height="200" /><figcaption class="wp-caption-text">Figure with default axes toolbar and interactivity</figcaption></figure> To test the changes, let&#8217;s prepare a figure with 10 tabs, with 10 overlapping panels and a single axes in each tab:</p>
<pre lang="matlab">
hFig = figure('Pos',[10,10,400,300]);
hTabGroup = uitabgroup(hFig);
for iTab = 1 : 10
    hTab = uitab(hTabGroup, 'title',num2str(iTab));
    hPanel = uipanel(hTab);
    for iPanel = 1 : 10
        hPanel = uipanel(hPanel);
    end
    hAxes(iTab) = axes(hPanel); %see MLint note below
    plot(hAxes(iTab),1:5,'-ob');
end
drawnow
</pre>
<p style="padding-left: 20px; margin-top: -15px;"><i><font size=-1>p.s. &#8211; there&#8217;s a incorrect MLint (Code Analyzer) warning in line 9 about the call to axes(hPanel) being inefficient in a loop. Apparently, MLint incorrectly parses this function call as a request to make the axes in-focus, rather than as a request to create the axes in the specified hPanel parent container. We can safely ignore this warning.</font></i></p>
<p>Now let&#8217;s create a run-time test script that simulates 2000 mouse movements using <a href="https://undocumentedmatlab.com/articles/gui-automation-robot" target="_blank">java.awt.Robot</a>:</p>
<pre lang="matlab">
tic
monitorPos = get(0,'MonitorPositions');
y0 = monitorPos(1,4) - 200;
robot = java.awt.Robot;
for iEvent = 1 : 2000
    robot.mouseMove(150, y0+mod(iEvent,100));
    drawnow
end
toc
</pre>
<p>This takes ~45 seconds to run on my laptop: ~23ms per mouse movement on average, with noticeable &#8220;linger&#8221; when the mouse pointer is near the plotted data line. Note that this figure is extremely simplistic &#8211; In a real-life program, the mouse events processing lag the mouse movements, making the GUI far more sluggish than the same GUI on R2018a or earlier. In fact, in one of my more complex GUIs, the entire GUI and Matlab itself came to a standstill that required killing the Matlab process, just by moving the mouse for several seconds.</p>
<p>Notice that at any time, only a single axes is actually visible in our test setup. The other 9 axes are not visible although their <b>Visible</b> property is <code>'on'</code>. Despite this, when the mouse moves within the figure, these other axes unnecessarily process the mouse events.</p>
<h3 id="interactions">Changing the default interactions</h3>
<p>Let&#8217;s modify the axes creation script as I mentioned above, by changing the default interactions (note the highlighted code addition):</p>
<pre lang="matlab" highlight="11">
hFig = figure('Pos',[10,10,400,300]);
hTabGroup = uitabgroup(hFig);
for iTab = 1 : 10
    hTab = uitab(hTabGroup, 'title',num2str(iTab));
    hPanel = uipanel(hTab);
    for iPanel = 1 : 10
        hPanel = uipanel(hPanel);
    end
    hAxes(iTab) = axes(hPanel);
    plot(hAxes(iTab),1:5,'-ob');
    hAxes(iTab).Interactions = [zoomInteraction regionZoomInteraction rulerPanInteraction];
end
drawnow
</pre>
<p>The test script now takes only 12 seconds to run &#8211; 4x faster than the default and yet IMHO with better interactivity (using RegionZoomInteraction).</p>
<h3 id="Toolbar">Effects of the axes toolbar</h3>
<p>The axes-specific toolbar, another innovation of R2018b, does not just have interactivity aspects, which are by themselves <a href="https://undocumentedmatlab.com/articles/reverting-axes-controls-in-figure-toolbar" target="_blank">much-contested</a>. A much less discussed aspect of the axes toolbar is that it degrades the overall performance of axes. The reason is that the axes toolbar&#8217;s transparency, visibility, background color and contents continuously update whenever the mouse moves within the axes area.</p>
<p>Since we have set up the default interactivity to a more-usable set above, and since we can replace the axes toolbar with figure-level toolbar controls, we can simply delete the axes-level toolbars for even more-improved performance:</p>
<pre lang="matlab" highlight="12">
hFig = figure('Pos',[10,10,400,300]);
hTabGroup = uitabgroup(hFig);
for iTab = 1 : 10
    hTab = uitab(hTabGroup, 'title',num2str(iTab));
    hPanel = uipanel(hTab);
    for iPanel = 1 : 10
        hPanel = uipanel(hPanel);
    end
    hAxes(iTab) = axes(hPanel);
    plot(hAxes(iTab),1:5,'-ob');
    hAxes(iTab).Interactions = [zoomInteraction regionZoomInteraction rulerPanInteraction];
    hAxes(iTab).Toolbar = [];
end
drawnow
</pre>
<p>This brings the test script&#8217;s run-time down to 6 seconds &#8211; <b>7x faster than the default run-time</b>. At ~3ms per mouse event, the GUI is now as performant and snippy as in R2018a, even with the new interactive mouse actions of R2018b active.</p>
<h3 id="Conclusions">Conclusions</h3>
<p>MathWorks definitely did not intend for this slow-down aspect, but it is an unfortunate by-product of the choice to auto-enable DataTipInteraction and of its sub-optimal implementation. Perhaps this side-effect was never noticed by MathWorks because the testing scripts probably had only a few axes in a very simple figure &#8211; in such a case the performance lags are very small and might have slipped under the radar. But I assume that many real-life complex GUIs will display significant lags in R2018b and newer Matlab releases, compared to R2018a and earlier releases. I assume that such users will be surprised/dismayed to discover that in R2018b their GUI not only interacts differently but also runs slower, although the program code has not changed.</p>
<p>One of the common claims that I often hear against using undocumented Matlab features is that the program might break in some future Matlab release that would not support some of these features. But users certainly do not expect that their programs might break in new Matlab releases when they only use documented features, as in this case. IMHO, this case (and others over the years) demonstrates that using undocumented features is usually not much riskier than using the standard documented features with regards to future compatibility, making the risk/reward ratio more favorable. In fact, of the ~400 posts that I have published in the past decade (this blog is already 10 years old, time flies&#8230;), very few tips no longer work in the latest Matlab release. When such forward compatibility issues do arise, whether with fully-documented or undocumented features, we can often find workarounds as I have shown above.</p>
<p>If your Matlab program could use a performance boost, I would be happy to assist making your program faster and more responsive. Don&#8217;t hesitate to reach out to me for a consulting quote.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/improving-graphics-interactivity">Improving graphics interactivity</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/undocumented-hg2-graphics-events" rel="bookmark" title="Undocumented HG2 graphics events">Undocumented HG2 graphics events </a> <small>Matlab's new HG2 graphics engine includes many new undocumented events that could be used in various ways. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/handle-graphics-behavior" rel="bookmark" title="Handle Graphics Behavior">Handle Graphics Behavior </a> <small>HG behaviors are an important aspect of Matlab graphics that enable custom control of handle functionality. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/improving-save-performance" rel="bookmark" title="Improving save performance">Improving save performance </a> <small>There are many different ways of improving Matlab's standard save function performance. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/waterloo-graphics" rel="bookmark" title="Waterloo graphics">Waterloo graphics </a> <small>Waterloo is an open-source library that can significantly improve Matlab GUI. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/improving-graphics-interactivity/feed</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Reverting axes controls in figure toolbar</title>
		<link>https://undocumentedmatlab.com/articles/reverting-axes-controls-in-figure-toolbar?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=reverting-axes-controls-in-figure-toolbar</link>
					<comments>https://undocumentedmatlab.com/articles/reverting-axes-controls-in-figure-toolbar#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Sun, 23 Dec 2018 19:52:19 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Figure]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<category><![CDATA[Toolbar]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8171</guid>

					<description><![CDATA[<p>In R2018b the axes controls were removed from the figure toolbar, but this can be reverted. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/reverting-axes-controls-in-figure-toolbar">Reverting axes controls in figure toolbar</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/customizing-figure-toolbar-background" rel="bookmark" title="Customizing figure toolbar background">Customizing figure toolbar background </a> <small>Setting the figure toolbar's background color can easily be done using just a tiny bit of Java magic powder. This article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uicontrol-side-effect-removing-figure-toolbar" rel="bookmark" title="uicontrol side-effect: removing figure toolbar">uicontrol side-effect: removing figure toolbar </a> <small>Matlab's built-in uicontrol function has a side-effect of removing the figure toolbar. This was undocumented until lately. This article describes the side-effect behavior and how to fix it....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-standard-figure-toolbar-menubar" rel="bookmark" title="Customizing the standard figure toolbar, menubar">Customizing the standard figure toolbar, menubar </a> <small>The standard figure toolbar and menubar can easily be modified to include a list of recently-used files....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-components" rel="bookmark" title="Figure toolbar components">Figure toolbar components </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>I planned to post a new article in my toolstrip mini-series, but then I came across something that I believe has a much greater importance and impacts many more Matlab users: the change in Matlab R2018b&#8217;s figure toolbar, where the axes controls (zoom, pan, rotate etc.) were moved to be next to the axes, which remain hidden until you move your mouse over the axes. Many users have complained about this unexpected change in the user interface of such important data exploration functionality:<br />
<center><figure style="width: 402px" class="wp-caption aligncenter"><img decoding="async" src="https://undocumentedmatlab.com/images/R2018a_toolbar2.gif" alt="R2018a (standard toolbar)" title="R2018a (standard toolbar)" width="402" height="359" /><figcaption class="wp-caption-text">R2018a (standard toolbar)</figcaption></figure> <figure style="width: 402px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/R2018b_toolbar_animated.gif" alt="R2018b (integrated axes toolbar)" title="R2018b (integrated axes toolbar)" width="402" height="409" /><figcaption class="wp-caption-text">R2018b (integrated axes toolbar)</figcaption></figure> </center><br />
Luckily, we can revert the change, <span id="more-8171"></span> as was recently explained in <a href="https://www.mathworks.com/matlabcentral/answers/419036-what-happened-to-the-figure-toolbar-in-r2018b-why-is-it-an-axes-toolbar-how-can-i-put-the-buttons" rel="nofollow" target="_blank">this Answers thread</a>:</p>
<pre lang="matlab">
addToolbarExplorationButtons(gcf) % Add the axes controls back to the figure toolbar
hAxes.Toolbar.Visible = 'off'; % Hide the integrated axes toolbar
%or:
hAxes.Toolbar = []; % Remove the axes toolbar data
</pre>
<p>And if you want to make these changes permanent (in other words, so that they would happen automatically whenever you open a new figure or create a new axes), then add the following code snippet to your <i>startup.m</i> file (in your Matlab startup folder):</p>
<pre lang="matlab">
try %#ok
    if ~verLessThan('matlab','9.5')
        set(groot,'defaultFigureCreateFcn',@(fig,~)addToolbarExplorationButtons(fig));
        set(groot,'defaultAxesCreateFcn',  @(ax,~)set(ax.Toolbar,'Visible','off'));
    end
end
</pre>
<p>MathWorks is taking a lot of heat over this change, and I agree that it could have done a better job of communicating the workaround in placing it as settable configurations in the Preferences panel or elsewhere. Whenever an existing functionality is broken, certainly one as critical as the basic data-exploration controls, MathWorks should take extra care to enable and communicate workarounds and settable configurations that would enable users a gradual smooth transition. Having said this, MathWorks does communicate the workaround in its <a href="https://www.mathworks.com/help/matlab/release-notes.html#mw_f6529119-5b24-43ff-b030-c649b2bf9600" rel="nofollow" target="_blank">release notes</a> (I&#8217;m not sure whether this was there from the very beginning or only recently added, but it&#8217;s there now).<br />
In my opinion the change was *not* driven by the marketing guys (as was the Desktop change from toolbars to toolstrip back in 2012 which <a href="https://www.mathworks.com/matlabcentral/answers/48070-experiences-with-release-2012b" rel="nofollow" target="_blank">received similar backlash</a>, and despite the heated accusations in the above-mentioned Answers thread). Instead, I believe that this change was technically-driven, as part of MathWorks&#8217; ongoing infrastructure changes to make Matlab increasingly web-friendly. The goal is that eventually all the figure functionality could transition to Java-script -based uifigures, replacing the current (&#8220;legacy&#8221;) Java-based figures, and enabling Matlab to work remotely, via any browser-enabled device (mobiles included), and not be tied to desktop operating systems. In this respect, toolbars do not transition well to webpages/Javascript, but the integrated axes toolbar does. Like it or not, eventually all of Matlab&#8217;s figures will become web-enabled content, and this is simply one step in this long journey. There will surely be other painful steps along the way, but hopefully MathWorks would learn a lesson from this change, and would make the transition smoother in the future.<br />
Once you regain your composure and take the context into consideration, you might wish to let MathWorks know what you think of the toolbar redesign <a href="https://www.mathworks.com/matlabcentral/answers/419036-what-happened-to-the-figure-toolbar-in-r2018b-why-is-it-an-axes-toolbar-how-can-i-put-the-buttons" rel="nofollow" target="_blank">here</a>. Please don&#8217;t complain to me &#8211; I&#8217;m only the messenger&#8230;<br />
Merry Christmas everybody!<br />
p.s. One of the complaints against the new axes toolbar is that it hurts productivity by forcing users to wait for the toolbar to fade-in and become clickable. Apparently the axes toolbar has a hidden private property called FadeGroup that presumably controls the fade-in/out effect. This can be accessed as follows:</p>
<pre lang="matlab">hFadeGroup = struct(hAxes.Toolbar).FadeGroup  % hAxes is the axes handle</pre>
<p>I have not [yet] discovered if and how this object can be customized to remove the fade animation or control its duration, but perhaps some smart hack would discover and post the workaround here (or let me know in a private message that I would then publish anonymously).</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/reverting-axes-controls-in-figure-toolbar">Reverting axes controls in figure toolbar</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/customizing-figure-toolbar-background" rel="bookmark" title="Customizing figure toolbar background">Customizing figure toolbar background </a> <small>Setting the figure toolbar's background color can easily be done using just a tiny bit of Java magic powder. This article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uicontrol-side-effect-removing-figure-toolbar" rel="bookmark" title="uicontrol side-effect: removing figure toolbar">uicontrol side-effect: removing figure toolbar </a> <small>Matlab's built-in uicontrol function has a side-effect of removing the figure toolbar. This was undocumented until lately. This article describes the side-effect behavior and how to fix it....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-standard-figure-toolbar-menubar" rel="bookmark" title="Customizing the standard figure toolbar, menubar">Customizing the standard figure toolbar, menubar </a> <small>The standard figure toolbar and menubar can easily be modified to include a list of recently-used files....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-components" rel="bookmark" title="Figure toolbar components">Figure toolbar components </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/reverting-axes-controls-in-figure-toolbar/feed</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>Toolbar button labels</title>
		<link>https://undocumentedmatlab.com/articles/toolbar-button-labels?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=toolbar-button-labels</link>
					<comments>https://undocumentedmatlab.com/articles/toolbar-button-labels#respond</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Mon, 08 Jan 2018 17:34:17 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Figure]]></category>
		<category><![CDATA[Toolbar]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7270</guid>

					<description><![CDATA[<p>GUI toolbar button labels can easily be set and customized using underlying Java components. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/toolbar-button-labels">Toolbar button labels</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/customizing-standard-figure-toolbar-menubar" rel="bookmark" title="Customizing the standard figure toolbar, menubar">Customizing the standard figure toolbar, menubar </a> <small>The standard figure toolbar and menubar can easily be modified to include a list of recently-used files....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-customizations" rel="bookmark" title="Figure toolbar customizations">Figure toolbar customizations </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to customize the Matlab figure toolbar....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/adding-a-search-box-to-figure-toolbar" rel="bookmark" title="Adding a search box to figure toolbar">Adding a search box to figure toolbar </a> <small>An interactive search-box can easily be added to a Matlab figure toolbar for enhanced user experience. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background" rel="bookmark" title="Customizing figure toolbar background">Customizing figure toolbar background </a> <small>Setting the figure toolbar's background color can easily be done using just a tiny bit of Java magic powder. This article explains how. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>I was recently asked by a client to add a few buttons labeled &#8220;1&#8221;-&#8220;4&#8221; to a GUI toolbar. I thought: How hard could that be? Simply <a href="/articles/figure-toolbar-components" target="_blank">get the toolbar&#8217;s handle from the figure</a>, then use the builtin <i><b>uipushtool</b></i> function to add a new button, specifying the label in the <b>String</b> property, right?<br />
<center><img decoding="async" src="https://undocumentedmatlab.com/images/toolbar_labels.png" alt="Labeled toolbar buttons" title="Labeled toolbar buttons" width="80%" style="max-width:612px;" height="140" /></center><br />
Well, not so fast it seems:</p>
<div class="wp_syntax">
<div class="code">
<pre class="matlab" style="font-family:monospace;">hToolbar = findall<span style="color: #080;">(</span>hFig, <span style="color:#A020F0;">'tag'</span>,<span style="color:#A020F0;">'FigureToolBar'</span><span style="color: #080;">)</span>;  <span style="color: #228B22;">% get the figure's toolbar handle</span>
uipushtool<span style="color: #080;">(</span>hToolbar, <span style="color:#A020F0;">'String'</span>,<span style="color:#A020F0;">'1'</span><span style="color: #080;">)</span>;               <span style="color: #228B22;">% add a pushbutton to the toolbar</span>
<span style="color: #FF0000;">Error using <u>uipushtool</u>
There is no String property on the PushTool class. </pre>
</div>
</div>
<p>Apparently, for some unknown reason, standard Matlab only enables us to set the icon (<b>CData</b>) of a toolbar control, but not a text label.<br />
Once again, Java to the rescue: <span id="more-7270"></span><br />
We first get the Java toolbar reference handle, then add the button in standard Matlab (using <i><b>uipushtool</b></i>, <i><b>uitoggletool</b></i> and their kin). We can now access the Java toolbar&#8217;s last component, relying on the fact that Matlab always adds new buttons at the end of the toolbar. Note that we need to use a short <i><b>drawnow</b></i> to ensure that the toolbar is fully re-rendered, otherwise we&#8217;d get an invalid Java handle. Finally, once we have this reference handle to the underlying Java button component, we can set and customize its label text and appearance (font face, border, size, alignment etc.):</p>
<pre lang="matlab">
hToolbar = findall(hFig, 'tag','FigureToolBar');     % get the figure's toolbar handle
jToolbar = hToolbar.JavaContainer.getComponentPeer;  % get the toolbar's Java handle
for buttonIdx = 1 : 4
    % First create the toolbar button using standard Matlab code
    label = num2str(buttonIdx);  % create a string label
    uipushtool(hToolbar, 'ClickedCallback',{@myCallback,analysisIdx}, 'TooltipString',['Run analysis #' label]);
    % Get the Java reference handle to the newly-created button
    drawnow; pause(0.01);  % allow the GUI time to re-render the toolbar
    jButton = jToolbar.getComponent(jToolbar.getComponentCount-1);
    % Set the button's label
    jButton.setText(label)
end
</pre>
<p>The standard Matlab toolbar button size (23&#215;23 pixels) is too small to display more than a few characters. To display a longer label, we need to widen the button:</p>
<pre lang="matlab">
% Make the button wider than the standard 23 pixels
newSize = java.awt.Dimension(50, jButton.getHeight);
jButton.setMaximumSize(newSize)
jButton.setPreferredSize(newSize)
jButton.setSize(newSize)
</pre>
<p>Using a text label does not prevent us from also displaying an icon: In addition to the text label, we can also display a standard icon (by setting the button&#8217;s <b>CData</b> property in standard Matlab). This icon will be displayed to the left of the text label. You can widen the button, as shown in the code snippet above, to make space for both the icon and the label. If you want to move the label to a different location relative to the icon, simply modify the Java component&#8217;s HorizontalTextPosition property:</p>
<pre lang="matlab">
jButton.setHorizontalTextPosition(jButton.RIGHT);   % label right of icon (=default)
jButton.setHorizontalTextPosition(jButton.CENTER);  % label on top of icon
jButton.setHorizontalTextPosition(jButton.LEFT);    % label left of icon
</pre>
<p>In summary, here&#8217;s the code snippet that generated the screenshot above:</p>
<pre lang="matlab">
% Get the Matlab & Java handles to the figure's toolbar
hToolbar = findall(hFig, 'tag','FigureToolBar');     % get the figure's toolbar handle
jToolbar = hToolbar.JavaContainer.getComponentPeer;  % get the toolbar's Java handle
% Button #1: label only, no icon, 23x23 pixels
h1 = uipushtool(hToolbar);
drawnow; pause(0.01);
jButton = jToolbar.getComponent(jToolbar.getComponentCount-1);
jButton.setText('1')
% Create the icon CData from an icon file
graphIcon = fullfile(matlabroot,'/toolbox/matlab/icons/plotpicker-plot.gif');
[graphImg,map] = imread(graphIcon);
map(map(:,1)+map(:,2)+map(:,3)==3) = NaN;  % Convert white pixels => transparent background
cdata = ind2rgb(graphImg,map);
% Button #2: label centered on top of icon, 23x23 pixels
h2 = uipushtool(hToolbar, 'CData',cdata);
drawnow; pause(0.01);
jButton = jToolbar.getComponent(jToolbar.getComponentCount-1);
jButton.setText('2')
jButton.setHorizontalTextPosition(jButton.CENTER)
% Button #3: label on right of icon, 50x23 pixels
h3 = uipushtool(hToolbar, 'CData',cdata);
drawnow; pause(0.01);
jButton = jToolbar.getComponent(jToolbar.getComponentCount-1);
jButton.setText('3...')
d = java.awt.Dimension(50, jButton.getHeight);
jButton.setMaximumSize(d); jButton.setPreferredSize(d); jButton.setSize(d)
% Button #4: label on left of icon, 70x23 pixels
h4 = uipushtool(hToolbar, 'CData',cdata);
drawnow; pause(0.01);
jButton = jToolbar.getComponent(jToolbar.getComponentCount-1);
jButton.setText('and 4:')
jButton.setHorizontalTextPosition(jButton.LEFT)
d = java.awt.Dimension(70, jButton.getHeight);
jButton.setMaximumSize(d); jButton.setPreferredSize(d); jButton.setSize(d)
</pre>
<p>Many additional toolbar customizations can be found <a href="/articles/tag/toolbar" target="_blank">here</a> and in my book &#8220;<a href="/books/matlab-java" target="_blank"><b>Undocumented Secrets of MATLAB-Java Programming</b></a>&#8220;. If you&#8217;d like me to design a professional-looking GUI for you, please <a href="/consulting" target="_blank">contact me</a>.<br />
Caveat emptor: all this only works with the regular Java-based GUI figures, not web-based (&#8220;App-Designer&#8221;) uifigures.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/toolbar-button-labels">Toolbar button labels</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/customizing-standard-figure-toolbar-menubar" rel="bookmark" title="Customizing the standard figure toolbar, menubar">Customizing the standard figure toolbar, menubar </a> <small>The standard figure toolbar and menubar can easily be modified to include a list of recently-used files....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-customizations" rel="bookmark" title="Figure toolbar customizations">Figure toolbar customizations </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to customize the Matlab figure toolbar....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/adding-a-search-box-to-figure-toolbar" rel="bookmark" title="Adding a search box to figure toolbar">Adding a search box to figure toolbar </a> <small>An interactive search-box can easily be added to a Matlab figure toolbar for enhanced user experience. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background" rel="bookmark" title="Customizing figure toolbar background">Customizing figure toolbar background </a> <small>Setting the figure toolbar's background color can easily be done using just a tiny bit of Java magic powder. This article explains how. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/toolbar-button-labels/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Customizing figure toolbar background</title>
		<link>https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=customizing-figure-toolbar-background</link>
					<comments>https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 20 Feb 2013 18:00:35 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Figure]]></category>
		<category><![CDATA[Toolbar]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3634</guid>

					<description><![CDATA[<p>Setting the figure toolbar's background color can easily be done using just a tiny bit of Java magic powder. This article explains how. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background">Customizing figure toolbar background</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/customizing-standard-figure-toolbar-menubar" rel="bookmark" title="Customizing the standard figure toolbar, menubar">Customizing the standard figure toolbar, menubar </a> <small>The standard figure toolbar and menubar can easily be modified to include a list of recently-used files....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/toolbar-button-labels" rel="bookmark" title="Toolbar button labels">Toolbar button labels </a> <small>GUI toolbar button labels can easily be set and customized using underlying Java components. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-components" rel="bookmark" title="Figure toolbar components">Figure toolbar components </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-customizations" rel="bookmark" title="Figure toolbar customizations">Figure toolbar customizations </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to customize the Matlab figure toolbar....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>In one of my projects, I needed to present a radar (polar) plot. Such plots are usually drawn on a black background and I wanted all the plot controls to blend into this background.<br />
<figure style="width: 423px" class="wp-caption alignright"><img loading="lazy" decoding="async" alt="Matlab figure having black toolbar background" src="https://undocumentedmatlab.com/images/Playback.gif" title="Matlab figure having black toolbar background" width="423" height="479" /><figcaption class="wp-caption-text">Matlab figure having black toolbar background</figcaption></figure><br />
For the plot itself I used a variation of Matlab&#8217;s buggy <a target="_blank" rel="nofollow" href="http://www.mathworks.com/help/matlab/ref/polar.html">polar</a> function, which I modified to enable proper dynamic resize / zoom / pan, bypass figure-renderer issues with patches and data-cursors, and other similar annoyances. Pretty standard stuff.<br />
For the slider I&#8217;ve used a <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/components/slider.html"><code>javax.swing.JSlider</code></a> having a <a target="_blank" href="/articles/continuous-slider-callback/">continuous-movement callback</a>. Again, for readers of this blog this is nothing special:</p>
<pre lang='matlab'>
[jSlider,hSlider] = javacomponent('javax.swing.JSlider',[0,0,.01,0.1],hFig);
set(hSlider, 'Units','norm','pos',[.15,0,.7,.05]);
set(jSlider, 'Background',java.awt.Color.black, ...
             'Value',0, 'Maximum',duration, ...
             'StateChangedCallback',{@cbSlider,hFig,axPlayback});
</pre>
<p>Setting the background color for all the GUI components to black was easy. But setting the toolbar&#8217;s background to black turned out to be a bit more interesting, and is the topic of this week&#8217;s article.<br />
<span id="more-3634"></span><br />
<center><figure style="width: 423px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Standard Matlab figure toolbar - yuck!" src="https://undocumentedmatlab.com/images/Toolbar1.gif" title="Standard Matlab figure toolbar - yuck!" width="423" height="114" /><figcaption class="wp-caption-text">Standard Matlab figure toolbar - yuck!</figcaption></figure></center><br />
The first step, naturally, is to <a target="_blank" href="/articles/figure-toolbar-components/">get the toolbar&#8217;s handle</a>:</p>
<pre lang='matlab'>
hToolbar = findall(hFig,'tag','FigureToolBar');
</pre>
<p>In my case, I programmatically create the figure and use the default figure toolbar, whose tag value is always &#8216;FigureToolBar&#8217;. If I had used a custom toolbar, I would naturally use the corresponding tag (for example, if you create a custom toolbar using GUIDE, then the tag name will probably be &#8216;toolbar1&#8217; or something similar).<br />
Since I&#8217;m setting the figure programmatically, I need to manually remove several unuseful toolbar controls. I do this by <a target="_blank" href="/articles/modifying-default-toolbar-menubar-actions/">directly accessing the toolbar control handles</a>:</p>
<pre lang='matlab'>
delete(findall(hToolbar,'tag','Plottools.PlottoolsOn'))
delete(findall(hToolbar,'tag','Plottools.PlottoolsOff'))
delete(findall(hToolbar,'tag','Annotation.InsertColorbar'))
delete(findall(hToolbar,'tag','DataManager.Linking'))
delete(findall(hToolbar,'tag','Standard.EditPlot'))
</pre>
<p>For setting the bgcolor, we get the <a target="_blank" href="/articles/customizing-standard-figure-toolbar-menubar/#Toolbar">toolbar&#8217;s underlying Java component</a>, then sprinkle some Java magic power:</p>
<pre lang='matlab'>
% ensure the toolbar is visible onscreen
drawnow;
% Get the underlying JToolBar component
jToolbar = get(get(hToolbar,'JavaContainer'),'ComponentPeer');
% Set the bgcolor to black
color = java.awt.Color.black;
jToolbar.setBackground(color);
jToolbar.getParent.getParent.setBackground(color);
% Remove the toolbar border, to blend into figure contents
jToolbar.setBorderPainted(false);
% Remove the separator line between toolbar and contents
jFrame = get(handle(hFig),'JavaFrame');
jFrame.showTopSeparator(false);
</pre>
<p>Unfortunately, this is not enough. The reason is that some of Matlab&#8217;s standard toolbar icons use non-opaque Java button controls (thereby showing the new black bgcolor), whereas other icons use opaque buttons, with a hard-coded gray background (I feel like spanking someone&#8230;). I&#8217;ve already <a target="_blank" href="/articles/figure-toolbar-customizations/">touched upon this issue briefly</a> a few years ago.<br />
<center><figure style="width: 423px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Matlab figure toolbar with black background, some opaque buttons" src="https://undocumentedmatlab.com/images/Toolbar2.gif" title="Matlab figure toolbar with black background, some opaque buttons" width="423" height="114" /><figcaption class="wp-caption-text">Matlab figure toolbar with black background, some opaque buttons</figcaption></figure></center><br />
Luckily, all is not lost: we simply need to loop over all the JToolBar&#8217;s components and force them to be non-opaque with a black bgcolor. In cases where the component is compound (e.g., the Brush Data <a target="_blank" href="/articles/uisplittool-uitogglesplittool/"><i><b>uisplittool</b></i></a>), we need to set the bgcolor for all the sub-components:</p>
<pre lang='matlab'>
jtbc = jToolbar.getComponents;
for idx=1:length(jtbc)
    jtbc(idx).setOpaque(false);
    jtbc(idx).setBackground(color);
    for childIdx = 1 : length(jtbc(idx).getComponents)
        jtbc(idx).getComponent(childIdx-1).setBackground(color);
    end
end
</pre>
<p>&#8230;finally ending up with the blended appearance that appears at the top of this article.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background">Customizing figure toolbar background</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/customizing-standard-figure-toolbar-menubar" rel="bookmark" title="Customizing the standard figure toolbar, menubar">Customizing the standard figure toolbar, menubar </a> <small>The standard figure toolbar and menubar can easily be modified to include a list of recently-used files....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/toolbar-button-labels" rel="bookmark" title="Toolbar button labels">Toolbar button labels </a> <small>GUI toolbar button labels can easily be set and customized using underlying Java components. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-components" rel="bookmark" title="Figure toolbar components">Figure toolbar components </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-customizations" rel="bookmark" title="Figure toolbar customizations">Figure toolbar customizations </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to customize the Matlab figure toolbar....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background/feed</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>Customizing the standard figure toolbar, menubar</title>
		<link>https://undocumentedmatlab.com/articles/customizing-standard-figure-toolbar-menubar?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=customizing-standard-figure-toolbar-menubar</link>
					<comments>https://undocumentedmatlab.com/articles/customizing-standard-figure-toolbar-menubar#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 09 Jan 2013 18:00:12 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented function]]></category>
		<category><![CDATA[Figure]]></category>
		<category><![CDATA[Menubar]]></category>
		<category><![CDATA[Toolbar]]></category>
		<category><![CDATA[uitools]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3461</guid>

					<description><![CDATA[<p>The standard figure toolbar and menubar can easily be modified to include a list of recently-used files.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-standard-figure-toolbar-menubar">Customizing the standard figure toolbar, menubar</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/customizing-figure-toolbar-background" rel="bookmark" title="Customizing figure toolbar background">Customizing figure toolbar background </a> <small>Setting the figure toolbar's background color can easily be done using just a tiny bit of Java magic powder. This article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/modifying-default-toolbar-menubar-actions" rel="bookmark" title="Modifying default toolbar/menubar actions">Modifying default toolbar/menubar actions </a> <small>The default Matlab figure toolbar and menu actions can easily be modified using simple pure-Matlab code. This article explains how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-components" rel="bookmark" title="Figure toolbar components">Figure toolbar components </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-customizations" rel="bookmark" title="Figure toolbar customizations">Figure toolbar customizations </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to customize the Matlab figure toolbar....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>A few days ago, a client asked me to integrate an MRU (most-recently-used) file list in a Matlab GUI window. The naive approach was to add a new &#8220;Recent files&#8221; main menu, but this would look bad. Today I explain how to integrate the MRU list into the toolbar&#8217;s standard &#8220;Open File&#8221; button, as well as into the standard &#8220;File&#8221; main menu.</p>
<h3 id="Menubar">Customizing the standard &#8220;File&#8221; main menu</h3>
<p>Note: this relies on earlier articles about <a target="_blank" href="/articles/tag/menubar/">customizing the figure menubar</a>, so if you are not comfortable with this topic you might benefit from reading these earlier articles.<br />
Customizing the standard &#8220;File&#8221; main menu is easy. First, let&#8217;s find the &#8220;File&#8221; main menu&#8217;s handle, and add an MRU sub-menu directly to it:</p>
<pre lang='matlab'>
hFileMenu = findall(gcf, 'tag', 'figMenuFile');
hMruMenu = uimenu('Label','Recent files', 'Parent',hFileMenu);
</pre>
<p>Our new MRU menu item is created at the end (in this case, at the bottom of the &#8220;File&#8221; main menu list). Let&#8217;s move it upward, between &#8220;New&#8221; and &#8220;Open&#8230;&#8221;, by reordering <code>hFileMenu</code>&#8216;s <b>Children</b> menu items:</p>
<pre lang='matlab'>
hAllMenuItems = allchild(hFileMenu);
set(hFileMenu, 'Children',fliplr(hAllMenuItems([2:end-1,1,end])));  % place in 2nd position, just above the "Open" item
</pre>
<p>Now let&#8217;s fix the &#8220;Open&#8230;&#8221; menu item&#8217;s callback to point to our custom <i>openFile()</i> function (unfortunately, the &#8220;Open&#8230;&#8221; menu item has no tag, so we must rely on its label to get its handle):</p>
<pre lang='matlab'>
hOpenMenu = findall(hFileMenu, 'Label', '&Open...');
set(hOpenMenu, 'Callback',@openFile);
</pre>
<p>Finally, let&#8217;s add the MRU list as a sub-menu of the new <code>hMruMenu</code>. I assume that we have a <i>getMRU()</i> function in our code, which returns a cell-array of filenames:</p>
<pre lang='matlab'>
% Add the list of recent files, one item at a time
filenames = getMRU();
for fileIdx = 1 : length(filenames)
    uimenu(hMruMenu, 'Label',filenames{fileIdx}, 'Callback',{@openFile,filenames{fileIdx}});
end
</pre>
<p><center><figure style="width: 445px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Modified standard figure menu-bar" src="https://undocumentedmatlab.com/images/Menubar_File_Open.png" title="Modified standard figure menu-bar" width="445" height="182" /><figcaption class="wp-caption-text">Modified standard figure menu-bar</figcaption></figure></center><br />
<span id="more-3461"></span><br />
Clicking the main &#8220;Open&#8230;&#8221; menu item calls our <i>openFile()</i> function without the optional filename input argument, while selecting one of the MRU files will call the <i>openFile()</i> function with that specific filename as input. The <i>openFile()</i> function could be implemented something like this:</p>
<pre lang='matlab'>
% Callback for the open-file functionality (toolbar and menubar)
function openFile(hObject,eventData,filename)
    % If no filename specified, ask for it from the user
    if nargin < 3
        filename = uigetfile({'*.csv','Data files (*.csv)'}, 'Open data file');
        if isempty(filename) || isequal(filename,0)
            return;
        end
    end
    % Open the selected file and read the data
    data = readDataFile(filename);
    % Update the display
    updateGUI(data)
end
</pre>
<p>We can take this idea even further by employing <a target="_blank" href="/articles/html-support-in-matlab-uicomponents/">HTML formatting</a>, as I have shown in my <a target="_blank" href="/articles/customizing-menu-items-part-1/">first article of the menubar mini-series</a>:<br />
<center><figure style="width: 411px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="HTML-rendered menu items" src="https://undocumentedmatlab.com/images/uimenu2b.png" title="HTML-rendered menu items" width="411" height="246"/><figcaption class="wp-caption-text">HTML-rendered menu items</figcaption></figure></center></p>
<h3 id="Toolbar">Customizing the standard toolbar's "Open File" button</h3>
<p>Note: this relies on earlier articles about <a target="_blank" href="/articles/tag/toolbar/">customizing the figure toolbar</a>, so if you are not comfortable with this topic you might benefit from reading these earlier articles.<br />
The basic idea here is to replace the standard toolbar's "Open File" pushbutton with a new <a target="_blank" href="/articles/uisplittool-uitogglesplittool/">uisplittool</a> button that will contain the MRU list in its picker-menu.<br />
The first step is to get the handle of the toolbar's "Open File" button:</p>
<pre lang='matlab'>
hOpen = findall(gcf, 'tooltipstring', 'Open File');
hOpen = findall(gcf, 'tag', 'Standard.FileOpen');  % Alternative
</pre>
<p>The second alternative is better for non-English Matlab installations where the tooltip text may be different, or in cases where we might have another GUI control with this specific tooltip. On the other hand, the <code>'Standard.FileOpen'</code> tag may be different in different Matlab releases. So choose whichever option is best for your specific needs.<br />
Assuming we have a valid (non-empty) <code>hOpen</code> handle, we get its properties data for later use:</p>
<pre lang='matlab'>
open_data = get(hOpen);
hToolbar = open_data.Parent;
</pre>
<p>We have all the information we need, so we can now simply delete the existing toolbar open button, and create a new split-button with the properties data that we just got:</p>
<pre lang='matlab'>
delete(hOpen);
hNewOpen = uisplittool('Parent',hToolbar, ...
                       'CData',open_data.CData, ...
                       'Tooltip',open_data.TooltipString, ...
                       'ClickedCallback',@openFile);
</pre>
<p>As with the menubar, the button is now created, but it appears on the toolbar&#8217;s right edge. Let&#8217;s move it to the far left. We could theoretically reorder <code>hToolbar</code>&#8216;s <b>Children</b> as for the menubar above, but Matlab has an internal bug that causes some toolbar buttons to misbehave upon rearranging. Using Java solves this:</p>
<pre lang='matlab'>
drawnow;  % this innocent drawnow is *very* important, otherwise Matlab might crash!
jToolbar = get(get(hToolbar,'JavaContainer'),'ComponentPeer');
jButtons = jToolbar.getComponents;
jToolbar.setComponentZOrder(jButtons(end),2);  % Move to the position between "New" and "Save"
jToolbar.revalidate;  % update the toolbar's appearance (drawnow will not do this)
</pre>
<p>Finally, let&#8217;s add the list of recent files to the new split-button&#8217;s picker menu:</p>
<pre lang='matlab'>
% (Use the same list of filenames as for the menubar's MRU list)
jNewOpen = get(hNewOpen,'JavaContainer');
jNewOpenMenu = jNewOpen.getMenuComponent;
for fileIdx = 1 : length(filenames)
    jMenuItem = handle(jNewOpenMenu.add(filenames{fileIdx}),'CallbackProperties');
    set(jMenuItem,'ActionPerformedCallback',{@openFile,filenames{fileIdx}});
end
</pre>
<p><center><figure style="width: 445px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Modified standard figure toolbar" src="https://undocumentedmatlab.com/images/Toolbar_Open_File.png" title="Modified standard figure toolbar" width="447" height="143" /><figcaption class="wp-caption-text">Modified standard figure toolbar</figcaption></figure></center><br />
Clicking the main Open button calls our <i>openFile()</i> function without the optional filename input argument, while clicking the picker button (to the right of the main Open button) and selecting one of the files will call the <i>openFile()</i> function with that specific filename as input.<br />
That&#8217;s it. Quite painless in fact. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-standard-figure-toolbar-menubar">Customizing the standard figure toolbar, menubar</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/customizing-figure-toolbar-background" rel="bookmark" title="Customizing figure toolbar background">Customizing figure toolbar background </a> <small>Setting the figure toolbar's background color can easily be done using just a tiny bit of Java magic powder. This article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/modifying-default-toolbar-menubar-actions" rel="bookmark" title="Modifying default toolbar/menubar actions">Modifying default toolbar/menubar actions </a> <small>The default Matlab figure toolbar and menu actions can easily be modified using simple pure-Matlab code. This article explains how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-components" rel="bookmark" title="Figure toolbar components">Figure toolbar components </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-customizations" rel="bookmark" title="Figure toolbar customizations">Figure toolbar customizations </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to customize the Matlab figure toolbar....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/customizing-standard-figure-toolbar-menubar/feed</wfw:commentRss>
			<slash:comments>12</slash:comments>
		
		
			</item>
		<item>
		<title>ScreenCapture utility</title>
		<link>https://undocumentedmatlab.com/articles/screencapture-utility?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=screencapture-utility</link>
					<comments>https://undocumentedmatlab.com/articles/screencapture-utility#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 15 Aug 2012 16:54:11 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Kesh Ikuma]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Toolbar]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3059</guid>

					<description><![CDATA[<p>The ScreenCapture utility uses purely-documented Matlab for capturing a screen region as an image from within Matlab. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/screencapture-utility">ScreenCapture utility</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/interesting-uitree-utility" rel="bookmark" title="An interesting uitree utility">An interesting uitree utility </a> <small>ExploreStruct is a utility that shows how custom uitrees can be integrated in Matlab GUI...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/savezip-utility" rel="bookmark" title="savezip utility">savezip utility </a> <small>Matlab data can be serialized and saved into a ZIP/GZIP file, and loaded back. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/plotedit-context-menu-customization" rel="bookmark" title="PlotEdit context-menu customization">PlotEdit context-menu customization </a> <small>A combination of Matlab and Java Robot commands to automate a certain animation can be used when we cannot access underlying GUI/graphics code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/frameless-undecorated-figure-windows" rel="bookmark" title="Frameless (undecorated) figure windows">Frameless (undecorated) figure windows </a> <small>Matlab figure windows can be made undecorated (borderless, title-less). ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>A few days ago, my <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/24323-ScreenCapture">ScreenCapture utility</a> was <a target="_blank" rel="nofollow" href="http://blogs.mathworks.com/pick/2012/08/10/grab-a-screen-shot-from-within-matlab/">selected</a> as Matlab&#8217;s Pick of the Week (POTW). POTW selections are normally extremely useful, well-written and instructive utilities, which are both great to use in their own right, as well as a great source of knowledge about Matlab programming features and good practices. I follow the weekly POTW selections closely, and often learn new stuff from these utilities. I take pride in the fact that some of my utilities have been selected for inclusion in this unique set.<br />
ScreenCapture enables Matlab users to take automated (programmatic) as well as interactive screen-captures of any Matlab GUI component or sub-region. This includes figure windows, axes, images, controls and even the Matlab Desktop. If the target handle for the capture is not specified, then ScreenCapture prompts the user to interactively select the capture region using an <i><b>rbbox</b></i> limiting box. ScreenCapture also includes a feature that plants a camera icon in the figure toolbar, such that clicking this icon will immediately trigger the interactive region-selection screen-capture.<br />
Whichever manner the capture was made, the user then has the option of sending the output to Matlab (as a 3D RGB image matrix), or to one of the standard image file formats (e.g., JPG or PNG).<br />
ScreenCapture has extensive help and is well-documented and relatively easy to use. For example:</p>
<pre lang='matlab'>figure; surf(peaks); imgData=screencapture(gcf); imshow(imgData);</pre>
<p><center><figure style="width: 450px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="ScreenCapture in action" src="https://undocumentedmatlab.com/images/ScreenCapture.png" title="ScreenCapture in action" width="450" height="480" /><figcaption class="wp-caption-text">ScreenCapture in action</figcaption></figure></center><br />
<span id="more-3059"></span><br />
Some additional usage examples:</p>
<pre lang='matlab'>
imageData = screencapture;                     % interactively select screen-capture rectangle
imageData = screencapture(hListbox);           % capture image of a uicontrol
imageData = screencapture(0,  [20,30,40,50]);  % capture a small desktop sub-region
imageData = screencapture(gcf,[20,30,40,50]);  % capture a small figure sub-region
% capture a small sub-region of an axes
imageData = screencapture(gca,[10,20,30,40]);
imshow(imageData);  % display the captured image in a matlab figure
imwrite(imageData,'myImage.png');  % save the captured image to file
% capture a sub-region of an image
img = imread('cameraman.tif');
hImg = imshow(img);
screencapture(hImg,[60,35,140,80]);  % in data units, not pixel units
screencapture(gcf,[],'myFigure.jpg');                   % capture the entire figure into file
screencapture('handle',gcf,'filename','myFigure.jpg');  % same as previous
screencapture('toolbar',gcf);                           % adds a screen-capture button to gcf's toolbar
screencapture('toolbar',[],'file','sc.bmp');            % same, using a default output filename
</pre>
<h3 id="documented">Purely documented</h3>
<p>Over the course of the past few years I have submitted <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/index?term=authorid%3A27420">40 utilities</a> to the Matlab File Exchange, several of which have been selected for POTW. Unfortunately, since most of my utilities employ undocumented Matlab features to some extent (I can&#8217;t help myself&#8230;), they are ineligible for being selected as POTF, useful and deserving as they may be. In fact, my <a target="_blank" href="/articles/cprintf-display-formatted-color-text-in-command-window/">cprintf utility</a>, which was selected as POTW, was quickly deselected as POTW because of this very issue. MathWorks fears (and I can certainly understand the concern) that highlighting a utility that relies on some undocumented feature as POTW might be considered as an official endorsement of these features.<br />
ScreenCapture is different in this regard: it uses purely documented Matlab functionality to achieve its aims, and apparently still succeeds in providing useful functionality. This does not mean that ScreenCapture uses pure Matlab. In fact, it relies on the <a target="_blank" href="http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html">Java <code>Robot</code> class</a>&#8216;s functionality of taking a screen-capture of a specified area of the screen. Using the Java <code>Robot</code> class in such a way is an entirely <a target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/matlab_external/f44062.html">documented</a> Matlab feature. I have discussed the Java <code>Robot</code> in two past articles on this blog, where guest blogger Kesh Ikuma explained (<a target="_blank" href="/articles/gui-automation-robot/">here</a> and <a target="_blank" href="/articles/gui-automation-utilities/">here</a>) how it can be used to simulate mouse and keyboard actions programmatically.</p>
<h3 id="technical">Under the hood</h3>
<p>ScreenCapture calculates the requested screen-capture rectangle coordinates and then invokes the Java <code>Robot</code> to take the actual bitmap screen-capture. The output is then converted into a Matlab image matrix for output, or stored in an image file, based on the user&#8217;s choice of parameters.<br />
Some difficulties that I overcame when programming ScreenCapture:</p>
<ul>
<li>The screen position of docked windows cannot be computed reliably. Docked windows therefore need to be automatically temporarily undocked for screen-capture.</li>
<li>Undocking in Windows 7 with Aero transparency features causes the <code>Robot</code> to take its screen-shot before the window becomes fully opaque. Adding a short delay in undocking solved this issue.</li>
<li>Images use reversed Y-axis (Y=0 is at the axes top, not bottom). Also, when specifying a sub-region for capture, many users are used to handling images using data units (e.g., for <i><b>imcrop</b></i>) rather than ScreenCapture&#8217;s standard pixel units. Taking screen-captures of images proved to be a non-trivial challenge indeed.</li>
<li>Different Matlab objects (controls, axes, figures) have different external borders and internal margins. I had to take these into account in order to achieve tight-fitting image captures of these objects.</li>
<li>Performance was a problem, and it turned out that the bottleneck was trying to convert from the Java image data format to Matlab&#8217;s image data format. A couple of suggestions by Jan Simon and Urs (us) Schwartz significantly improved this performance hotspot. I&#8217;ve submitted the relevant code snippets to MathWorks for incorporation in a published <a target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-2WPAYR/">technical solution</a>, and I was happy to see that they have indeed incorporated them into that solution.</li>
<li>Finally, I thought that adding a custom toolbar image would be a nice touch. Since I&#8217;m not much of an artist, creating the camera icon programmatically proved to be a bit of a challenge&#8230;</li>
</ul>
<p>Please feel free to download ScreenCapture&#8217;s code and check how I chose to program around these issues.</p>
<h3 id="TODO">TODO list</h3>
<p>Some potentially-useful features have so far eluded me in ScreenCapture&#8217;s implementation. Perhaps one day I will find a way to do them:</p>
<ol>
<li>Enable output of the image data, as an image object, to the system clipboard. It is easy to serialize the data and store it as a string in the clipboard, using the built-in <i><b>clipboard</b></i> function. But we would not be able to paste this data as an image into an external editor or image-processing utility. I have not yet found an easy way to store the image data as an object, although it should not be very difficult to do (here&#8217;s a <a target="_blank" rel="nofollow" href="http://www.exampledepot.com/egs/java.awt.datatransfer/ToClipImg.html">starter</a>).</li>
<li>When interactively selecting a screen-capture region, <i><b>rbbox</b></i>&#8216;s starting point needs to be somewhere within the boundaries of a Matlab figure. The box can extend beyond the figure&#8217;s borders, but it has to start somewhere within the figure. I would like to be able to use <i><b>rbbox</b></i> without this limitation.</li>
</ol>
<p><b><u>Addendum Jan 28. 2013</u></b>: A new version of ScreenCapture was uploaded to the <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/24323-ScreenCapture">File Exchange</a> today which appears to solve both of the TODO issues above: The copy-to-clipboard feature relies on Jiro Doke&#8217;s <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/28708-imclipboard">imclipboard utility</a> as mentioned by Matt <a target="_blank" href="/articles/screencapture-utility/#comment-106188">below</a> &#8212; simply specify the &#8216;clipboard&#8217; string as the capture target (rather than a filename); the solution of the <i><b>rbbox</b></i>-anywhere feature relies on using a temporary <a target="_blank" href="/articles/transparent-matlab-figure-window/">transparent window</a> that <a target="_blank" href="/articles/minimize-maximize-figure-window/">spans the entire desktop area</a>, capturing the user&#8217;s <i><b>rbbox</b></i> clicks anywhere within the desktop area. Interested readers can easily adapt the code to fit multiple monitors (I didn&#8217;t bother).</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/screencapture-utility">ScreenCapture utility</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/interesting-uitree-utility" rel="bookmark" title="An interesting uitree utility">An interesting uitree utility </a> <small>ExploreStruct is a utility that shows how custom uitrees can be integrated in Matlab GUI...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/savezip-utility" rel="bookmark" title="savezip utility">savezip utility </a> <small>Matlab data can be serialized and saved into a ZIP/GZIP file, and loaded back. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/plotedit-context-menu-customization" rel="bookmark" title="PlotEdit context-menu customization">PlotEdit context-menu customization </a> <small>A combination of Matlab and Java Robot commands to automate a certain animation can be used when we cannot access underlying GUI/graphics code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/frameless-undecorated-figure-windows" rel="bookmark" title="Frameless (undecorated) figure windows">Frameless (undecorated) figure windows </a> <small>Matlab figure windows can be made undecorated (borderless, title-less). ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/screencapture-utility/feed</wfw:commentRss>
			<slash:comments>35</slash:comments>
		
		
			</item>
		<item>
		<title>handle2struct, struct2handle &#038; Matlab 8.0</title>
		<link>https://undocumentedmatlab.com/articles/handle2struct-struct2handle-and-matlab-8?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=handle2struct-struct2handle-and-matlab-8</link>
					<comments>https://undocumentedmatlab.com/articles/handle2struct-struct2handle-and-matlab-8#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 29 Dec 2010 18:00:56 +0000</pubDate>
				<category><![CDATA[Handle graphics]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Semi-documented function]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[GUIDE]]></category>
		<category><![CDATA[HG2]]></category>
		<category><![CDATA[Hidden property]]></category>
		<category><![CDATA[Menubar]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<category><![CDATA[Toolbar]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2023</guid>

					<description><![CDATA[<p>This article explains how we can use a couple of undocumented functions in Matlab GUI, and what we can learn from this about Matlab's future.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/handle2struct-struct2handle-and-matlab-8">handle2struct, struct2handle &amp; Matlab 8.0</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/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 point as it returns with 100 or less points....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/fig-files-format" rel="bookmark" title="FIG files format">FIG files format </a> <small>FIG files are actually MAT files in disguise. This article explains how this can be useful in Matlab applications....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-layout-managers-uicontainer-and-relatives" rel="bookmark" title="Matlab layout managers: uicontainer and relatives">Matlab layout managers: uicontainer and relatives </a> <small>Matlab contains a few undocumented GUI layout managers, which greatly facilitate handling GUI components in dynamically-changing figures....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-hg2" rel="bookmark" title="Matlab&#039;s HG2 mechanism">Matlab&#039;s HG2 mechanism </a> <small>HG2 is presumably the next generation of Matlab graphics. This article tries to explore its features....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Last week I explained that <a target="_blank" href="/articles/fig-files-format/">FIG files are simply MAT files in disguise</a>. Today, we look under the hood of Matlab&#8217;s <i><b>hgsave</b></i> function, which is used to save FIG files. We shall see that this is both useful and illuminating vis-a-vis Matlab&#8217;s future.</p>
<h3 id="handle2struct">handle2struct</h3>
<p>Under the hood, <i><b>hgsave</b></i> uses the <a target="_blank" href="/articles/legend-semi-documented-feature/#Semi-documented">semi-documented</a> built-in <i><b>handle2struct</b></i> function to convert the figure handle into a Matlab <i><b>struct</b></i> that is then stored with a simple <i><b>save</b></i> (the same function that saves MAT files) function call.<br />
The fact that <i><b>handle2struct</b></i> is semi-documented means that the function is explained in a help comment (which can be seen via the <i><b>help</b></i> command), that is nonetheless not part of the official doc sections. It is an unsupported feature originally intended only for internal Matlab use (which of course doesn&#8217;t mean we can&#8217;t use it).<br />
<i><b>handle2struct</b></i> merits a dedicated mention, since I can envision several use-cases for storing only a specific GUI handle (for example, a <i><b>uipanel</b></i>, a specific graph, or a set of GUI controls&#8217; state). In this case, all we need to do is to call <i><b>handle2struct</b></i> with the requested parent handle, then <i><b>save</b></i> the returned structure. So simple, so powerful. <i><b>handle2struct</b></i> automatically returns all the non-default property information, recursively in all the handle&#8217;s children.<br />
Note that features that are not properties of displayed handles (camera position, 3D rotation/pan/zoom states, annotations, axes-linking etc.) are not processed by <i><b>handle2struct</b></i>. For storing the states of these features, you need to use some specific handling &#8211; see the code within %matlabroot%\toolbox\matlab\graphics\private\hgsaveStructDbl.m for details. Basically, hgsaveStructDbl.m reads the state of all these features and temporarily stores them in the base handle&#8217;s <strong>ApplicationData</strong> property; <i><b>handle2struct</b></i> then reads them as any other regular handle property data, and then hgsaveStructDbl.m clears the temporary data from the handle&#8217;s <strong>ApplicationData</strong>. We can use the same trick for any other application state, of course.</p>
<h3 id="struct2handle">struct2handle</h3>
<p><i><b>handle2struct</b></i> has a reverse function &#8211; the semi-documented <i><b>struct2handle</b></i>. I use it for creating dynamic preference panels: As in Matlab&#8217;s Preferences window, I have a list of preference topics and a set of corresponding options panels. In my case, it was easy to design each panel as a separate FIG file using GUIDE. In run-time, I simply load the relevant panel from its FIG file as described above, and place it onscreen in a dedicated <i><b>uipanel</b></i> using <i><b>struct2handle</b></i>. This enables very easy maintenance of preference panels, without sacrificing any functionality.<br />
<center><figure style="width: 450px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Matlab's preferences panels" src="https://undocumentedmatlab.com/images/FIG_MAT.png" title="Matlab's preferences panels" width="450" height="274" /><figcaption class="wp-caption-text">Matlab's preferences panels</figcaption></figure></center><br />
Figure menus and toolbars are not normally stored by <i><b>hgsave</b></i>, unless you use the optional &#8216;all&#8217; parameter (and correspondingly in <i><b>hgload</b></i>, if you choose to use it). <i><b>handle2struct</b></i> and <i><b>handle2struct</b></i> accept the same optional &#8216;all&#8217; parameter as <i><b>hgsave</b></i> and <i><b>hgload</b></i>. Unfortunately, a warning message indicates that this option will be discontinued in some future Matlab version.<br />
Which brings us to our final topic for today:</p>
<h3 id="Matlab8">Matlab 8: Boldly going where no FIG has gone before&#8230;</h3>
<p>Remember my post earlier this year about the <a target="_blank" href="/articles/matlab-hg2/">new HG2 mechanism</a>? I speculated that when MathWorks decides to release HG2, it will define this as a major Matlab release and label it Matlab 8.0.<br />
The source code in hgsave.m appears to confirm my speculation. Here is the relevant code section (slightly edited for clarity), which speaks for itself:</p>
<pre lang="matlab">
% Decide which save code path to use
if ~feature('HGUsingMatlabClasses')   % <== existing HG
    % Warn if user passed in 'all' flag
    if SaveAll
        warning( 'MATLAB:hgsave:DeprecatedOption', ...
            'The ''all'' option to hgsave will be removed in a future release.');
    end
    hgS = hgsaveStructDbl(h, SaveAll);
    SaveVer = '070000';
    SaveOldFig = true;
else   % <== HG2
    % Warn if user passed in 'all' flag
    if SaveAll
        warning( 'MATLAB:hgsave:DeprecatedOption', ...
            'The ''all'' option to hgsave has been removed.');
    end
    if SaveOldFig
        hgS = hgsaveStructClass(h);
        SaveVer = '080000';
    else
        hgO = hgsaveObject(h);
        SaveVer = '080000';
    end
end
% Revision encoded as 2 digits for major revision,
% 2 digits for minor revision, and 2 digits for
% patch revision.  This is the minimum revision
% required to fully support the file format.
% e.g. 070000 means 7.0.0
</pre>
<p>As can be seen, when Matlab starts using HG2 (perhaps in 2011?), the top-level structure node will be called "hgS_080000", indicating Matlab 8.0. QED.<br />
As a side-note, note that in HG2/Matlab8, although the comment about using 'all' indicates that it has been removed, in practice it is still accepted (although not being used). This will enable your code to be backward-compatible whenever HG2 launches, and future-compatible today.<br />
Have you used <i><b>handle2struct</b></i> or <i><b>struct2handle</b></i>? If so, please share your experience in a <a href="/articles/handle2struct-struct2handle-and-matlab-8/#respond">comment</a>.<br />
<em>Let the upcoming 2011 be a year filled with revelations, announcements and fulfillment! Happy New Year everybody!</em></p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/handle2struct-struct2handle-and-matlab-8">handle2struct, struct2handle &amp; Matlab 8.0</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/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 point as it returns with 100 or less points....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/fig-files-format" rel="bookmark" title="FIG files format">FIG files format </a> <small>FIG files are actually MAT files in disguise. This article explains how this can be useful in Matlab applications....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-layout-managers-uicontainer-and-relatives" rel="bookmark" title="Matlab layout managers: uicontainer and relatives">Matlab layout managers: uicontainer and relatives </a> <small>Matlab contains a few undocumented GUI layout managers, which greatly facilitate handling GUI components in dynamically-changing figures....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-hg2" rel="bookmark" title="Matlab&#039;s HG2 mechanism">Matlab&#039;s HG2 mechanism </a> <small>HG2 is presumably the next generation of Matlab graphics. This article tries to explore its features....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/handle2struct-struct2handle-and-matlab-8/feed</wfw:commentRss>
			<slash:comments>11</slash:comments>
		
		
			</item>
		<item>
		<title>uisplittool &#038; uitogglesplittool callbacks</title>
		<link>https://undocumentedmatlab.com/articles/uisplittool-uitogglesplittool-callbacks?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=uisplittool-uitogglesplittool-callbacks</link>
					<comments>https://undocumentedmatlab.com/articles/uisplittool-uitogglesplittool-callbacks#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 15 Dec 2010 18:00:35 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Callbacks]]></category>
		<category><![CDATA[Hidden property]]></category>
		<category><![CDATA[Toolbar]]></category>
		<category><![CDATA[uitools]]></category>
		<category><![CDATA[uiundo]]></category>
		<category><![CDATA[Undocumented function]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1999</guid>

					<description><![CDATA[<p>Matlab's undocumented uisplittool and uitogglesplittool are powerful toolbar controls - this article explains how to customize their behavior</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/uisplittool-uitogglesplittool-callbacks">uisplittool &amp; uitogglesplittool callbacks</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/uisplittool-uitogglesplittool" rel="bookmark" title="uisplittool &amp; uitogglesplittool">uisplittool &amp; uitogglesplittool </a> <small>Matlab's undocumented uisplittool and uitogglesplittool are powerful controls that can easily be added to Matlab toolbars - this article explains how...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-components" rel="bookmark" title="Figure toolbar components">Figure toolbar components </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events" rel="bookmark" title="Matlab callbacks for Java events">Matlab callbacks for Java events </a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/enabling-user-callbacks-during-zoom-pan" rel="bookmark" title="Enabling user callbacks during zoom/pan">Enabling user callbacks during zoom/pan </a> <small>Matlab zoom, pan and rotate3d modes hijack the user's figure callbacks, but this can be overridden. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Last week, I presented the undocumented <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> functions and showed how they can be added to a Matlab figure toolbar. Today I wish to conclude this topic by explaining how these controls can be customized with user-defined callbacks and pop-up menus.</p>
<h3 id="Callbacks">Callback functionality</h3>
<p>Both <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> have a <strong>Callback</strong> property, in addition to the standard <strong>ClickedCallback</strong> property that is available in <i><b>uipushtool</b></i>s and <i><b>uitoggletool</b></i>s.<br />
The standard <strong>ClickedCallback</strong> is invoked when the main button is clicked, while <strong>Callback</strong> is invoked when the narrow arrow button is clicked. <i><b>uitogglesplittool</b></i>, like <i><b>uitoggletool</b></i>, also has settable <strong>OnCallback</strong> and <strong>OffCallback</strong> callback properties.<br />
The accepted convention is that <strong>ClickedCallback</strong> should invoke the default control action (in our case, an Undo/Redo of the topmost <a target="_blank" href="/articles/uiundo-matlab-undocumented-undo-redo-manager/">uiundo action stack</a>), while <strong>Callback</strong> should display a drop-down of selectable actions.<br />
While this can be done programmatically using the <strong>Callback</strong> property, this functionality is already pre-built into <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> for our benefit. To access it, we need to get the control&#8217;s underlying Java component.<br />
Accessing the underlying Java component is normally done using the <a target="_blank" href="/articles/findjobj-find-underlying-java-object/">findjobj utility</a>, but in this case we have a shortcut: the control handle&#8217;s hidden <strong>JavaContainer</strong> property that holds the underlying <i>com.mathworks.hg.peer.SplitButtonPeer</i> (or <i>.ToggleSplitButtonPeer</i>) Java reference handle. This Java object&#8217;s <strong>MenuComponent</strong> property returns a reference to the control&#8217;s drop-down sub-component (which is a <i>com.mathworks.mwswing.MJPopupMenu</i> object):</p>
<pre lang="matlab">
>> jUndo = get(hUndo,'JavaContainer')
jUndo =
com.mathworks.hg.peer.SplitButtonPeer@f09ad5
>> jMenu = get(jUndo,'MenuComponent')  % or: =jUndo.getMenuComponent
jMenu =
com.mathworks.mwswing.MJPopupMenu[Dropdown Picker ButtonMenu,...]
</pre>
<p>Let&#8217;s add a few simple textual options:</p>
<pre lang="matlab">
jOption1 = jMenu.add('Option #1');
jOption1 = jMenu.add('Option #2');
set(jOption1, 'ActionPerformedCallback', 'disp(''option #1'')');
set(jOption2, 'ActionPerformedCallback', {@myCallbackFcn, extraData});
</pre>
<p><center><figure style="width: 310px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="setting uisplittool &amp; uitogglesplittool popup-menus" src="https://undocumentedmatlab.com/images/uisplittool3.png" title="setting uisplittool &amp; uitogglesplittool popup-menus" width="99" height="72" /><figcaption class="wp-caption-text">setting <i><b>uisplittool</b></i> &amp; <i><b>uitogglesplittool</b></i> popup-menus</figcaption></figure></center><br />
Popup-menus are described in more detail elsewhere (and in future articles). In the past I have already explained how icons and HTML markup can be added to menu items. Sub-menus can also be added.</p>
<h3 id="Example">A complete example</h3>
<p>Let&#8217;s now use this information, together with last year&#8217;s <a target="_blank" href="/articles/tag/uiundo/">set of articles</a> about Matlab&#8217;s undocumented <i><b>uiundo</b></i> functionality, to generate a complete and more realistic example, of undo/redo toolbar buttons.<br />
Undo and redo are actions that are particularly suited for <i><b>uisplittool</b></i>, since its main button enables us to easily undo/redo the latest action (like a simple toolbar button, by clicking the main <i><b>uisplittool</b></i> button) as well as select items from the actions drop-down (like a combo-box, by clicking the attached arrow button) – all this using a single component.</p>
<pre lang="matlab">
% Display our GUI
hEditbox = uicontrol('style','edit', 'position',[20,60,40,40]);
set(hEditbox, 'Enable','off', 'string','0');
hSlider = uicontrol('style','slider','userdata',hEditbox);
set(hSlider,'Callback',@test_uiundo);
% Display the figure toolbar that was hidden by the uicontrol function
set(gcf,'Toolbar','figure');
% Add the Undo/Redo buttons
hToolbar = findall(gcf,'tag','FigureToolBar');
hUndo = uisplittool('parent',hToolbar);
hRedo = uitogglesplittool('parent',hToolbar);
% Load the Redo icon
icon = fullfile(matlabroot,'/toolbox/matlab/icons/greenarrowicon.gif');
[cdata,map] = imread(icon);
% Convert white pixels into a transparent background
map(find(map(:,1)+map(:,2)+map(:,3)==3)) = NaN;
% Convert into 3D RGB-space
cdataRedo = ind2rgb(cdata,map);
cdataUndo = cdataRedo(:,[16:-1:1],:);
% Add the icon (and its mirror image = undo) to latest toolbar
set(hUndo, 'cdata',cdataUndo, 'tooltip','undo','Separator','on', ...
           'ClickedCallback','uiundo(gcbf,''execUndo'')');
set(hRedo, 'cdata',cdataRedo, 'tooltip','redo', ...
           'ClickedCallback','uiundo(gcbf,''execRedo'')');
% Re-arrange the Undo/Redo buttons
jToolbar = get(get(hToolbar,'JavaContainer'),'ComponentPeer');
jButtons = jToolbar.getComponents;
for buttonIdx = length(jButtons)-3 : -1 : 7  % end-to-front
   jToolbar.setComponentZOrder(jButtons(buttonIdx), buttonIdx+1);
end
jToolbar.setComponentZOrder(jButtons(end-2), 5);    % Separator
jToolbar.setComponentZOrder(jButtons(end-1), 6);    % Undo
jToolbar.setComponentZOrder(jButtons(end), 7);      % Redo
jToolbar.revalidate;
% Retrieve redo/undo object
undoObj = getappdata(gcf,'uitools_FigureToolManager');
if isempty(undoObj)
   undoObj = uitools.FigureToolManager(gcf);
   setappdata(gcf,'uitools_FigureToolManager',undoObj);
end
% Populate Undo actions drop-down list
jUndo = get(hUndo,'JavaContainer');
jMenu = get(jUndo,'MenuComponent');
undoActions = get(undoObj.CommandManager.UndoStack,'Name');
jMenu.removeAll;
for actionIdx = length(undoActions) : -1 : 1    % end-to-front
    jActionItem = jMenu.add(undoActions(actionIdx));
    set(jActionItem, 'ActionPerformedCallback', @myUndoCallbackFcn);
end
jToolbar.revalidate;
% Drop-down callback function
function myUndoCallbackFcn(jActionItem,hEvent)
    % user processing needs to be placed here
end  % myUndoCallbackFcn
</pre>
<p><center><figure style="width: 395px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="undo/redo buttons implemented using uisplittool" src="https://undocumentedmatlab.com/images/uisplittool4b.png" title="undo/redo buttons implemented using uisplittool" width="395" height="257" /><figcaption class="wp-caption-text">undo/redo buttons implemented using <i><b>uisplittool</b></i></figcaption></figure></center><br />
In a real-world application, the code-segment above that populated the drop-down list would be placed within the slider&#8217;s <em>test_uiundo()</em> callback function, and we would set a similar drop-down for the hRedu button. In addition, we would dynamically modify the button tooltips. As a final customization, we could modify the figure&#8217;s main menu. Menu customization will be discussed in a future separate set of articles.<br />
Have you used <i><b>uisplittool</b></i> or <i><b>uitogglesplittool</b></i> in your GUI? If so, please tell us what use you have made of them, in a comment <a href="/articles/uisplittool-uitogglesplittool-callbacks/#respond">below</a>. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/uisplittool-uitogglesplittool-callbacks">uisplittool &amp; uitogglesplittool callbacks</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/uisplittool-uitogglesplittool" rel="bookmark" title="uisplittool &amp; uitogglesplittool">uisplittool &amp; uitogglesplittool </a> <small>Matlab's undocumented uisplittool and uitogglesplittool are powerful controls that can easily be added to Matlab toolbars - this article explains how...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-components" rel="bookmark" title="Figure toolbar components">Figure toolbar components </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events" rel="bookmark" title="Matlab callbacks for Java events">Matlab callbacks for Java events </a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/enabling-user-callbacks-during-zoom-pan" rel="bookmark" title="Enabling user callbacks during zoom/pan">Enabling user callbacks during zoom/pan </a> <small>Matlab zoom, pan and rotate3d modes hijack the user's figure callbacks, but this can be overridden. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/uisplittool-uitogglesplittool-callbacks/feed</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>uisplittool &#038; uitogglesplittool</title>
		<link>https://undocumentedmatlab.com/articles/uisplittool-uitogglesplittool?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=uisplittool-uitogglesplittool</link>
					<comments>https://undocumentedmatlab.com/articles/uisplittool-uitogglesplittool#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 09 Dec 2010 00:06:33 +0000</pubDate>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[Figure window]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[Undocumented function]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<category><![CDATA[Toolbar]]></category>
		<category><![CDATA[uitools]]></category>
		<category><![CDATA[uiundo]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1994</guid>

					<description><![CDATA[<p>Matlab's undocumented uisplittool and uitogglesplittool are powerful controls that can easily be added to Matlab toolbars - this article explains how</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/uisplittool-uitogglesplittool">uisplittool &amp; uitogglesplittool</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/uisplittool-uitogglesplittool-callbacks" rel="bookmark" title="uisplittool &amp; uitogglesplittool callbacks">uisplittool &amp; uitogglesplittool callbacks </a> <small>Matlab's undocumented uisplittool and uitogglesplittool are powerful toolbar controls - this article explains how to customize their behavior...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-components" rel="bookmark" title="Figure toolbar components">Figure toolbar components </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/toolbar-button-labels" rel="bookmark" title="Toolbar button labels">Toolbar button labels </a> <small>GUI toolbar button labels can easily be set and customized using underlying Java components. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uiundo-matlab-undocumented-undo-redo-manager" rel="bookmark" title="uiundo &#8211; Matlab&#039;s undocumented undo/redo manager">uiundo &#8211; Matlab&#039;s undocumented undo/redo manager </a> <small>The built-in uiundo function provides easy yet undocumented access to Matlab's powerful undo/redo functionality. This article explains its usage....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Matlab 7.6 (R2008a) and onward contain a reference to <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> in the javacomponent.m and %matlabroot%/bin/registry/hg.xml files. These are reported as built-in functions by the <i><b>which</b></i> function, although they have no corresponding m-file as other similar built-in functions (note the double &#8216;t&#8217;, as in <i>split-tool</i>):</p>
<pre lang="matlab">
>> which uisplittool
built-in (C:\Matlab\R2010b\toolbox\matlab\uitools\uisplittool)
</pre>
<p>These uitools are entirely undocumented, even today (R2010b). They puzzled me for a very long time. An acute reader (Jeremy Raymonds) suggested they are related to toolbars, like other uitools such as the <i><b>uipushtool</b></i> and <i><b>uitoggletool</b></i>. This turned out to be the missing clue that unveiled these useful tools:</p>
<h3 id="Intro">So what are <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i>?</h3>
<p>Both <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> are basic Handle-Graphics building blocks used in Matlab toolbars, similarly to the well-documented <i><b>uipushtool</b></i> and <i><b>uitoggletool</b></i>.<br />
<i><b>uisplittool</b></i> presents a simple drop-down, whereas <i><b>uitogglesplittool</b></i> presents a drop-down that is also selectable.<br />
The Publish and Run controls on the Matlab Editor&#8217;s toolbar are examples of <i><b>uisplittool</b></i>, and so are the Brush / Select-Data control on the figure toolbar, and the plot-selection drop-down on the Matlab Desktop&#8217;s Workspace toolbar:<br />
<center><figure style="width: 361px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="uisplittool in action in the Matlab Desktop" src="https://undocumentedmatlab.com/images/uisplittool.png" title="uisplittool in action in the Matlab Desktop" width="361" height="221" /><figcaption class="wp-caption-text"><i><b>uisplittool</b></i> in action in the Matlab Desktop</figcaption></figure></center></p>
<h3 id="Appearance">Adding <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> to a toolbar</h3>
<p>Adding a <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> to a toolbar is done in a similar manner to adding <i><b>uipushtool</b></i>s and <i><b>uitoggletool</b></i>s:</p>
<pre lang="matlab">
hToolbar = findall(gcf,'tag','FigureToolBar');
hUndo=uisplittool('parent',hToolbar);       % uisplittool
hRedo=uitogglesplittool('parent',hToolbar); % uitogglesplittool
</pre>
<p>Like <i><b>uipushtool</b></i> and <i><b>uitoggletool</b></i>, <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> also have unique <strong>Type</strong> property values, &#8216;uisplittool&#8217; and &#8216;uitogglesplittool&#8217; respectively. The handles can also be tested using the built-in <i><b>isa</b></i> function:</p>
<pre lang="matlab">
>> isa(handle(hUndo),'uisplittool')   % or: 'uitogglesplittool'
ans =
     1
>> class(handle(hUndo))
ans =
uisplittool
</pre>
<p>Just as with <i><b>uipushtool</b></i>s and <i><b>uitoggletool</b></i>s, the new buttons have an empty button-face appearance, until we fix their <strong>CData</strong>, <strong>Tooltip</strong> and similar settable properties:</p>
<pre lang="matlab">
% Load the Redo icon
icon = fullfile(matlabroot,'/toolbox/matlab/icons/greenarrowicon.gif');
[cdata,map] = imread(icon);
% Convert white pixels into a transparent background
map(find(map(:,1)+map(:,2)+map(:,3)==3)) = NaN;
% Convert into 3D RGB-space
cdataRedo = ind2rgb(cdata,map);
cdataUndo = cdataRedo(:,[16:-1:1],:);
% Add the icon (and its mirror image = undo) to latest toolbar
set(hUndo, 'cdata',cdataUndo, 'tooltip','undo','Separator','on', ...
           'ClickedCallback','uiundo(gcbf,''execUndo'')');
set(hRedo, 'cdata',cdataRedo, 'tooltip','redo', ...
           'ClickedCallback','uiundo(gcbf,''execRedo'')');
</pre>
<p><center><figure style="width: 450px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="User-created uisplittool &amp; uitogglesplittool toolbar buttons" src="https://undocumentedmatlab.com/images/uisplittool2b2.png" title="User-created uisplittool &amp; uitogglesplittool toolbar buttons" width="450" height="107" /><figcaption class="wp-caption-text">User-created <i><b>uisplittool</b></i> &amp; <i><b>uitogglesplittool</b></i> toolbar buttons</figcaption></figure></center><br />
Note that the controls can be created with these properties in a single command:</p>
<pre lang="matlab">hUndo = uisplittool('parent',hToolbar, 'cdata',cdataRedo, ...);</pre>
<h3 id="Arranging">Re-arranging the toolbar controls placement</h3>
<p>Let us now re-arrange our toolbar buttons. Unfortunately, a bug causes <i><b>uisplittool</b></i>s and <i><b>uitogglesplittool</b></i>s to always be placed flush-left when the toolbar&#8217;s children are re-arranged (anyone at TMW reading this in time for the R2011a bug-parade selection?).<br />
So, we can&#8217;t re-arrange the buttons at the HG-children level. Luckily, we can re-arrange directly at the Java level (note that until now, the entire discussion of <i><b>uisplittool</b></i> and <i><b>uitogglesplittool</b></i> was purely Matlab-based):</p>
<pre lang="matlab">
jToolbar = get(get(hToolbar,'JavaContainer'),'ComponentPeer');
jButtons = jToolbar.getComponents;
for buttonId = length(jButtons)-3 : -1 : 7  % end-to-front
   jToolbar.setComponentZOrder(jButtons(buttonId), buttonId+1);
end
jToolbar.setComponentZOrder(jButtons(end-2), 5);   % Separator
jToolbar.setComponentZOrder(jButtons(end-1), 6);   % Undo
jToolbar.setComponentZOrder(jButtons(end), 7);     % Redo
jToolbar.revalidate;
</pre>
<p><center><figure style="width: 450px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Re-arranged uisplittool &amp; uitogglesplittool toolbar buttons" src="https://undocumentedmatlab.com/images/uisplittool2c2.png" title="Re-arranged uisplittool &amp; uitogglesplittool toolbar buttons" width="450" height="106" /><figcaption class="wp-caption-text">Re-arranged <i><b>uisplittool</b></i> &amp; <i><b>uitogglesplittool</b></i> toolbar buttons<br />(not as simple as it may sound)</figcaption></figure></center><br />
Next week, I will combine the information in this article, with <a target="_blank" href="/articles/tag/uiundo/">last year&#8217;s articles</a> about <i><b>uiundo</b></i>, and show how we can create a dynamic figure toolbar drop-down of undo/redo events. Here is a preview to whet your appetite:<br />
<center><figure style="width: 450px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="undo/redo buttons implemented using uisplittool" src="https://undocumentedmatlab.com/images/uisplittool4.png" title="undo/redo buttons implemented using uisplittool" width="450" height="226" /><figcaption class="wp-caption-text">undo/redo buttons implemented using <i><b>uisplittool</b></i></figcaption></figure></center></p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/uisplittool-uitogglesplittool">uisplittool &amp; uitogglesplittool</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/uisplittool-uitogglesplittool-callbacks" rel="bookmark" title="uisplittool &amp; uitogglesplittool callbacks">uisplittool &amp; uitogglesplittool callbacks </a> <small>Matlab's undocumented uisplittool and uitogglesplittool are powerful toolbar controls - this article explains how to customize their behavior...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-toolbar-components" rel="bookmark" title="Figure toolbar components">Figure toolbar components </a> <small>Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/toolbar-button-labels" rel="bookmark" title="Toolbar button labels">Toolbar button labels </a> <small>GUI toolbar button labels can easily be set and customized using underlying Java components. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uiundo-matlab-undocumented-undo-redo-manager" rel="bookmark" title="uiundo &#8211; Matlab&#039;s undocumented undo/redo manager">uiundo &#8211; Matlab&#039;s undocumented undo/redo manager </a> <small>The built-in uiundo function provides easy yet undocumented access to Matlab's powerful undo/redo functionality. This article explains its usage....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/uisplittool-uitogglesplittool/feed</wfw:commentRss>
			<slash:comments>11</slash:comments>
		
		
			</item>
		<item>
		<title>Modifying default toolbar/menubar actions</title>
		<link>https://undocumentedmatlab.com/articles/modifying-default-toolbar-menubar-actions?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=modifying-default-toolbar-menubar-actions</link>
					<comments>https://undocumentedmatlab.com/articles/modifying-default-toolbar-menubar-actions#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 02 Jun 2010 14:08:19 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[Handle graphics]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Menubar]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<category><![CDATA[Toolbar]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1430</guid>

					<description><![CDATA[<p>The default Matlab figure toolbar and menu actions can easily be modified using simple pure-Matlab code. This article explains how.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/modifying-default-toolbar-menubar-actions">Modifying default toolbar/menubar actions</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/customizing-standard-figure-toolbar-menubar" rel="bookmark" title="Customizing the standard figure toolbar, menubar">Customizing the standard figure toolbar, menubar </a> <small>The standard figure toolbar and menubar can easily be modified to include a list of recently-used files....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background" rel="bookmark" title="Customizing figure toolbar background">Customizing figure toolbar background </a> <small>Setting the figure toolbar's background color can easily be done using just a tiny bit of Java magic powder. This article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/toolbar-button-labels" rel="bookmark" title="Toolbar button labels">Toolbar button labels </a> <small>GUI toolbar button labels can easily be set and customized using underlying Java components. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/getting-default-hg-property-values" rel="bookmark" title="Getting default HG property values">Getting default HG property values </a> <small>Matlab has documented how to modify default property values, but not how to get the full list of current defaults. This article explains how to do this. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Did you ever wish to modify Matlab&#8217;s default toolbar/menubar items?<br />
I recently consulted to a client who needed to modify the default behavior of the <i><b>legend</b></i> action in the toolbar, and the corresponding item on the main menu (Insert / Legend). The official version is that only user-added items can be customized, whereas standard Matlab items cannot.<br />
Luckily, this can indeed be done using simple pure-Matlab code. The trick is to get the handles of the toolbar/menubar objects and then to modify their callback properties.<br />
<center><figure style="width: 463px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Default legend insertion action" src="https://undocumentedmatlab.com/images/Toolbar_Menubar_actions1.png" title="Default legend insertion action" width="463" height="265" /><figcaption class="wp-caption-text">Default legend insertion action</figcaption></figure></center></p>
<h3 id="Item-handles">Using the toolbar/menubar item handles</h3>
<p>Getting the handles can be done in several different ways. In my experience, using the object&#8217;s tag string is often easiest, fastest and more maintainable. Note that the Matlab toolbar and main menu handles are normally hidden (<b>HandleVisibility</b> = &#8216;off&#8217;). Therefore, in order to access them we need to use the <i><b>findall</b></i> function rather than the better-known <i><b>findobj</b></i> function. Also note that to set the callbacks we need to access differently-named properties: <b>ClickedCallback</b> for toolbar buttons, and <b>Callback</b> for menu items:</p>
<pre lang="matlab">
hToolLegend = findall(gcf,'tag','Annotation.InsertLegend');
set(hToolLegend, 'ClickedCallback',{@cbLegend,hFig,myData});
hMenuLegend = findall(gcf,'tag','figMenuInsertLegend');
set(hMenuLegend, 'Callback',{@cbLegend,hFig,myData});
</pre>
<p>As a side note, two undocumented/unsupported functions,  <i><b>uitool</b>(hFig,tagName)</i> and <i><b>uigettoolbar</b></i>, also retrieve toolbar items. Since using <i><b>findall</b></i> is so simple and more supported, I suggest using the <i><b>findall</b></i> approach.<br />
To see the list of available toolbar/menubar tags, use the following code snippet:</p>
<pre lang="matlab">
% Toolbar tag names:
>> hToolbar = findall(gcf,'tag','FigureToolBar');
>> get(findall(hToolbar),'tag')
ans =
    'FigureToolBar'
    'Plottools.PlottoolsOn'
    'Plottools.PlottoolsOff'
    'Annotation.InsertLegend'
    'Annotation.InsertColorbar'
    'DataManager.Linking'
    'Exploration.Brushing'
    'Exploration.DataCursor'
    'Exploration.Rotate'
    'Exploration.Pan'
    'Exploration.ZoomOut'
    'Exploration.ZoomIn'
    'Standard.EditPlot'
    'Standard.PrintFigure'
    'Standard.SaveFigure'
    'Standard.FileOpen'
    'Standard.NewFigure'
    ''
% Menu-bar tag names:
>> get(findall(gcf,'type','uimenu'),'tag')
ans =
    'figMenuHelp'
    'figMenuWindow'
    'figMenuDesktop'
    'figMenuTools'
    'figMenuInsert'
    'figMenuView'
    'figMenuEdit'
    'figMenuFile'
    'figMenuHelpAbout'
    'figMenuHelpPatens'
    'figMenuHelpTerms'
    'figMenuHelpActivation'
    'figMenuDemos'
    'figMenuTutorials'
    'figMenuHelpUpdates'
    'figMenuGetTrials'
    'figMenuWeb'
    'figMenuHelpPrintingExport'
    'figMenuHelpAnnotatingGraphs'
    'figMenuHelpPlottingTools'
    'figMenuHelpGraphics'
    ''
    ''                    < = Note the missing tag names...
    ''
    'figMenuToolsBFDS'    <= note the duplicate tag names...
    'figMenuToolsBFDS'
    'figDataManagerBrushTools'
    'figMenuToolsAlign'
    'figMenuToolsAlign'
    ... (plus many many more...)
</pre>
<p>Unfortunately, as seen above, Matlab developers forgot to assign tags to some default toolbar/menubar items. Luckily, in my particular case, both legend handles (toolbar, menu) have valid tag names that can be used: 'Annotation.InsertLegend' and 'figMenuInsertLegend'.<br />
If an item's tag is missing, you can always find its handle using its <b>Parent</b>, <b>Label</b>, <b>Tooltip</b> or <b>Callback</b> properties. For example:</p>
<pre lang="matlab">hPrintMenuItem = get(findall(gcf,'Label','&Print...'));</pre>
<p>Note that property values, such as the tag names or labels, are unsupported and undocumented. They may change without warning or notice between Matlab releases, and have indeed done so in the past. Therefore, if your code needs to be compatible with older Matlab releases, ensure that you cover all the possible property values, or use a different way to access the handles. While <u>using</u> the tag name or label as shown above is considered &#8220;Low risk&#8221; (I don&#8217;t expect it to break in the near future), their actual <u>values</u> should be considered somewhat more risky and we should therefore code defensively.</p>
<h3 id="Print">Another simple example: Print Preview</h3>
<p>As another simple and very useful example, let&#8217;s modify the default Print action (and tooltip) on the figure toolbar to display the Print-Preview window rather than send the figure directly to the printer:<br />
<center><figure style="width: 463px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Matlab's default toolbar Print action" src="https://undocumentedmatlab.com/images/Toolbar_Menubar_actions2b.png" title="Matlab's default toolbar Print action" width="463" height="108" /><figcaption class="wp-caption-text">Matlab's default toolbar Print action</figcaption></figure></center></p>
<pre lang="matlab">
hToolbar = findall(gcf,'tag','FigureToolBar');
hPrintButton = findall(hToolbar,'tag','Standard.PrintFigure');
set(hPrintButton, 'ClickedCallback','printpreview(gcbf)', 'TooltipString','Print Preview');
</pre>
<p><center><figure style="width: 450px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Matlab's Print Preview window" src="https://undocumentedmatlab.com/images/PrintPreview.png" title="Matlab's Print Preview window" width="450" height="418" /><figcaption class="wp-caption-text">Matlab's Print Preview window</figcaption></figure></center><br />
More advanced customization of the toolbar and menu items are possible, but require a bit of Java code. Examples of toolbar customizations were presented in past articles (<a target="_blank" href="/articles/figure-toolbar-components/">here</a> and <a target="_blank" href="/articles/figure-toolbar-customizations/">here</a>). A future article will explain how to customize menu items.<br />
Have you found a nifty way to customize the menubar or toolbar? If so, please share it in the <a href="#respond">comments section</a> below.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/modifying-default-toolbar-menubar-actions">Modifying default toolbar/menubar actions</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/customizing-standard-figure-toolbar-menubar" rel="bookmark" title="Customizing the standard figure toolbar, menubar">Customizing the standard figure toolbar, menubar </a> <small>The standard figure toolbar and menubar can easily be modified to include a list of recently-used files....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background" rel="bookmark" title="Customizing figure toolbar background">Customizing figure toolbar background </a> <small>Setting the figure toolbar's background color can easily be done using just a tiny bit of Java magic powder. This article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/toolbar-button-labels" rel="bookmark" title="Toolbar button labels">Toolbar button labels </a> <small>GUI toolbar button labels can easily be set and customized using underlying Java components. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/getting-default-hg-property-values" rel="bookmark" title="Getting default HG property values">Getting default HG property values </a> <small>Matlab has documented how to modify default property values, but not how to get the full list of current defaults. This article explains how to do this. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/modifying-default-toolbar-menubar-actions/feed</wfw:commentRss>
			<slash:comments>9</slash:comments>
		
		
			</item>
	</channel>
</rss>
