Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Ask User to Continue

Asked by manish sharma on 30 Nov 2011

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

0 Comments

manish sharma

Products

No products are associated with this question.

2 Answers

Answer by Naz on 30 Nov 2011
Accepted answer

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 Comments

Naz on 30 Nov 2011

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 on 1 Dec 2011

Hey Naz,

That worked.

Thanks!

Naz
Answer by Image Analyst on 30 Nov 2011
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

0 Comments

Image Analyst

Contact us