Clear Filters
Clear Filters

I'm using the input command, with multiple inputs. I don't want the user to have to re-enter all the inputs if an error occurs.

2 views (last 30 days)
Here's my script. Thanks :D
%% User Inputs
% Numerous errors are accounted for.
point1=input('Enter the [x,y] coordinate of point 1: ');
if length(point1)~=2
error('Inputs must be in the form [x,y]')
else
point2=input('Enter the [x,y] coordinate of point 2: ');
if length(point2)~=2
error('Inputs must be in the form [x,y]')
elseif point1(1)==point2(1)
error('Inputs must be non-vertical. (Cannot have the same x-value)')
else
point3=input('Enter the [x,y] coordinate of point 3: ');
if length(point3)~=2
error('Inputs must be in the form [x,y]')
elseif point1(1)==point2(1) | point2(1)==point3(1) | point1(1)==point3(1)
error('Inputs must be non-vertical. (Cannot have the same x-value)')
else
end
end
end
  3 Comments
Andrew Jones
Andrew Jones on 11 Jul 2019
im a bit confused regarding your explanation. care to elaborate a little deeper? I'm still a bit of a newbie. Thanks sir
Stephen23
Stephen23 on 11 Jul 2019
Edited: Stephen23 on 11 Jul 2019
Using input like this is a slow way to get input data, and IMO is not a particularly user-friendly way to write code. It also means that functions cannot be automatically tested and makes repeated calls completely impractical.
Consider specifying a config file and importing that, or simply writing a function with clearly specified and tested input/output arguments and leaving it up to the user to decide where their values come from.

Sign in to comment.

Accepted Answer

Raj
Raj on 11 Jul 2019
Edited: Raj on 11 Jul 2019
I would recommend not to use 'error' as program will stop and exit execution everytime an error occurs. Next time it will always start from beginning and user will have to enter all the values again. Instead, you can use a while loop and just display the message to user. This way program keeps on running till user gives correct value. Once the user gives correct value, move to next level. Something like this:
temp=1;
while temp==1
point1=input('Enter the [x,y] coordinate of point 1: ');
if length(point1)~=2
disp('Inputs must be in the form [x,y]')
else
break
end
end
%% Write code to input point 2 and 3 on similar lines.

More Answers (0)

Categories

Find more on Get Started with MATLAB 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!