ho to implement Undo/Redo in a Matlab App

조회 수: 9 (최근 30일)
Sylvain
Sylvain 2025년 5월 7일
댓글: Sylvain 2025년 5월 8일
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