I have the code in Part1 below which stores the values of x and y after the last iteration of my while-loop. Is there a way for me to achieve to what I am trying to do in Part2; that is, to retain all values of x and y along the way as the loop is still running/prompting the user for inputs? I have already achieved this with a for-loop, but I need to be able to do this with a while-loop and have hit a wall.
Any ideas? Also, it would be nice to be able to display an error message when the while-condition no longer holds true; the only way I know how to do this is with an if-statement and disp construct, which doesn't seem to be working inside the while-loop (again, I've been able to accomplish this in a for-loop but would like to do it in a while-loop). Any thoughts are welcome.
*
Part1 - Currently have:*
temp=('Enter initial [x y] pair: ');
while isempty(temp)==0
x=temp(1);
y=temp(2);
temp=input('Enter next [x y] pair');
end
*Part2 - Would like to have:*
temp=('Enter initial [x y] pair: ');
while isempty(temp)==0
x=temp(1);
y=temp(2);
temp=input('Enter next [x y] pair');
end

 Respuesta aceptada

Image Analyst
Image Analyst el 10 de Ag. de 2014

0 votos

You can have them enter some value that indicates when to quit, like if x or temp is negative of something
if x < 0
break;
end
or
if temp(1) < 0 || temp(2) < 0
uiwait(warndlg('Exiting loop because you entered a negative number'));
break;
end

3 comentarios

Maroulator
Maroulator el 10 de Ag. de 2014
Image Analyst,
Thanks! Any thoughts on the first part of the question, i.e. retaining all values of x and y along the way (in a 1xn array) as the loop is still running/prompting the user for inputs?
Image Analyst
Image Analyst el 10 de Ag. de 2014
Just use a counter that you increment each time:
counter = 1;
while...........
x(counter) = ..............
y(counter) = ...............
counter = counter + 1;
end
Maroulator
Maroulator el 11 de Ag. de 2014
Nice! Thanks again.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 10 de Ag. de 2014

Comentada:

el 11 de Ag. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by