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 uiundo function. Note that uiundo and its functionality is not Java-based but rather uses Matlab’s classes and the similarly-undocumented schema-based object-oriented approach.
A couple of months ago, I explained how to customize the figure toolbar. In that article, I used the undocumented uiundo function as a target for the toolbar customization and promised to explain its functionality later. I would now like to explain uiundo and its usage.
The uiundo function is basically an accessor for Matlab’s built-in undo/redo manager object. It is located in the uitools folder (%MATLABROOT%\toolbox\matlab\uitools) and its @uiundo sub-folder. To use uiundo, simply define within each uicontrol’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. uiundo then takes care of adding this data to the figure’s undo/redo options under Edit in the main figure menu.
For example, let’s build a simple GUI consisting of a slider that controls the value of an edit box:
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);

Simple GUI with slider update of a numeric value
Now, let’s attach undo/redo actions to the slider’s callback. First, place the following in test_uiundo.m:
% 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
And now let’s point the slider’s callback to our new function:
>> set(hSlider,'Callback',@test_uiundo);

Undo/redo functionality integrated in the figure
We can also invoke the current Undo and Redo actions programmatically, by calling uiundo with the ‘execUndo’ and ‘execRedo’ arguments:
uiundo(hFig,'execUndo'); uiundo(hFig,'execRedo');
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:
uiundo(hFig,'execUndo','Save data'); % should equal cmd.Name
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 above-mentioned article. Here is the end-result:


Undo/redo functionality integrated in the figure toolbar
In my next post, due next week, I will explore advanced customizations of this functionality.
Related posts:
- Customizing uiundo This article describes how Matlab's undocumented uiundo undo/redo manager can be customized...
- Undocumented mouse pointer functions Matlab contains several well-documented functions and properties for the mouse pointer. However, some very-useful functions have remained undocumented and unsupported. This post details their usage....
- FindJObj – find a Matlab component’s underlying Java object The FindJObj utility can be used to access and display the internal components of Matlab controls and containers. This article explains its uses and inner mechanism....
- Matlab and the Event Dispatch Thread (EDT) The Java Swing Event Dispatch Thread (EDT) is very important for Matlab GUI timings. This article explains the potential pitfalls and their avoidance using undocumented Matlab functionality....
- ismembc – undocumented helper function Matlab has several undocumented internal helper functions that can be useful on their own in some cases. This post presents the ismembc function....
- Figure toolbar components 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....
Tags: callbacks, pure Matlab, schema.class, Toolbar, uitools, uiundo, Undocumented feature, Undocumented function
Categories: Figure window, GUI, High risk of breaking in future versions, Stock Matlab function, UI controls, Undocumented feature, Undocumented function
This entry was posted
on Thursday, October 29th, 2009 at 3:11 pm PST
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
