<?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>uiundo &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/uiundo/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Wed, 15 Dec 2010 18:00:35 +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>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 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 fetchpriority="high" 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 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>Customizing uiundo</title>
		<link>https://undocumentedmatlab.com/articles/customizing-uiundo?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=customizing-uiundo</link>
					<comments>https://undocumentedmatlab.com/articles/customizing-uiundo#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 05 Nov 2009 00:29:13 +0000</pubDate>
				<category><![CDATA[Handle graphics]]></category>
		<category><![CDATA[Hidden property]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Menubar]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<category><![CDATA[uitools]]></category>
		<category><![CDATA[uiundo]]></category>
		<category><![CDATA[Undocumented function]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=708</guid>

					<description><![CDATA[<p>This article describes how Matlab's undocumented uiundo undo/redo manager can be customized</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-uiundo">Customizing uiundo</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/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>
<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/customizing-contour-plots-part-2" rel="bookmark" title="Customizing contour plots part 2">Customizing contour plots part 2 </a> <small>The contour lines of 3D Matlab plot can be customized in many different ways. This is the 2nd article on this issue. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-print-setup" rel="bookmark" title="Customizing print setup">Customizing print setup </a> <small>Matlab figures print-setup can be customized to automatically prepare the figure for printing in a specific configuration...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Last week I discussed <a target="_blank" href="/articles/uiundo-matlab-undocumented-undo-redo-manager/"><b><i>uiundo</i></b> &#8211; Matlab&#8217;s undocumented undo/redo manager</a>. Today, I will show how this object can be customized for some specific needs. However, we first need to understand a little more about how <b><i>uiundo</i></b> works beneath the hood.<br />
Matlab stores all of a figure&#8217;s undo/redo data in a hidden figure object, referenced by <b><i>getappdata</i></b>(hFig,&#8217;uitools_FigureToolManager&#8217;). This means that by default <b><i>uiundo</i></b> works at the figure level, rather than the application level or the GUI component level. If we wish to modify this default behavior, we need to programmatically inspect and filter the undo/redo actions stack based on the action source. Read below to see how this can be done.<br />
The hidden <i>uitools_FigureToolManager</i> object, defined in %MATLABROOT%\toolbox\matlab\uitools\@uiundo\, uses a stack to store instances of the undo/redo <i>cmd</i> data structure introduced in last week&#8217;s post:</p>
<pre lang="matlab">
% Retrieve redo/undo object
undoObj = getappdata(hFig,'uitools_FigureToolManager');
if isempty(undoObj)
   try
      % R2014a and earlier
      undoObj = uitools.FigureToolManager(hFig);
   catch
      % R2014b and newer
      undoObj = matlab.uitools.internal.FigureToolManager(hFig);
   end
   setappdata(hFig,'uitools_FigureToolManager',undoObj);
end
>> get(undoObj)
    CommandManager: [1x1 uiundo.CommandManager]
            Figure: [1x1 figure]
        UndoUIMenu: [1x1 uimenu]
        RedoUIMenu: [1x1 uimenu]
</pre>
<p>There are several interesting things we can do with this undoObj. First, let&#8217;s modify the main-menu items (I will discuss menu customization in more detail in another post):</p>
<pre lang="matlab">
% Modify the main menu item (similarly for redo/undo)
if ~isempty(undoObj.RedoUIMenu)
   undoObj.RedoUIMenu.Position =1; %default=2 (undo above redo)
   undoObj.RedoUIMenu.Enable = 'off';     % default='on'
   undoObj.RedoUIMenu.Checked = 'on';     % default='off'
   undoObj.RedoUIMenu.ForegroundColor = [1,0,0];  % =red
end
if ~isempty(undoObj.UndoUIMenu)
   undoObj.UndoUIMenu.Label = '<html><b><i>Undo action';
   % Note: &Undo underlines 'U' and adds a keyboard accelerator
   % but unfortunately only if the label is non-HTML ...
   undoObj.UndoUIMenu.Separator = 'on';   % default='off'
   undoObj.UndoUIMenu.Checked = 'on';     % default='off'
   undoObj.UndoUIMenu.ForegroundColor = 'blue'; % default=black
end
</i></b></html></pre>
<p><center><figure style="width: 284px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Menu before customization" src="https://undocumentedmatlab.com/images/uiundo2a.png" title="Menu before customization" width="284" height="166" /></p>
<p>
<img loading="lazy" decoding="async" alt="Menu after customization" src="https://undocumentedmatlab.com/images/uiundo3a.png" title="Menu after customization" width="284" height="166" /><figcaption class="wp-caption-text">Figure menu before and after customization</figcaption></figure></p>
<p></center><br />
Now, let&#8217;s take a look at undoObj&#8217;s <i>CommandManager</i> child (the <i>Figure</i> child object is simply <i><b>handle</b>(hFig)</i>, and so is not very interesting):</p>
<pre lang="matlab">
>> undoObj.CommandManager.get
             UndoStack: [13x1 uiundo.FunctionCommand]
             RedoStack: [1x1 uiundo.FunctionCommand]
    MaxUndoStackLength: []
               Verbose: []
>> undoObj.CommandManager.UndoStack(end).get
             Parent: []
       MCodeComment: []
               Name: 'slider update (0.48 to 0.38)'
           Function: @internal_update
           Varargin: {[53.0037841796875]  [0.38]  [1x1 double]}
    InverseFunction: @internal_update
    InverseVarargin: {[53.0037841796875]  [0.48]  [1x1 double]}
</pre>
<p>This looks familiar: In fact, it is exactly the <i>cmd</i> data structure being passed to the <b><i>uiundo</i></b> function, with the additional (apparently unused) properties <i>Parent</i> and <i>MCodeComment</i>. <i>CommandManager</i>&#8216;s <i>UndoStack</i> and <i>RedoStack</i> child objects contain all stored undo/redo actions such that the latest action is at the end of these arrays. In the snippet above, there are 13 undo-able actions, with the latest action in <i>UndoStack(end)</i>. <i>UndoStack</i> and <i>RedoStack</i> have the same structure:</p>
<ul>
<li><i>Name</i> contains the action description (presented in the figure&#8217;s menu)</li>
<li><i>Function</i> is the function handle that will be invoked if the action is <b>re</b>done</li>
<li><i>Varargin</i> are the arguments passed to <i>Function</i> during redo</li>
<li><i>InverseFunction</i> is the function handle that will be invoked if the action is <b>un</b>done</li>
<li><i>InverseVarargin</i> are the arguments passed to <i>InverseFunction</i> during undo</li>
<li><i>Parent</i> and <i>MCodeComment</i> &#8211; I could not determine what these are used for
</li>
</ul>
<p>We can inspect the latest undo/redo actions, without activating them, by using <i>CommandManager</i>&#8216;s <i>peekundo()</i> and <i>peekredo()</i> methods (which return empty [] if no undo/redo action is available):</p>
<pre lang="matlab">
>> undoObj.CommandManager.peekredo.get % first check if isempty
             Parent: []
       MCodeComment: []
               Name: 'slider update (0.38 to 0.28)'
           Function: @internal_update
           Varargin: {[53.0037841796875]  [0.28]  [1x1 double]}
    InverseFunction: @internal_update
    InverseVarargin: {[53.0037841796875]  [0.38]  [1x1 double]}
>> undoObj.CommandManager.peekundo.get
             Parent: []
       MCodeComment: []
               Name: 'slider update (0.48 to 0.38)'
           Function: @internal_update
           Varargin: {[53.0037841796875]  [0.38]  [1x1 double]}
    InverseFunction: @internal_update
    InverseVarargin: {[53.0037841796875]  [0.48]  [1x1 double]}
>> undoObj.CommandManager.peekundo.Name
ans =
slider update (0.48 to 0.38)
</pre>
<p>We can undo/redo the latest action (last element of the <i>UndoStack</i>/<i>RedoStack</i>) by invoking <i>CommandManager</i>&#8216;s <i>undo()</i>/<i>redo()</i> methods. This is actually what <b><i>uiundo</i></b> is doing behind the scenes when it is called with the &#8216;execUndo&#8217; and &#8216;execRedo&#8217; arguments:</p>
<pre lang="matlab">
undoObj.CommandManager.undo;
undoObj.CommandManager.redo;
</pre>
<p>We can clear the entire actions stack by using <i>CommandManager</i>&#8216;s <i>empty()</i> method. This can be useful, for example, after a &#8216;Save&#8217; or &#8216;Apply&#8217; operation in our GUI:</p>
<pre lang="matlab">
undoObj.CommandManager.empty;
</pre>
<p>If we set <i>CommandManager</i>&#8216;s <i>Verbose</i> property to any non-empty value, debug information is spilled onto the Command Window when new <b><i>uiundo</i></b> actions are added:</p>
<pre lang="matlab">
>> undoObj.CommandManager.Verbose = 1;
% now move the slider and see the debug info below:
internal_update(h_uicontrol, [0.48,], h_uicontrol); % Called by slider update (0.28 to 0.48)
internal_update(h_uicontrol, [0.58,], h_uicontrol); % Called by slider update (0.48 to 0.58)
</pre>
<p>Finally, <i>CommandManager</i> uses its <i>MaxUndoStackLength</i> property to limit the size of the undo/redo stacks. This property is defined as read-only in %matlabroot%\toolbox\matlab\uitools\@uiundo\@CommandManager\schema.m line #12, so if you wish to programmatically modify this property from its default value of empty (=unlimited), you will need to comment out that line.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-uiundo">Customizing uiundo</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/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>
<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/customizing-contour-plots-part-2" rel="bookmark" title="Customizing contour plots part 2">Customizing contour plots part 2 </a> <small>The contour lines of 3D Matlab plot can be customized in many different ways. This is the 2nd article on this issue. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-print-setup" rel="bookmark" title="Customizing print setup">Customizing print setup </a> <small>Matlab figures print-setup can be customized to automatically prepare the figure for printing in a specific configuration...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/customizing-uiundo/feed</wfw:commentRss>
			<slash:comments>9</slash:comments>
		
		
			</item>
		<item>
		<title>uiundo &#8211; Matlab&#039;s undocumented undo/redo manager</title>
		<link>https://undocumentedmatlab.com/articles/uiundo-matlab-undocumented-undo-redo-manager?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=uiundo-matlab-undocumented-undo-redo-manager</link>
					<comments>https://undocumentedmatlab.com/articles/uiundo-matlab-undocumented-undo-redo-manager#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 29 Oct 2009 22:11:11 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Undocumented function]]></category>
		<category><![CDATA[Callbacks]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<category><![CDATA[schema.class]]></category>
		<category><![CDATA[Toolbar]]></category>
		<category><![CDATA[uitools]]></category>
		<category><![CDATA[uiundo]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=688</guid>

					<description><![CDATA[<p>The built-in uiundo function provides easy yet undocumented access to Matlab's powerful undo/redo functionality. This article explains its usage.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/uiundo-matlab-undocumented-undo-redo-manager">uiundo &#8211; Matlab&#039;s undocumented undo/redo manager</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-uiundo" rel="bookmark" title="Customizing uiundo">Customizing uiundo </a> <small>This article describes how Matlab's undocumented uiundo undo/redo manager can be customized...</small></li>
<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/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/undocumented-cursorbar-object" rel="bookmark" title="Undocumented cursorbar object">Undocumented cursorbar object </a> <small>Matlab's internal undocumented graphics.cursorbar object can be used to present dynamic data-tip cross-hairs...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Whenever we have a Matlab GUI containing user-modifiable controls (edit boxes, sliders, toggle buttons etc.), we may wish to include an undo/redo feature. This would normally be a painful programming task. Luckily, there is an undocumented built-in Matlab support for this functionality via the <b><i>uiundo</i></b> function. Note that <b><i>uiundo</i></b> and its functionality is <u>not</u> Java-based but rather uses Matlab&#8217;s classes and the similarly-undocumented <b><i>schema</i></b>-based object-oriented approach.<br />
A couple of months ago, I explained <a target="_blank" href="/articles/figure-toolbar-components/">how to customize the figure toolbar</a>. In that article, I used the undocumented <b><i>uiundo</i></b> function as a target for the toolbar customization and promised to explain its functionality later. I would now like to explain <b><i>uiundo</i></b> and its usage.<br />
The <b><i>uiundo</i></b> function is basically an accessor for Matlab&#8217;s built-in undo/redo manager object. It is located in the <i>uitools</i> folder (%MATLABROOT%\toolbox\matlab\uitools) and its <i>@uiundo</i> sub-folder. To use <b><i>uiundo</i></b>, simply define within each uicontrol&#8217;s callback function (where we normally place our application GUI logic) the name of the undo/redo action, what should be done to undo the action, and what should be done if the user wished to redo the action after undoing it. <b><i>uiundo</i></b> then takes care of adding this data to the figure&#8217;s undo/redo options under Edit in the main figure menu.<br />
For example, let&#8217;s build a simple GUI consisting of a slider that controls the value of an edit box:</p>
<pre lang="matlab">
hEditbox = uicontrol('style','edit', 'position',[20,60,40,40]);
set(hEditbox, 'Enable','off', 'string','0');
hSlider = uicontrol('style','slider','userdata',hEditbox);
callbackStr = 'set(get(gcbo,''userdata''),''string'',num2str(get(gcbo,''value'')))';
set(hSlider,'Callback',callbackStr);
</pre>
<p><center><figure style="width: 284px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Simple GUI with slider update of a numeric value" src="https://undocumentedmatlab.com/images/uiundo1.png" title="Simple GUI with slider update of a numeric value" width="284" height="166" /><figcaption class="wp-caption-text">Simple GUI with slider update of a numeric value</figcaption></figure></center><br />
Now, let&#8217;s attach undo/redo actions to the slider&#8217;s callback. First, place the following in test_uiundo.m:</p>
<pre lang="matlab">
% Main callback function for slider updates
function test_uiundo(varargin)
  % Update the edit box with the new value
  hEditbox = get(gcbo,'userdata');
  newVal = get(gcbo,'value');
  set(hEditbox,'string',num2str(newVal));
  % Retrieve and update the stored previous value
  oldVal = getappdata(gcbo,'oldValue');
  if isempty(oldVal),  oldVal=0;  end
  setappdata(gcbo,'oldValue',newVal);
  % Prepare an undo/redo action
  cmd.Name = sprintf('slider update (%g to %g)',oldVal,newVal);
  % Note: the following is not enough since it only
  %       updates the slider and not the editbox...
  %cmd.Function        = @set;                  % Redo action
  %cmd.Varargin        = {gcbo,'value',newVal};
  %cmd.InverseFunction = @set;                  % Undo action
  %cmd.InverseVarargin = {gcbo,'value',oldVal};
  % This takes care of the update problem...
  cmd.Function        = @internal_update;       % Redo action
  cmd.Varargin        = {gcbo,newVal,hEditbox};
  cmd.InverseFunction = @internal_update;       % Undo action
  cmd.InverseVarargin = {gcbo,oldVal,hEditbox};
  % Register the undo/redo action with the figure
  uiundo(gcbf,'function',cmd);
end
% Internal update function to update slider & editbox
function internal_update(hSlider,newValue,hEditbox)
  set(hSlider,'value',newValue);
  set(hEditbox,'string',num2str(newValue));
end
</pre>
<p>And now let&#8217;s point the slider&#8217;s callback to our new function:</p>
<pre lang="matlab">
>> set(hSlider,'Callback',@test_uiundo);
</pre>
<p><center><figure style="width: 284px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Undo/redo functionality integrated in the figure" src="https://undocumentedmatlab.com/images/uiundo2a.png" title="Undo/redo functionality integrated in the figure" width="284" height="166" /><figcaption class="wp-caption-text">Undo/redo functionality integrated in the figure</figcaption></figure></center><br />
We can also invoke the current Undo and Redo actions programmatically, by calling <b><i>uiundo</i></b> with the &#8216;execUndo&#8217; and &#8216;execRedo&#8217; arguments:</p>
<pre lang="matlab">
uiundo(hFig,'execUndo');
uiundo(hFig,'execRedo');
</pre>
<p>When invoking the current Undo and Redo actions programmatically, we can ensure that this action would be invoked only if it is a specific action that is intended:</p>
<pre lang="matlab">
uiundo(hFig,'execUndo','Save data');  % should equal cmd.Name
</pre>
<p>We can use this approach to attach programmatic undo/redo actions to new toolbar or GUI buttons. The code for this was given in the <a target="_blank" href="/articles/figure-toolbar-components/">above-mentioned article</a>. Here is the end-result:<br />
<center><br />
<figure style="width: 382px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Undo/redo functionality integrated in the figure toolbar" src="https://undocumentedmatlab.com/images/uiundo5.png" title="Undo/redo functionality integrated in the figure toolbar" width="302" height="199" /><br />
<br />
<img loading="lazy" decoding="async" alt="Undo/redo functionality integrated in the figure toolbar" src="https://undocumentedmatlab.com/images/uiundo6.png" title="Undo/redo functionality integrated in the figure toolbar" width="382" height="221" /><figcaption class="wp-caption-text">Undo/redo functionality integrated in the figure toolbar</figcaption></figure><br />
</center><br />
In my next post, due next week, I will explore advanced customizations of this functionality.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/uiundo-matlab-undocumented-undo-redo-manager">uiundo &#8211; Matlab&#039;s undocumented undo/redo manager</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-uiundo" rel="bookmark" title="Customizing uiundo">Customizing uiundo </a> <small>This article describes how Matlab's undocumented uiundo undo/redo manager can be customized...</small></li>
<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/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/undocumented-cursorbar-object" rel="bookmark" title="Undocumented cursorbar object">Undocumented cursorbar object </a> <small>Matlab's internal undocumented graphics.cursorbar object can be used to present dynamic data-tip cross-hairs...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/uiundo-matlab-undocumented-undo-redo-manager/feed</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Figure toolbar components</title>
		<link>https://undocumentedmatlab.com/articles/figure-toolbar-components?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=figure-toolbar-components</link>
					<comments>https://undocumentedmatlab.com/articles/figure-toolbar-components#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 27 Aug 2009 16:31:36 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Semi-documented function]]></category>
		<category><![CDATA[Toolbar]]></category>
		<category><![CDATA[uitools]]></category>
		<category><![CDATA[uiundo]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=541</guid>

					<description><![CDATA[<p>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.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/figure-toolbar-components">Figure toolbar components</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/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-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/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/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>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Toolbars are by now a staple of modern GUI design. An unobtrusive list of small icons enables easy access to multiple application actions without requiring large space for textual descriptions. Unfortunately, the built-in documented support for the Matlab toolbars is limited to adding icon buttons via the <b><i>uipushtool</i></b> and <b><i>uitoggletool</i></b> functions, and new toolbars containing them via the <b><i>uitoolbar</i></b> function. In this post I will introduce several additional customizations that rely on undocumented features.<br />
This article will only describe figure toolbars. However, much of the discussion is also relevant to the desktop (Command Window) toolbars and interested users can adapt it accordingly.</p>
<h3 id="Matlab">Accessing toolbar buttons &#8211; undo/redo</h3>
<p>Let&#8217;s start by adding undo/redo buttons to the existing figure toolbar. I am unclear why such an elementary feature was not included in the default figure toolbar, but this is a fact that can easily be remedied. In <a target="_blank" href="/articles/uiundo-matlab-undocumented-undo-redo-manager/">another post</a> I describe <b><i>uiundo</i></b>, Matlab&#8217;s semi-documented support for undo/redo functionality, but for the present let&#8217;s assume we already have this functionality set up.<br />
First, let&#8217;s prepare our icons, which are basically a green-filled triangle icon and its mirror image:</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],:);
</pre>
<p>Now let&#8217;s add these icons to the default figure toolbar:</p>
<pre lang="matlab">
% Add the icon (and its mirror image = undo) to the latest toolbar
hUndo = uipushtool('cdata',cdataUndo, 'tooltip','undo', 'ClickedCallback','uiundo(gcbf,''execUndo'')');
hRedo = uipushtool('cdata',cdataRedo, 'tooltip','redo', 'ClickedCallback','uiundo(gcbf,''execRedo'')');
</pre>
<p><center><figure style="width: 296px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Undo/redo buttons" src="https://undocumentedmatlab.com/images/uiundo4.png" title="Undo/redo buttons" width="296" height="199" /><figcaption class="wp-caption-text">Undo/redo buttons</figcaption></figure></center><br />
In the preceding screenshot, since no figure toolbar was previously shown, <b><i>uipushtool</i></b> added the undo and redo buttons to a new toolbar. Had the figure toolbar been visible, then the buttons would have been added to its right end. Since undo/redo buttons are normally requested near the left end of toolbars, we need to rearrange the toolbar buttons:</p>
<pre lang="matlab">
hToolbar = findall(hFig,'tag','FigureToolBar');
%hToolbar = get(hUndo,'Parent');  % an alternative
hButtons = findall(hToolbar);
set(hToolbar,'children',hButtons([4:end-4,2,3,end-3:end]));
set(hUndo,'Separator','on');
</pre>
<p><center><figure style="width: 302px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Undo/redo buttons in their expected positions" src="https://undocumentedmatlab.com/images/uiundo5.png" title="Undo/redo buttons in their expected positions" width="302" height="199" /><figcaption class="wp-caption-text">Undo/redo buttons in their expected positions</figcaption></figure></center><br />
We would normally preserve hUndo and hRedo, and modify their <i>Tooltip</i> and <i>Visible</i>/<i>Enable</i> properties in run-time, based on the availability and name of the latest undo/redo actions:</p>
<pre lang="matlab">
% Retrieve redo/undo object
undoObj = getappdata(hFig,'uitools_FigureToolManager');
if isempty(undoObj)
   undoObj = uitools.FigureToolManager(hFig);
   setappdata(hFig,'uitools_FigureToolManager',undoObj);
end
% Customize the toolbar buttons
latestUndoAction = undoObj.CommandManager.peekundo;
if isempty(latestUndoAction)
   set(hUndo, 'Tooltip','', 'Enable','off');
else
   tooltipStr = ['undo' latestUndoAction.Name];
   set(hUndo, 'Tooltip',tooltipStr, 'Enable','on');
end
</pre>
<p>We can easily adapt the method I have just shown to modify/update existing toolbar icons: hiding/disabling them etc. based on the application needs at run-time.</p>
<h3 id="Java">Adding non-button toolbar components &#8211; undo dropdown</h3>
<p>A more advanced customization is required if we wish to present the undo/redo actions in a drop-down (combo-box). Unfortunately, since Matlab only enables adding <b><i>uipushtool</i></b>s and <b><i>uitoggletool</i></b>s to toolbars, we need to use a Java component. The drawback of using such a component is that it is inaccessible via the toolbar&#8217;s Children property (implementation of the drop-down callback function is left as an exercise to the reader):</p>
<pre lang="matlab">
% Add undo dropdown list to the toolbar
jToolbar = get(get(hToolbar,'JavaContainer'),'ComponentPeer');
if ~isempty(jToolbar)
   undoActions = get(undoObj.CommandManager.UndoStack,'Name');
   jCombo = javax.swing.JComboBox(undoActions(end:-1:1));
   set(jCombo, 'ActionPerformedCallback', @myUndoCallbackFcn);
   jToolbar(1).add(jCombo,5); %5th position, after printer icon
   jToolbar(1).repaint;
   jToolbar(1).revalidate;
end
% Drop-down (combo-box) callback function
function myUndoCallbackFcn(hCombo,hEvent)
   itemIndex = get(hCombo,'SelectedIndex');  % 0=topmost item
   itemName  = get(hCombo,'SelectedItem');
   % user processing needs to be placed here
end
</pre>
<p><center><figure style="width: 382px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Undo dropdown list" src="https://undocumentedmatlab.com/images/uiundo6.png" title="Undo dropdown list" width="382" height="221" /><figcaption class="wp-caption-text">Undo dropdown list</figcaption></figure></center><br />
Note that the javax.swing.JComboBox constructor accepts a cell-array of strings (undoActions in the snippet above). A user-defined dropdownlist might be constructed as follows (also see a <a rel="nofollow" target="_blank" href="https://www.mathworks.com/matlabcentral/newsreader/view_thread/160674#405575">related CSSM thread</a>):</p>
<pre lang="matlab">
...
dropdownStrings = {'here', 'there', 'everywhere'};
jCombo = javax.swing.JComboBox(dropdownStrings);
set(jCombo, 'ActionPerformedCallback', @myUndoCallbackFcn);
jToolbar(1).addSeparator;
jToolbar(1).add(jCombo);  % at end, following a separator mark
jToolbar(1).repaint;
jToolbar(1).revalidate;
...
</pre>
<p>A similar approach can be used to add checkboxes, radio-buttons and other non-button controls.<br />
In next week&#8217;s post I will describe how the toolbar can be customized using undocumented functionality to achieve a non-default background, a floating toolbar (&#8220;palette&#8221;) effect and other interesting customizations. If you have any specific toolbar-related request, I&#8217;ll be happy to hear in the comments section below.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/figure-toolbar-components">Figure toolbar components</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/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-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/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/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>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/figure-toolbar-components/feed</wfw:commentRss>
			<slash:comments>50</slash:comments>
		
		
			</item>
	</channel>
</rss>
