ho to implement Undo/Redo in a Matlab App

4 次查看(过去 30 天)
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

采纳的回答

Hitesh
Hitesh 2025-5-8
编辑:Hitesh 2025-5-8
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 个评论
Sylvain
Sylvain 2025-5-8
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by