ho to implement Undo/Redo in a Matlab App

10 views (last 30 days)
I am after a good snippet code that would allow to have some Undo/Redo function.
I have found this intersiting one: uiundo - Matlab's undocumented undo/redo manager - Undocumented Matlab But it seems that uiundo might be removed at some point. I am happy to keep exploring this method, but I would like to find something more general in term of time history.
How to capture previous actions/values from the gui? Should I implement something like having a private property:
app.Data
app.DataBackwardStep
app.DataForwardStep
on new action:
  • DataBackwardStep(end+1) = app.Data
on undo:
  • app.DataForwardStep = app.Data
  • app.Data = app.DataBackwardStep(end-1)
  • app.DataBackwardStep(end) = [ ]
on redo
  • app.Data = app.DataForwardStep
  • DataBackwardStep(end+1) = app.Data
happy to see what others have already implemented

Accepted Answer

Hitesh
Hitesh on 8 May 2025
Edited: Hitesh on 8 May 2025
Hi Sylvain,
Absolutely, you’re on the right track! Implementing a custom undo/redo stack is a robust, general approach, especially when you want to avoid relying on undocumented or deprecated features like "uiundo". Following is a general undo/redo pattern you need to adapt for your MATLAB GUI app.
You maintained two stacks:
  • Undo Stack (DataBackwardStep): stores previous states.
  • Redo Stack (DataForwardStep): stores states you can return to after an undo.
I have implemented a similar functionality and attached the .mlapp file for your reference. Please review the code section of the app; it will give you a better understanding of the implementation.
Kindly ensure the following points for the usecase of this application.
  • State Storage: If your data is large, consider storing only the differences (deltas) or using lightweight representations.
  • Limit Stack Size: To save memory, you might cap the stack size (e.g., keep only the last 20 actions).
  • GUI Update: After undo/redo, update any relevant UI components to reflect the restored state.
For more information regarding the "uiButton" in App Designer. Kindly refer to the following MATALB documentation:
  1 Comment
Sylvain
Sylvain on 8 May 2025
Thanks, @Hitesh, indeed I need to limit the stack storage (will be set by user parameter in the app). I will need to test the stack size to disable the undo/redo buttons.
I will work around what you suggested, Thanks again

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!