Update Your Code to Accept Strings - MATLAB & Simulink (original) (raw)

If you write code for other MATLAB® users, then it is to your advantage to update your API to accept string arrays, while maintaining backward compatibility with other text data types. String adoption makes your code consistent with MathWorks® products.

If your code has few dependencies, or if you are developing new code, then consider using string arrays as your primary text data type for better performance. In that case, best practice is to write or update your API to accept input arguments that are character vectors, cell arrays of character vectors, or string arrays.

For the definitions of string array and other terms, see Terminology for Character and String Arrays.

What Are String Arrays?

In MATLAB, you can store text data in two ways. One way is to use a character array, which is a sequence of characters, just as a numeric array is a sequence of numbers. The other way is to store a sequence of characters in a string. You can store multiple strings in a string array. For more information, seeCharacters and Strings.

When your code has many dependencies, and you must maintain backward compatibility, follow these approaches for updating functions and classes to present a compatible API.

Functions

Classes

How to Adopt String Arrays in Old APIs

You can adopt strings in old APIs by accepting string arrays as input arguments, and then converting them to character vectors or cell arrays of character vectors. If you perform such a conversion at the start of a function, then you do not need to update the rest of it.

The convertStringsToChars function provides a way to process all input arguments, converting only those arguments that are string arrays. To enable your existing code to accept string arrays as inputs, add a call toconvertStringsToChars 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 usingconvertStringsToChars. Leave the rest of your code unaltered.

function y = myFunc(a,b,c) [a,b,c] = convertStringsToChars(a,b,c); <line 1 of original code> <line 2 of original code> ...

In this example, the arguments [a,b,c] overwrite the input arguments in place. If any input argument is not a string array, then it is unaltered.

If myFunc accepts a variable number of input arguments, then process all the arguments specified by varargin.

function y = myFunc(varargin) [varargin{:}] = convertStringsToChars(varargin{:}); ...

Performance Considerations

The convertStringsToChars function is more efficient when converting one input argument. If your function is performance sensitive, then you can convert input arguments one at a time, while still leaving the rest of your code unaltered.

function y = myFunc(a,b,c) a = convertStringsToChars(a); b = convertStringsToChars(b); c = convertStringsToChars(c); ...

When your code has few dependencies, or you are developing entirely new code, consider using strings arrays as the primary text data type. String arrays provide good performance and efficient memory usage when working with large amounts of text. Unlike cell arrays of character vectors, string arrays have a homogeneous data type. String arrays make it easier to write maintainable code. To use string arrays while maintaining backward compatibility to other text data types, follow these approaches.

Functions

Classes

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 usingconvertCharsToStrings.

function y = myFunc(a,b,c) [a,b,c] = convertCharsToStrings(a,b,c); <line 1 of original code> <line 2 of original code> ...

In this example, the arguments [a,b,c] overwrite the input arguments in place. If any input argument is not a character vector or cell array of character vectors, then it is unaltered.

If myFunc accepts a variable number of input arguments, then process all the arguments specified by varargin.

function y = myFunc(varargin) [varargin{:}] = convertCharsToStrings(varargin{:}); ...

Performance Considerations

The convertCharsToStrings function is more efficient when converting one input argument. If your function is performance sensitive, then you can convert input arguments one at a time, while still leaving the rest of your code unaltered.

function y = myFunc(a,b,c) a = convertCharsToStrings(a); b = convertCharsToStrings(b); c = convertCharsToStrings(c); ...

How to Manually Convert Input Arguments

If it is at all possible, avoid manual conversion of input arguments that contain text, and instead use theconvertStringsToChars 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
(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) |

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 thestrlength function.

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

Note

Do 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)

Check for Empty String Arrays

An empty string array is, in fact, an empty array—that is, an array that has at least one dimension whose length is 0.

The recommended way to create an empty string array is to use thestrings function, specifying 0 as at least one of the input arguments. The isempty function returns1 when the input is an empty string array.

str = strings(0); tf = isempty(str)

The strlength function returns a numeric array that is the same size as the input string array. If the input is an empty string array, thenstrlength returns an empty array.

str = strings(0); L = strlength(str)

Check for Missing Strings

String arrays also can contain missing strings. The missing string is the string equivalent to NaN for numeric arrays. It indicates where a string array has missing values. The missing string displays as<missing>, with no quotation marks.

You can create missing strings using the missing function. The recommended way to check for missing strings is to use theismissing function.

str = string(missing); tf = ismissing(str)

Note

Do not check for missing strings by comparing a string to the missing string.

The missing string is not equal to itself, just as NaN is not equal to itself.

str = string(missing); f = (str == missing)

Terminology for Character and String Arrays

MathWorks documentation uses these terms to describe character and string arrays. For consistency, use these terms in your own documentation, error messages, and warnings.

See Also

char | cellstr | string | strings | convertStringsToChars | convertCharsToStrings | isstring | isStringScalar | ischar | iscellstr | strlength | validateattributes | convertContainedStringsToChars

Topics