Borrar filtros
Borrar filtros

while lopps. help plz ´,

1 visualización (últimos 30 días)
Jian Gui
Jian Gui el 23 de Jun. de 2014
Comentada: Jian Gui el 24 de Jun. de 2014
Hi all. I am trying to make a while loops that will keep on running if the user input is a str, a number less and equal 0 or the number is longer the length of my vector. But it doesn't work, plz help:)
deleterowStr=input('Please enter the number of the row:','s');
deleterow=str2num(deleterowStr);
while ~((~isempty( deleterow) && deleterow <=0 || deleterow > length(W)) || strcmpi(deleterowStr,'')==1)
disp('Invaild row number')
deleterowstr=input('Please enter the number of the row:','s');
deleterow=str2num(deleterowstr);
end
thx:)
  1 comentario
Joseph Cheng
Joseph Cheng el 23 de Jun. de 2014
anyways. I would suggest put a break point in and see why you do not meet the criteria to breakout of the loop.

Iniciar sesión para comentar.

Respuesta aceptada

Joseph Cheng
Joseph Cheng el 23 de Jun. de 2014
okay so doing a quick testing with different inputs the issue is when you put in an input like 'a'. where the logic checks would become empty. So what i did is broke it out and when the deleterow number is empty put one of the other conditionals so it would loop again. It would break out of the loop iff none of the loop conditions are met.
W=[1 2 3 4 5]
deleterowStr=input('Please enter the number of the row:','s');
deleterow=str2num(deleterowStr);
while 1
inputEmpty = isempty(deleterow);
if inputEmpty==1
deleterow = -1;
end
inputNeg = deleterow<=0;
input2Long = deleterow>length(W);
inputBlank = strcmpi(deleterowStr,'')==1;
if sum([inputEmpty inputNeg input2Long inputBlank])==0
break
end
disp('Invaild row number')
deleterowstr=input('Please enter the number of the row:','s');
deleterow=str2num(deleterowstr);
end
W(deleterow) = []
  2 comentarios
Joseph Cheng
Joseph Cheng el 23 de Jun. de 2014
condensed version
W=[1 2 3 4 5]
deleterowStr=input('Please enter the number of the row:','s');
deleterow=str2num(deleterowStr);
if isempty(deleterow)
deleterow = -1;
end
while (deleterow<=0 || deleterow > length(W)) || strcmpi(deleterowStr,'')==1
disp('Invaild row number')
deleterowstr=input('Please enter the number of the row:','s');
deleterow=str2num(deleterowstr);
if isempty(deleterow)
deleterow = -1;
end
end
W(deleterow) = []
Jian Gui
Jian Gui el 24 de Jun. de 2014
works perfectly thx:)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by