Question

I wanted to create an "undo" like functionality in a GUI I made. My idea was to create a copy of an object that stores all the settings of my GUI whenever a setting was changed. To do this it would be convenient if there was a "master" callback that would trigger whenever any other GUI callback was triggered by the user. Does such a callback exist? Thanks!

Was it helpful?

Solution

I am not aware of a callback like you described, but if you store your settings in a structure array, you can go back to previous settings.

Here is my proposal:

1) Store your gui settings in a structure with commands like this:

gui_settings.button1=1;

gui_settings.button2 = 0;

2) Store the gui settings structure in the UserData of your gui figure

set(gcf, 'UserData', gui_settings) 3) Create a callback for "StoreGuiSettings" that would read all the gui settings, then store them back in the UserData, the commands would look something like this:

gui_settings = get(gcf, 'UserData');

gui_settings(end+1) = gui_settings(end); %copy the last group of settings

gui_settings(end).button1 = get(h_button1, 'value'); % read button 1's setting

% read all the other settings ...

set(gcf, 'UserData', gui_settings) % Store the settings in UserData

4) Modify your other callbacks to call the "StoreGuiSettings" callback at some point to enable the undo

5) Make an "Undo" callback that retrieves the gui settings from the UserData, deletes the last element in the structure array gui_settings(end) = [];, and stores the gui_settings back in UserData.

Deleting the last setting is OK if you do not want the option of having a "redo" option. If you want to "redo", then you will have to maintain a pointer to which element in the gui_settings structure array is the current one, then change the pointer when the user wants to "redo" or "undo".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top