How to restrict input

6 次查看(过去 30 天)
Aldrich Dias
Aldrich Dias 2021-2-11
编辑: Adam Danz 2021-2-15
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

采纳的回答

Adam Danz
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)
ans = logical
1
testx(-12345)
ans = logical
1
testx('a')
ans = logical
0
testx(3.1213)
ans = logical
0
testx(00001)
ans = logical
0
testx(99999)
ans = logical
1
testx([12345 54321])
ans = logical
0

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by