why on earth does strlength(string.empty) evaluate to an empty matrix and not 0?
47 views (last 30 days)
Show older comments
Rueben Mendelsberg
on 1 Nov 2024 at 1:26
Moved: Walter Roberson
on 1 Nov 2024 at 19:34
strlength(string.empty) evaluates to an empty matrix but strlength(char.empty), strlength('') and strlength("") all evaluate to 0, why would this be the case?
Accepted Answer
埃博拉酱
on 1 Nov 2024 at 1:57
strlength for string is a vectorized function that can be applied to an array of string:
>> strlength(["a","bb","ccc"])
ans =
1 2 3
So the size of the return value of strlength must be the same as the size of the input string array. "" is a string scalar of size (1,1) so strlength returns a scalar 0; while string.empty is an empty string array of size (0,0) so strlength returns empty (0,0), too.
More Answers (1)
Stephen23
on 1 Nov 2024 at 6:21
Moved: Walter Roberson
on 1 Nov 2024 at 19:34
"strlength(string.empty) evaluates to an empty matrix but strlength(char.empty), strlength('') and strlength("") all evaluate to 0, why would this be the case?"
You are mixing up the size of the container array with the number of characters it contains.
String arrays are container arrays, they contain character vectors. STRLENGTH returns the number of characters in each container and therefore by definition, it always returns an output with exactly the same size as the string array itself.
Character arrays are not container arrays: STRLENGTH returns the number of characters in each row. So an empty character array will return zero.
- CHAR.EMPTY is not a container array, it is an empty character array (which therefore has zero characters). Therefore STRLENGTH is zero.
- '' is not a container array, it is an empty character array (which therefore has zero characters). Therefore STRLENGTH is zero.
- "" is a scalar container array so STRLENGTH will also return a scalar. It contains a character vector with zero characters so STRLENGTH is zero.
- STRING.EMPTY is an empty container array so STRLENGTH will also return an empty array. There is no character vector contained in an empty container array, so zero would make absolutely no sense.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!