How to enter negative values ​​and values ​​greater than 9 in input with string.

11 visualizaciones (últimos 30 días)
clear
clc
n=10;
ii=1;
V=0;
display('number between 10 and 20!')
while ii<10
teste = input('Enter a value: ','s')
if strcmp(teste,'End')
display('You cannot type end before you have 10 values entered')
continue;
end
X(ii) = teste
V(ii) = str2num(X(ii))
ii = ii+1;
end
while (ii>10) | (ii<20)
teste = input('Enter a value: ','s')
if strcmp(teste,'End')
display('End Program')
break;
end
X(ii) = teste
V(ii) = str2num(X(ii))
ii = ii+1;
end

Respuesta aceptada

Khalid Mahmood
Khalid Mahmood el 14 de Abr. de 2021
%for 10 values, while ii<11 is needed whe we start with ii=1, and
%string(teste) to convert character array into string
clear,clc
n=10;
ii=1;
V=0;
display('number between 10 and 20!')
while ii<11
teste = input('Enter a value: ','s')
if strcmpi(teste,'End')
display('You cannot type end before you have 10 values entered')
continue;
end
X(ii) = string(teste)
V(ii) = str2num(X(ii))
ii = ii+1;
end
fprintf('\n loop 2:');
while (ii>10) | (ii<20)
teste = input('Enter a value: ','s')
if strcmpi(teste,'End')
display('End Program')
break;
end
X(ii) = teste
V(ii) = str2num(X(ii))
ii = ii+1;
end
  3 comentarios
Adam Danz
Adam Danz el 14 de Abr. de 2021
It's also better to use str2double than str2um. See discussion.
Luccas S.
Luccas S. el 14 de Abr. de 2021
Okay, now it worked. I just had to reverse the order of the vectors X (ii) and V (ii).
X (ii) = str2double (test)
V (ii) = X (ii)
I didn't remember the strcmpi function, thanks.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Extend Testing Frameworks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by