How to restrict input
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi everyone, I am new to MATLAB, and I have a question on how to restrict inputs on Matlab. I want to make sure you that my input is exactly a 5 digit number, and it cannot be alphabets.
Thank you
댓글 수: 0
채택된 답변
Adam Danz
2021년 2월 12일
편집: Adam Danz
2021년 2월 15일
> I want to make sure you that my input is exactly a 5 digit number, and it cannot be alphabets
Assuming "5 digits" means an integer (positive or negative)
% Function to test x
testx = @(x)isnumeric(x) && ... % x must be a number
isscalar(x) && ... % x must be scalar
mod(x,1)==0 && ... % x must be an integer
abs(x)<100000 && abs(x)>9999; % x must have 5 digits (pos or neg)
% Some tests
testx(12345)
testx(-12345)
testx('a')
testx(3.1213)
testx(00001)
testx(99999)
testx([12345 54321])
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!