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 次查看(过去 30 天)
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 个评论
Andrew Jones
Andrew Jones 2019-7-11
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 2019-7-11
编辑:Stephen23 2019-7-11
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.

请先登录,再进行评论。

采纳的回答

Raj
Raj 2019-7-11
编辑:Raj 2019-7-11
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by