Drop-down menu Callback

15 views (last 30 days)
Scott
Scott on 23 Nov 2014
I have two different figures in my GUI. In the first figure that appears, I want the user to be able to select a bet from a drop-down menu that they would like for a hand of blackjack.
I would then like for this value to be saved, and then passed into the other part of the function, where we open the second figure file and use this bet which has been selected in the first figure.
I have:
persistent basebet
popup = uicontrol('Style', 'popup',...
'String', {'10','25','50','20'},...
'Position', [15 90 50 50],'background','green');
But I am not sure what the callback should be, or how to make sure that this value passed on to the other function to be used.

Answers (1)

Geoff Hayes
Geoff Hayes on 23 Nov 2014
Scott - at what point does the second figure launch? Are you destroying/closing the first figure before opening the second, or are both open at the same time?
If just prior to closing the first, then you could access the selected value from the drop-down menu using its handle, popup, as
sels = get(popup,'String'); % get the list of bets
idx = get(popup,'Value'); % get the index of the selected bet
basebet = str2num(sels{idx}); % get the bet
If, however, you wish to use a callback, then consider nesting it within your Blackjack GUI main program. One interesting feature of nested functions is that they have access to the local variables defined in the main function/program. In this case, we could use the local variable basebet that the nested function would update or later use (for the second figure). Somethine like
function blackjack
% declare and initialize basebet
basebet = 25;
% create the figure with drop-down menu
figure;
popup = uicontrol('Style', 'popup',...
'String', {'10','25','50','20'},...
'Position', [15 90 100 50],'background','green',...
'Value',2,'Callback',@popupCallback);
% callback for the drop-down menu
function popupCallback(obj,event)
sels = get(popup,'String');
idx = get(popup,'Value');
basebet = str2double(sels{idx});
end
end
Any other nested function (similar to popupCallback) would then be able to use basebet in the same way.
  3 Comments
Geoff Hayes
Geoff Hayes on 16 Jan 2015
No particular reason for using str2double or str2num (though I suspect when I used the latter there was a warning in the editor that I should use the former since it performs faster though only operates on scalars).
I am not sure why calling str2num('CLOSE') would close your currently open figure. If I try to evaluate this statement, the result is the empty matrix [].
Nicolás Casaballe
Nicolás Casaballe on 28 Mar 2016
Within the function str2num there's the sentence (at line 79 in my version) that after some basic parsing essentially evaluates the argument:
STR2NUM_VaR = eval(STR2NUM_StR);
Hence, by calling str2num('close'), the close command/function gets evaluated at some point, leading to the current figure closing.
Hardly the expected behaviour of str2num, but I imagine most of the functions would do the same when faced with such a strange call. A workaround might be to test for the value of the string before calling str2num:
tmp_str = sels{idx};
if ~strcmpi( tmp_str, 'close') % Check if tmp_str is not CLOSE
basebet = str2num (tmp_str);
end

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!