Keep track of variable value in gui

I am designing a program with GUIDE that simulates a coin toss game. It has 3 components:
+ A static text box, called text1 to keep track of your current balance.
+ An editable text box, called text2 so that you can type in the amount you want to bet.
+ A push button, called button1 to simulate a coin toss.
This is what I have tried:
%first, I used the Property Inspector on text1 to change its string from "Static text" to "1000"
%This way, the player starts the game with $1000
Current_Balance=str2double(get(handles.text1,'string'));
%Then, I acquire the bet amount from text2:
Bet=str2double(get(handles.text2,'string'));
%Next, to simulate a coin toss:
result=binornd(1,0.5);
if result==0;
Current_Balance=Current_Balance - Bet;
else Current_Balance=Current_Balance + Bet;
end
%I tried to update the value of Current_Balance with the next code.
%This is where the error message pops up;
set(handles.text1,'string',Current_Balance);
What I am trying to do is to calculate and show the new result Current_Balance each time a button is pressed. Any ideas?

댓글 수: 1

Jan
Jan 2017년 3월 25일
You mention an error message, but keep the message as your secret. Please reveal it, such that we do not have to guess it. :-)

댓글을 달려면 로그인하십시오.

답변 (2개)

Jan
Jan 2017년 3월 25일
편집: Jan 2017년 3월 25일

1 개 추천

Current_Balance = str2double(get(handles.text1, 'string'));
Now Current_Balance is a double array, most likely a scalar. You perform some arithmetic operations with it, and then:
set(handles.text1, 'string', Current_Balance);
tries to set the 'string' value with a double scalar. I guess you want:
set(handles.text1, 'string', num2str(Current_Balance));
or
set(handles.text1, 'string', sprintf('%.2f', Current_Balance));
to provide a string.

댓글 수: 2

Nguyen Quang
Nguyen Quang 2017년 3월 26일
편집: Nguyen Quang 2017년 3월 26일
Thank you very much for your help. I am sorry that I only posted part of the program, because I thought it was the one part that caused the problem. I ended up getting a wrong answer for that. I apologize for wasting your time.
Anyway, this is the full game:
+ A static text box, called text1 to keep track of your current balance (just like the previous one). The string value is changed from "Static text" to "1000"(so that players start with $1000);
+ An editable text box, called text2 so that you can type in the amount you want to bet ((just like the previous one);).
+ A push button, called buttonA, lets you join a coin toss game.
+ Another push button, called buttonB, lets you join a game of dice.
+ A third button, buttonC, simulates the result and updates your Current Balance
The game goes as follow:
%Press buttonA to play coin game
function buttonA_Callback(hObject, eventdata, handles)
Current_Balance=str2double(get(handles.text1,'string'));
Bet=str2double(get(handles.text2,'string'));
result=binornd(1,0.5);
if result==0;
Current_Balance=Current_Balance - Bet;
else Current_Balance=Current_Balance + Bet;
end
%Press buttonB to play dice game
function buttonB_Callback(hObject, eventdata, handles)
Current_Balance=str2double(get(handles.text1,'string'));
Bet=str2double(get(handles.text2,'string'));
result=binornd(1,0.1666);
if result==0
Current_Balance=Current_Balance - 3*Bet;
else Current_Balance=Current_Balance + 3*Bet;
end
%Press button C to show the result
function buttonC_Callback(hObject, eventdata, handles)
set (handles.text1,'string',Current_Balance);
Whenever I press buttonC, Matlab shows the error message as follow:
??? Undefined function or variable 'Current_Balance'.
Error in ==> untitled>buttonC_Callback at 127
set(handles.text1, 'string', Current_Balance);
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> untitled at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)untitled('buttonC_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
I don't know what the problem is. I guess it has to do with making sure that either buttonA or buttonB is pushed before button C is pushed. Can you guys please help me, please?
The error message is clear: In this piece of code:
function buttonC_Callback(hObject, eventdata, handles)
set(handles.text1,'string',Current_Balance);
The variable "Current_Balance" is not defined. It was created in another callback, but the variables are not shared between functions.
The topic of sharing variables between callback is discussed frequently in this forum, therefore searching in the forum is a good idea. See e.g.:

댓글을 달려면 로그인하십시오.

Nguyen Quang
Nguyen Quang 2017년 3월 31일

0 개 추천

Thanks for the article you recommended.I managed solved the problem myself. The solution is to declare Current_Balance a global variable at the beginning of each button callback, like this:
%Press buttonA to play coin game
function buttonA_Callback(hObject, eventdata, handles)
global Current_Balance
(...)
%Press buttonB to play coin game
function buttonB_Callback(hObject, eventdata, handles)
global Current_Balance
(...)
%Press button C to show the result
function buttonC_Callback(hObject, eventdata, handles)
global Current_Balance
set (handles.text1,'string',Current_Balance)
It worked this time. Thanks a lot.

댓글 수: 1

Stephen23
Stephen23 2017년 3월 31일
편집: Stephen23 2017년 3월 31일
@Nguyen Quang: declaring the variable as global is not a good solution, because using globals is a bad way to write code. Read the links that Jan Simon gave you, they describe much better solutions.
You should avoid globals:

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Video games에 대해 자세히 알아보기

태그

질문:

2017년 3월 25일

편집:

2017년 3월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by