Not effective validation algorithm for input format

7 visualizaciones (últimos 30 días)
Destro Katramis
Destro Katramis el 1 de Jun. de 2020
Comentada: Ameer Hamza el 1 de Jun. de 2020
Hello everyone, I am asking as input, coordinates of quadrilateral vertices and I am adding an extra layer for format checking. Simply speaking inputs must be numerical.
vx=zeros(1,4);
vy=zeros(1,4);
vertices=['A'; 'B'; 'C'; 'D'];
fprintf('\nPlease enter the coordinates of vertices:\n');
for cnt=1:4
fprintf('\n For vertix <strong> %s </strong>', vertices(cnt));
fprintf(' :\n');
vx(cnt) = input(' Χ=');
vxtest=isnumeric(vx(cnt));
while vxtest==0
fprintf('ERROR: Non numerical character, try again.\n');
vx(cnt) = input(' Χ=');
vxtest=isnumeric(vx(cnt));
end
vy(cnt) = input(' Y=');
vytest=isnumeric(vy(cnt));
while vytest==0
fprintf('ERROR: Non numerical character, try again.\n');
vy(cnt) = input(' Y=');
vytest=isnumeric(vy(cnt));
end
end
vx=vx(1:4);
vy=vy(1:4);
But still it is possible to put a string as an input and continue to the next coordinate. At the end, there is an output area of a quadrilateral with vertices out of string coordinates.
Thanks in advance!

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 1 de Jun. de 2020
Try this
vx=zeros(1,4);
vy=zeros(1,4);
vertices=['A'; 'B'; 'C'; 'D'];
fprintf('\nPlease enter the coordinates of vertices:\n');
for cnt=1:4
fprintf('\n For vertix <strong> %s </strong>', vertices(cnt));
fprintf(' :\n');
temp = input(' Χ=');
vxtest=isnumeric(temp);
vx(cnt) = temp;
while vxtest==0
fprintf('ERROR: Non numerical character, try again.\n');
temp = input(' Χ=');
vxtest=isnumeric(temp);
vx(cnt) = temp;
end
temp = input(' Y=');
vytest=isnumeric(temp);
vy(cnt) = temp;
while vytest==0
fprintf('ERROR: Non numerical character, try again.\n');
temp = input(' Y=');
vytest=isnumeric(temp);
vy(cnt) = temp;
end
end
vx=vx(1:4);
vy=vy(1:4);
  2 comentarios
Destro Katramis
Destro Katramis el 1 de Jun. de 2020
Editada: Destro Katramis el 1 de Jun. de 2020
I don't know what it was causing it but that fixed it! Thank you very much Ameer!!
Ameer Hamza
Ameer Hamza el 1 de Jun. de 2020
I am glad to be of help!
The problem was caused because vx is a numeric matrix (because of line vx=zeros(1,4)). So when you run the line
vx(cnt) = input(' Χ=');
the character is converted to a numeric value using ASCII table, and isnumeric() function return true.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by