Convert character arrays to string arrays, leaving other arrays unaltered
When working with your own code, you can use
convertCharsToStrings
to make your code accept character
arrays. Then you do not have to make any other changes to code you had written to work
with string arrays.
[B1,...,Bn] = convertCharsToStrings(A1,...,An)
converts any
character arrays or cell arrays of character vectors in A1,...,An
to string arrays, and then returns them as the corresponding output arguments in
B1,...,Bn
. If any of the arguments
A1,...,An
has any other data type, then
convertCharsToStrings
returns it unaltered.
To enable code that works with strings to accept character arrays as inputs,
add a call to convertCharsToStrings
at the beginning of
your code.
For example, if you have defined a function myFunc
that
accepts three input arguments, process all three inputs using
convertCharsToStrings
. Leave the rest of your code
unchanged.
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 output arguments [a,b,c]
overwrite the
input arguments in place. If any input argument is not a character array or a
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{:}); ...
The convertCharsToStrings
function is more efficient when
converting one input argument. If performance is a concern, then call
convertCharsToStrings
on one input argument at a time,
rather than calling it once on multiple inputs.
convertContainedStringsToChars
| convertStringsToChars
| iscellstr
| ischar
| ismissing
| isstring
| isStringScalar
| string
| varargin