MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
Required Input Argument
Type

Old Check New Check

(ischar(X) ||
isStringScalar(X)) &&
X ~= ""
Cell array of character
vectors or string array

iscellstr(X) iscellstr(X) ||
isstring(X)
Any text data type ischar(X) ||
iscellstr(X)

ischar(X) ||
iscellstr(X) ||
isstring(X)

Check for Empty Strings


An empty string is a string with no characters. MATLAB displays an empty string as a pair
of double quotes with nothing between them (""). However, an empty string is still a 1-
by-1 string array. It is not an empty array.


The recommended way to check whether a string is empty is to use the strlength
function.


str = "";
tf = (strlength(str) ~= 0)


NoteDo not use the isempty function to check for an empty string. An empty string has
no characters but is still a 1-by-1 string array.


The strlength function returns the length of each string in a string array. If the string
must be a string scalar, and also not empty, then check for both conditions.


tf = (isStringScalar(str) && strlength(str) ~= 0)


If str could be either a character vector or string scalar, then you still can use
strlength to determine its length. strlength returns 0 if the input argument is an
empty character vector ('').


tf = ((ischar(str) || isStringScalar(str)) && strlength(str) ~= 0)


Update Your Code to Accept Strings
Free download pdf