How can a GUI pop up menu be made to show the previous selected option?
古いコメントを表示
I want to rephrase my earlier question as follows:
How can a GUI pop up menu be made to show the last selected option?
回答 (2 件)
Image Analyst
2013 年 4 月 12 日
This would probably work, though I haven't tested it.
allItems = get(handles.popup, 'string');
numberOfItems = length(allItems);
set(handles.popup, 'value', numberOfItems);
10 件のコメント
Shyam
2013 年 4 月 12 日
Image Analyst
2013 年 4 月 12 日
Then try the 'Selected' property or something like that
set(handles.popup, 'Selected', numberOfItems);
Image Analyst
2013 年 4 月 12 日
If you set Selected equal to the number of items in the list, then the selection will be the last one on the list - the bottom item on the list. Isn't that what you mean by "last"?
Shyam
2013 年 4 月 12 日
Image Analyst
2013 年 4 月 12 日
Glad that my suggestion to read the FAQ helped. But I guess I'm missing something. Why would the selected item change? If the user selects something, it will stay on that selection unless you, in code, or the user changes it to something else, like I've already said. When does it change so that it's not on the last selected item?
Shyam
2013 年 4 月 12 日
Image Analyst
2013 年 4 月 12 日
編集済み: Image Analyst
2013 年 4 月 12 日
Yes. I didn't know that you were completely shutting down your application. What you do in that case is to have two functions. One called SaveUserSettings(), which saves the current state of all the controls to a mat file. I use a structure called userSettings and have all the various states and settings as members of that. Then to save you query all the controls, store their settings in userSettings, and then save to a mat file:
function SaveUserSettings(handles)
userSettings = handles.userSettings;
userSettings.popupSelection = get(handles.popup, 'Selection');
% And so on for the other controls whose values you want to save.
save(matFullFileName, 'userSettings');
You can call this function when you exit, or whenever you change a control's state - whenever you prefer.
Then you need another function called LoadUserSettings() where you load that mat file and set the values of the controls.
function LoadUserSettings(handles)
matFullFileName = fullfile(pwd, 'UserSettings.mat');
if exist(matFullFileName, 'file)
storedStructure = load(matFullFileName);
handles.userSettings = storedStructure.UserSettings;
userSettings = handles.userSettings; % Create a shorter-named version.
% Assign states
set(handles.popup1, 'Selection', userSettings.popupSelection);
% and so on
end
Does that make sense?
Shyam
2013 年 4 月 12 日
Image Analyst
2013 年 4 月 12 日
If that will acceptable, then please mark the answer as "Accepted" to close this out. Thanks.
Yao Li
2013 年 4 月 12 日
allItems = get(handles.popupmenu1, 'string')
set(handles.popupmenu1,'String',allItems);
13 件のコメント
Shyam
2013 年 4 月 12 日
Shyam
2013 年 4 月 12 日
Yao Li
2013 年 4 月 12 日
Sel_val = get(handles.popupmenu1, 'value');
assignin('base','Sel_val',Sel_val)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
last_pop=evalin('base','Sel_val');
if~isempty(last_pop)
set(handles.popupmenu1,'value',last_pop);
else
end
Try to add above two paragraphs to the right location to meet your requirements
Yao Li
2013 年 4 月 12 日
base specifies the base workspace
Sel_val is just a variable defined to store the current selection of popmenu
Yao Li
2013 年 4 月 12 日
I've tried the above codes in a GUI script which works well. Maybe you need to replace the 2nd paragraph with blew:
temp='exist(''Sel_val'',''var'')';
if evalin('base',temp)
last_pop=evalin('base','Sel_val')
if~isempty(last_pop)
set(handles.popupmenu1,'value',last_pop);
else
end
else
end
I add this paragraph in OpeningFcn and the other paragraph in the popmenu Callback. Anyway, select the best location for your specification.
Shyam
2013 年 4 月 12 日
Shyam
2013 年 4 月 12 日
Image Analyst
2013 年 4 月 12 日
You don't need to be doing anything with evalin(), and it's recommended that you not use the base workspace for sharing variables. If you want to have a variable seen from within more than one function, see the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
Shyam
2013 年 4 月 12 日
Shyam
2013 年 4 月 12 日
Image Analyst
2013 年 4 月 12 日
Obviously. If you want to save the value to a disk file, you can do that with save() or fprintf(), or even xlswrite() or dlmwrite.
カテゴリ
ヘルプ センター および File Exchange で Entering Commands についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!