How can I save permanently a change made in a GUI?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Pol Auladell
el 9 de Dic. de 2018
Comentada: Jan
el 10 de Dic. de 2018
Hi guys, I made an interface, and one of its functions is a register.
The GUI has a pop up menu where appears all the registered users, if the user isn't in the list, you can add another user by typing his name. Now in the pop up menu I am able to see the new user and save his data.
But, of course, when I close the figure and I reopen the interface, I loose the new user I have introduced in the list (his data is saved in a txt, so the only thing I loose is his name in the menu).
Is it possible saving permanently that change in the pop up menu?
How can I do it?
I've read something about to save the state in a mat file, but I don't really understand how it works. Could someone explain me it?
Many thanks!
0 comentarios
Respuesta aceptada
Jan
el 9 de Dic. de 2018
You can insert a command to load the data in the OpeningFcn:
function OpeningFcn(ObjectH, EventData, handles)
PrefFile = fullfile(prefdir, 'MyGUIData.mat'); % Use a proper and unique name
handles.PrefFile = PrefFile;
if exist(PrefFile, 'file')
data = load(PrefFile);
set(handles.PopupMenu, 'String', data.PopupMenuString);
end
guidata(ObjectH, handles);
end
The functions CloseRequestFcn and DeletFcn are called during closing and deleting. Add the code to store the current list of strings there:
function CloseRequestFcn(objectH, EventData, handles)
PopupMenuString = get(handles.PopupMenu, 'String');
save(handles.PrefFile, 'PopupMenuString');
end
and the same in the DeletFcn. It is smarter, to write a small subfunction for saving, which is called from the CloseRequestFcn and DeleteFcn to avoid to have redundant code. Soring the file name in the handles struct is a good idea also, because you have to change the code at one location only, if you choose another file name.
2 comentarios
Jan
el 10 de Dic. de 2018
It is not meaningful to use fullfile with 1 input argument only. Using the current directory is less smart than the prefdir, because you cannot know, which is the current directory, when the GUI is started.
Only creating the CloseRequestFcn and the DeleteFcn is not enough, but you have to define the correpsonding properties of the figure. Are you working with GUIDE? You find the PropertyInspector there to set the callback.
Más respuestas (0)
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!