Restart script when user says yes

42 ビュー (過去 30 日間)
Jake keogh
Jake keogh 2021 年 2 月 11 日
コメント済み: Jake keogh 2021 年 2 月 11 日
Hi,
I have a while statement at the end of my code asking if they would like to select another choice to repeat the code, however I cannot get it to begin from the beginning of the script again, and so just repeats itself.
My code:
function [m] = AnotherTeam
while(1)
m = input('Do you want to choose a different team, Y/N [Y]:','s');
if m == 'N'
break
elseif m == 'Y'
[team] = choose_team;
end
end
end
Is there a way to get this to restart the code from a specific point
Many thanks in advance for any feedback
  2 件のコメント
Geoff Hayes
Geoff Hayes 2021 年 2 月 11 日
Jake - you may need to show more of your code so that we can get an idea of what you mean by ..begin ffom the beginning of the script again. What is the beginning? Outside of the while loop or somewhere else? If the user selects N then (from what I can see) you break out of the while loop. But where do you want to "go" if the user says Y.
Also, for string comparisons, it is best to use strcmp or strcmpi else you may get an error when using == and comparing strings of different lengths.
Jake keogh
Jake keogh 2021 年 2 月 11 日
Hi Geoff, thank you for your reply,
I will show my main script which is calling different functions.
% clear workspace and command window
clear; clc
%Loads menu prompting user to choose a team to access data for....
%Displays table showing there stats.
[team] = choose_team;
%Decides whether to run the function for analysing all teams together or
%one team specifically
all_team(team);
%Calculates average wins based on every season in data set
[avg] = avg_wins(team);
% asks the user if they would like to choose another team which will
% (hopefully) reset script to [team] = choose_team
[m] = AnotherTeam;
So i'm hoping to get this function at the end of the script to then re run the script from the beginning.
Many thanks

サインインしてコメントする。

採用された回答

Geoff Hayes
Geoff Hayes 2021 年 2 月 11 日
Jake - you could try removing the while from the AnotherTeam function and put that in your main script. Suppose that AnotherTeam is simply
function [m] = AnotherTeam
m = input('Do you want to choose a different team, Y/N [Y]:','s');
end
then your main would look something like
% clear workspace and command window
clear; clc
m = 'Y';
while strcmp(m,'Y')
%Loads menu prompting user to choose a team to access data for....
%Displays table showing there stats.
[team] = choose_team;
%Decides whether to run the function for analysing all teams together or
%one team specifically
all_team(team);
%Calculates average wins based on every season in data set
[avg] = avg_wins(team);
% asks the user if they would like to choose another team which will
% (hopefully) reset script to [team] = choose_team
[m] = AnotherTeam;
end
  1 件のコメント
Jake keogh
Jake keogh 2021 年 2 月 11 日
Thanks :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by