How can I save value of a variable when calling the same function again?

Hello,
I am doing a tictactoe using GUI game and want to show which round is currently playing ex 'Round 1'. This is what I have
round = 0;
if round == 0
set(handles.text6, 'String', 'Round 1')
round = 1;
elseif round == 1
set(handles.text6, 'String', 'Round 2')
round == 2;
elseif round == 2
set(handles.text6, 'String', 'Round 3')
round == 3;
end
the problem is that every time I call the function, round is set to zero so it will always be 'Round 1'. How can I keep the value of round when calling the same function again?
Thank you!

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 1일
편집: Azzi Abdelmalek 2016년 5월 1일
Because in the first line you wrote
round=0
You can declare the variable round as persistent
function -------
persistent round
if isempty(round)
round=0
end
% your code

댓글 수: 1

Jay
Jay 2016년 5월 1일
편집: Jay 2016년 5월 1일
Thank you so much it worked. I appreciate it.

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

추가 답변 (1개)

카테고리

도움말 센터File Exchange에서 Strategy & Logic에 대해 자세히 알아보기

질문:

Jay
2016년 5월 1일

편집:

Jay
2016년 5월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by