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 Center および File ExchangeGet 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