Not effective validation algorithm for input format

1 view (last 30 days)
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!

Accepted Answer

Ameer Hamza
Ameer Hamza on 1 Jun 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 Comments
Destro Katramis
Destro Katramis on 1 Jun 2020
Edited: Destro Katramis on 1 Jun 2020
I don't know what it was causing it but that fixed it! Thank you very much Ameer!!
Ameer Hamza
Ameer Hamza on 1 Jun 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!