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

2 ビュー (過去 30 日間)
Jay
Jay 2016 年 5 月 1 日
編集済み: Jay 2016 年 5 月 1 日
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 件)

Walter Roberson
Walter Roberson 2016 年 5 月 1 日
persistent round
if isempty(round); round = 0; end
How do you plan to indicate that you need to reset round to 0?

カテゴリ

Help Center および File ExchangeStrategy & Logic についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by