Make Pushbutton Visible in GUI
조회 수: 3 (최근 30일)
이전 댓글 표시
I'm attempting to make a pushbutton visible in my GUI when a specific selection is picked in the popup menu. Here's my code:
hbutton0 = uicontrol('Style','pushbutton','String','Load Time','Position',...
[150,365,90,30],'Callback',@callbackfn);
set(hbutton0,'Visible','Off')
hspopup0 = uicontrol('Style','popup','BackgroundColor','white','position',[20,350,100,40],'String',{'Modality';'PBS';'DS'});
if strcmp(get(hspopup0,'String'),'PBS')
set(hbutton0,'Visible','On');
end
Yet the hbutton0 does not appear when 'PBS' is selected. Can you please help? Thank you.
댓글 수: 0
답변 (2개)
Walter Roberson
2015년 10월 11일
The String property of a uicontrol('Style','popup') always holds all of the menu items, not just the currently selected item. In order to determine the currently selected item, you need to look at the numeric property Value of the uicontrol (which can be empty if nothing has been selected.) You can get() the String property and index it by the Value property if you want to know the string that was selected.
댓글 수: 0
Image Analyst
2015년 10월 11일
% Get the index of the item they chose.
selectedIndex = get(hspopup0, 'Value');
% Get a list of everything in the popup.
allItems = get(hspopup0,'String'); % Cell array of all items in the popup
% Get the string indicating the exact one item that they chose.
selectedItem = allItems{selectedIndex};
% Now do the comparison.
if strcmp(selectedItem ,'PBS')
set(hbutton0,'Visible','On');
end
댓글 수: 4
Walter Roberson
2015년 10월 11일
Works for me when I test it by using your code
hbutton0 = uicontrol('Style','pushbutton','String','Load Time','Position',...
[150,365,90,30],'Callback',@callbackfn);
set(hbutton0,'Visible','Off')
hspopup0 = uicontrol('Style','popup','BackgroundColor','white','position',[20,350,100,40],'String',{'Modality';'PBS';'DS'});
and then selecting the PBS entry and then using Image Analyst's code.
If you are looking to have it immediately switch then you would need to set up a callback on hspopup0
Image Analyst
2015년 10월 11일
Use the debugger to step through your code and find out what lines of code it's actually executing.
참고 항목
카테고리
Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!