Ask User to Continue

88 ビュー (過去 30 日間)
manish sharma
manish sharma 2011 年 11 月 30 日
コメント済み: Diana Hunter 2023 年 3 月 22 日
Hi,
I am interested in asking the user to continue the code execution.
If he enters 'Y', it should make the code to run again from the start. Otherwise for 'N' it should stop.
I think, it will start with something like this:
m=input('Do you want to continue, Y/N [Y]:','s')
Please help
Thanks

採用された回答

Naz
Naz 2011 年 11 月 30 日
Set the infinite loop. I did not check if this code runs, but the idea is this:
while(1)
%%your code here
m=input('Do you want to continue, Y/N [Y]:','s')
if m=='N'
break
end
end
Also, to avoid check up for extra cases from user input I would use function 'menu' instead of 'input':
while(1)
%%your code here
choice = menu('Press yes no','Yes','No');
if choice==2 | choice==0
break;
end
end
  2 件のコメント
Naz
Naz 2011 年 11 月 30 日
You also can create a dummy variable and set it to zero in the 'if' statement:
x=1;
while(x)
if ...
x=0;
end
end
manish sharma
manish sharma 2011 年 12 月 1 日
Hey Naz,
That worked.
Thanks!

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2011 年 11 月 30 日
promptMessage = sprintf('Do you want to Continue processing,\nor Cancel to abort processing?');
button = questdlg(promptMessage, 'Continue', 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
return; % Or break or continue
end
  1 件のコメント
Diana Hunter
Diana Hunter 2023 年 3 月 22 日
Thank you so much for this. I was having problems with accidentally overwriting exported figures so now I have adapted this so it asks me if I want to export the figure or not, saves a lot of hassle as I always forget to comment it out when I just want to test run the formatting of the figure first.

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


Peyman Obeidy
Peyman Obeidy 2017 年 3 月 24 日
%
while(1)
%%your code here
promptMessage = sprintf('Do you want to Continue processing,\nor Cancel to abort processing?');
button = questdlg(promptMessage, 'Continue', 'Continue', 'Terminate', 'Continue');
if strcmpi(button, 'Terminate')
break
end
end

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by