Limiting number of characters to three positions (ranging from 0–150)

3 vues (au cours des 30 derniers jours)
Lisa M
Lisa M le 9 Fév 2020
Commenté : Walter Roberson le 16 Fév 2020
The code below limits number of characters to three positions ranging from 0 to 100.
if length(string) <= 1 || strcmp([string, char], '100')
string = [string, char];
end
However, if I try:
if length(string) <= 1 || strcmp([string, char], '150')
string = [string, char];
end
Number of characters are limited to two positions and range from 0 to 99.
If I try:
if length(string) <= 2 || strcmp([string, char], '150')
string = [string, char];
end
Number of characters are limited to three positions and range from 0 to 999.
I would like characters to be limited to three positions ranging from 0-150. Any suggestions? Thanks!

Réponse acceptée

Walter Roberson
Walter Roberson le 9 Fév 2020
Or in
(string(1)=='1' & string(2) <='4')
  4 commentaires
Lisa M
Lisa M le 16 Fév 2020
Thanks your suggestion helped! The below line gives me numbers from 0 until 159 now. It somehow still does not limit the response to 150...
((length(string)<= 1 || string(1)=='1' && string(2)<='5') && (length(string)<= 2 || string(2)<='5' && string(3)=='0'))
Walter Roberson
Walter Roberson le 16 Fév 2020
(isempty(string) && char ~= '0') || length(string) == 1 || (length(string) == 2 && string(1)=='1' && string(2) <='4') || strcmp([string, char], '150')
This code ignores leading 0 as well.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by