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

 Akzeptierte Antwort

Hitesh
Hitesh am 8 Mai 2025
Bearbeitet: Hitesh am 8 Mai 2025

0 Stimmen

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 Kommentar

Sylvain
Sylvain am 8 Mai 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Entering Commands finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2024b

Gefragt:

am 7 Mai 2025

Kommentiert:

am 8 Mai 2025

Community Treasure Hunt

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

Start Hunting!

Translated by