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

7 ビュー (過去 30 日間)
Pol Auladell
Pol Auladell 2018 年 12 月 9 日
コメント済み: Jan 2018 年 12 月 10 日
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!

採用された回答

Jan
Jan 2018 年 12 月 9 日
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 件のコメント
Pol Auladell
Pol Auladell 2018 年 12 月 9 日
編集済み: Pol Auladell 2018 年 12 月 9 日
%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 2018 年 12 月 10 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by