You can use the colon operator to generate a character vector containing each lower case letter in the alphabet:
>> 'a':'z'
ans =
'abcdefghijklmnopqrstuvwxyz'
Same with upper case letters:
>> 'A':'Z'
ans =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
Or this, if you want:
>> 'A':'z'
ans =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz'
See here for more info. You can use double() and char() to switch between number and corresponding character: >> double('a')
ans =
97
>> char(97)
ans =
'a'
So, 'a':'z' is kind of like
>> char(double('a'):double('z'))
ans =
'abcdefghijklmnopqrstuvwxyz'
When you compare characters,
>> 'a' < 'z'
ans =
logical
1
this is like
>> double('a') < double('z')
ans =
logical
1
or rather
>> 97 < 122
ans =
logical
1
In your professor's code:
Here, each character within str is being compared to 'A' and 'Z'. The output is a logical array with the same size as str, containing a 1 where the corresponding character of str is between 'A' and 'Z' (i.e. somewhere within the array 'A':'Z').
>> 'GHSIEVBSK' >= 'A' & 'GHSIEVBSK' <= 'Z'
ans =
1×9 logical array
1 1 1 1 1 1 1 1 1
Here, str has all capital letters, so every element in the output is true.
>> 'GhSIeVBsK' >= 'A' & 'GhSIeVBsK' <= 'Z'
ans =
1×9 logical array
1 0 1 1 0 1 1 0 1
Here, some characters in str are lower-case. The corresponding elements in the output are false. With
if sum(str >= 'A' & str <= 'Z') == length(str)
your professor is checking that every element in the output is a 1. Also valid would be
if all(str >= 'A' & str <= 'Z')
Take another look at the documentation for contains. >> contains('abc', 'a':'z')
ans =
logical
0
This returns 0 because the entirety of 'a':'z' is not found within 'abc'. To check if any element of 'a':'z' is within 'abc', you could use for example
>> contains('abc', cellstr(('a':'z')')')
ans =
logical
1
Best Answer