How can I save permanently a change made in a GUI?

7 visualizaciones (últimos 30 días)
Pol Auladell
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!

Respuesta aceptada

Jan
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
Pol Auladell
Pol Auladell el 9 de Dic. de 2018
Editada: Pol Auladell el 9 de Dic. de 2018
%Edit, I've added the function off CloseRequestFcn immediately after I 've done the changes in the popUp menu.
Solved :)
Okay, I've just tried it. But it doesn't work. I may have been doing something wrong.
I guess your OpeningFcn is the one that MATLAB has created automatically (interfaceName_OpeningFcn). The directory where I saved my file is the same where I'm working:
prefFile = fullfile('savedState.mat');
then I change your PopupMenu by the tag I have put.
I've created CloseRequestFcn and DeletFcn functions since it doesn't appear in the code as default.
When I run my program and I close I don't see any savedState.mat in my directory, and obviosly, when I execute it again, the data is the same as the beggining.
What am I doing wrong?
Jan
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by