Building GUIs in Matlab – Part III Utilizing User Input

In this post we will combine the ideas presented in part I and part II on building a GUI utilizing guide in Matlab. So far we have generated two graphical windows, one of which includes radio buttons, edittable text boxes, a drop-down menu, and a push button, and the second of which has a modifiable strategy table. The object now is to combine the two windows, and allow for proper user control of all of these components.

As a reminder here is what we currently have:

User interface and table GUIs

The goal now is to associate the table with different selections within the drop-down menu, and obtain user inputs from the dealer radio buttons and player text boxes. Finally, we’ll utilize the ‘OK’ push button to output a recommended decision into the ‘Recommended Decision’ un-edittable text box. A short explanation is shown below for the text box and radio button components. Following next week’s post when we discuss the drop-down menu and OK button, we’ll also provide all of the code for you to play around with.

– Dealer Card Radio Buttons
The 10 radio buttons for the dealer card should have string values of ‘2’,’3′,…’9′,’10,J,Q,K’,and ‘Ace’. For the radio button group labeled ‘Dealer’s Card’, we have tagged the group with the value dealerCard. To get to this screen, and make the modifications, right-click on the button group and select “Property Inspector”.
Property inspector for radio button group
Upon clicking the OK button, we will want to know which of the 10 values was selected and save that string value into the variable “dealer”, therefore under the pushbuttonOK_Callback routine, enter the following:

dealer = get(get(handles.dealerCard,'SelectedObject'),'String');

That’s it! Now you have the selected value from within the radio button group. As with all other components, the combination of get and handles will come in handy, with values matching those seen in the “property inspector” for many pre-configured items. Of note is the “handles” variable which will be utilized throughout your code, as this is the structure within which variables can be added and updated, with the structure automatically passed into all functions, courtesy of guide.

One other functionality which is of importance is our ability to index within the table. As the table is setup with the dealer cards being the columns, we want to convert our string value for the dealer card into the column index. This will be simple for values 2:9, bu we need to have a similar n+1 offset for the tens (T,J,Q,K) and Ace.

if isequal(dealer,'Ace')
    dealer = '11'; % convert ace value into a string number value of 11
end
if isequal(dealer,'10, J, Q, K')
    dealer = '10'; % convert the T values into 10
end

From here we can simply convert the string to a number (using strnum), and subtract 1 to determine the column index. Now for the player cards.

– Player Card Text Boxes
Similar to the radio buttons, we can access the contents of the text boxes using get and the handles structure. We’ll assign our two cards to the values “card1” and “card2” within the pushbuttonOK_Callback routine.

card1 = get(handles.FirstCard,'String');
card2 = get(handles.SecCard,'String');

Again, simple. But this time we also need to be concerned with proper inputs entered into each of the text boxes. In order to perform error handling, we’ll need to check for valid numbers (2 thru 10) and letters (lower and upper case a,t,j,q,k). We can do this within each cards callback (“FirstCard_Callback” and “SecCard_Callback as we’ve given FirstCard and SecCard as the label for the two text boxes). For example the first card text box callback will look as follows:

function FirstCard_Callback(hObject, eventdata, handles)

cardNums = 2:10;
cardFace = {'A','a','K','k','Q','q','J','j','T','t'};

cardValue = get(hObject,'String');
% ensure that the value typed in is suitable for a playing card
if ~ismember(cardValue,cardFace) && ~ismember(str2double(cardValue),cardNums)
    set(hObject,'String','')
end

With this is place, if an invalid value is entered into a textbox (i.e. ‘Hello World’), no error message will be displayed, rather the textbox will be cleared with the next action.

So now we have ways of accessing the text box values and the radio button values. Next week we’ll wrap this entire series up with a look at the push button and drop-down menu, while also providing all the code for having your own blackjack strategy GUI.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.