MATLAB Answers

How to create undo and redo buttons in GUI that can be pressed multiple times?

20 views (last 30 days)
John Doe
John Doe on 14 May 2020
Edited: John Doe on 18 May 2020
Hello everyone! I have a working GUI where I have a undo and redo buttons. I have saved the current and previous states in two variables and implemented them in the respective callbacks. But I can undo and redo for only once. I guess the data is being overwritten everytime. Is there a way around it? Or a easier solution?
Thanks in advance!


Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 14 May 2020
John - without seeing your code, we can't determine why it is allowing you to undo and redo only once. You mention that you have saved the current and previous states in two variables. Are these scalar variables? If I were to implement redo and undo functionality, then I might use an array of some kind that would push the states to the front of the array so that when I call undo, the front would be popped up and the variables (or whatever) updated with the new front of the array. The popped off data would be pushed to the front of the redo array so that if I chose to call redo, then it's front would be popped off. There is lots more that you would have to consider - when should the redo array be emptied, when should the undo array be emptied, etc.


John Doe
John Doe on 17 May 2020
Thanks Geoff! It's working nicely now and the tip about using the handles really made the code look neater.
If you have any other tips regarding GUIDE, please do share.
Also, I was wondering if there was a way to activate the redo push button only if the undo button has been pressed? If undo is not pressed, one cannot press the redo button. Is this possible for push buttons?
Members like you are the main reason which make this MATLAB community so happening! Thanks for sticking with this thread for the past few days.
Image Analyst
Image Analyst on 17 May 2020
Not sure which button is redo, but you can set the Enable property to off in GUIDE by double clicking on the button and changing the Enable property to off. Now when they run it, it will be disabled. Then in the callback for the undo button, you can enable the redo button.
handles.btnRedo.Enable = 'on';
Any time you want to disable it again, you can do this:
handles.btnRedo.Enable = 'off';
Of course, change the name to whatever you called the redo button.
Thanks for Accepting and Voting for Geoff's answer. It's how people earn reputation points and is a good way to thank answerers (of which Geoff is one of the best).

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!