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!