MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

How to Manually Convert Input Arguments


If it is at all possible, avoid manual conversion of input arguments that contain text, and
instead use the convertStringsToChars or convertCharsToStrings functions.
Checking the data types of input arguments and converting them yourself is a tedious
approach, prone to errors.

If you must convert input arguments, then use the functions in this table.

Conversion Function
String scalar to character vector char
String array to cell array of character
vectors

cellstr

Character vector to string scalar string
Cell array of character vectors to string
array

string

How to Check Argument Data Types


To check the data type of an input argument that could contain text, consider using the
patterns shown in this table.

Required Input Argument
Type

Old Check New Check

Character vector or string
scalar

ischar(X) ischar(X) ||
isStringScalar(X)
validateattributes(X,
{'char','string'},
{'scalartext'})
Character vector or string
scalar

validateattributes(X,
{'char'},{'row'})

validateattributes(X,
{'char','string'},
{'scalartext'})
Nonempty character vector
or string scalar

ischar(X) &&
~isempty(X)

(ischar(X) ||
isStringScalar(X)) &&
strlength(X) ~= 0

6 Characters and Strings

Free download pdf