Functions
• Accept any text data types as input arguments.
• If an input argument can be a string array, then also allow it to be a character vector or cell
array of character vectors.
• Accept character arrays as both names and values in name-value pair arguments.
• In name-value pair arguments, allow names to be specified as either character vectors or
strings—that is, with either single or double quotes around the name. If a value can be a string
array, then also allow it to be a character vector or cell array of character vectors.
• Do not accept cell arrays of string arrays for text input arguments.
• A cell array of string arrays has a string array in each cell. While you can create such a cell
array, it is not recommended for storing text. If your code uses strings as the primary text data
type, store multiple pieces of text in a string array, not a cell array of string arrays.
However, if your code accepts heterogeneous cell arrays as inputs, then consider accepting cell
arrays that contain strings.
• In general, return strings.
• If your function returns output arguments that are text, then return them as string arrays.
• Return the same data type when the function modifies input text.
• If your function modifies input text and returns the modified text as the output argument, then
the input and output arguments should have the same data type.
Classes
• Treat methods as functions.
• Accept character vectors and cell arrays of character vectors as input arguments, as described
in the previous section. In general, return strings as outputs.
• Specify properties as string arrays.
• If a property contains text, then set the property using a string array. When you access the
property, return the value as a string array.
How to Maintain Compatibility in New Code
When you write new code, or modify code to use string arrays as the primary text data type, maintain
backward compatibility with other text data types. You can accept character vectors or cell arrays of
character vectors as input arguments, and then immediately convert them to string arrays. If you
perform such a conversion at the start of a function, then the rest of your code can use string arrays
only.
The convertCharsToStrings function provides a way to process all input arguments, converting
only those arguments that are character vectors or cell arrays of character vectors. To enable your
new code to accept these text data types as inputs, add a call to convertCharsToStrings at the
beginnings of your functions and methods.
For example, if you have defined a function myFunc that accepts three input arguments, process all
three inputs using convertCharsToStrings.
Update Your Code to Accept Strings
6-67
Précédent

- 249/1450

Suivant