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 件のコメント
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
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 Exchange で Get Started with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!