이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How can a GUI pop up menu be made to show the previous selected option?
조회 수: 1 (최근 30일)
이전 댓글 표시
Shyam
2013년 4월 12일
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
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"?
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일
say u select the second choice in gui ...this will set some parameters and initiate some activities...now u close the gui and run the simulation...second time the gui is opened..it will be displaying the first choice ...now user who wants to select the first choice, leaves the gui untouched, closes it and wen he runs the simulation...everything will correspond to the second choice...becoz unless he manually selects the first choice , it wont initiate the first choice's activities...
hope its clear now...any questions?
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?
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일
but how will it show my earlier selection...i need to rearrange the string somehow to make my earlier selection come to the first
correct me if i am wrong
Shyam
2013년 4월 12일
This is a part of a code...CODE needs to have the knowledge of past selection to work correctly...
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일
allItems = get(handles.popupmenu1, 'string') set(handles.popupmenu1,'String',allItems);
wat this does is getting the string frm pop up and then putting it back...right?....y do u want to put it back again?
Shyam
2013년 4월 12일
Kept the below in popupmenu callback
Sel_val = get(handles.popupmenu4, 'Value'); assignin('base','Sel_val',Sel_val)
kept the below in the opening func
temp='exist(''Sel_val'',''var'')'; if evalin('base',temp) last_pop=evalin('base','Sel_val') if~isempty(last_pop) set(handles.popupmenu4,'value',last_pop); else end else end
NO WAY!!
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일
but if u close matlab then the value u stored using setappdata is lost.... how to retain the value?
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)