Make Pushbutton Visible in GUI

2 views (last 30 days)
Adam Glick
Adam Glick on 11 Oct 2015
Commented: Image Analyst on 11 Oct 2015
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.

Answers (2)

Walter Roberson
Walter Roberson on 11 Oct 2015
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.

Image Analyst
Image Analyst on 11 Oct 2015
% 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 Comments
Walter Roberson
Walter Roberson on 11 Oct 2015
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
Image Analyst on 11 Oct 2015
Use the debugger to step through your code and find out what lines of code it's actually executing.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!