convertStringsToChars - Convert string arrays to character arrays, leaving other arrays
unaltered - MATLAB ([original](https://in.mathworks.com/help/matlab/ref/convertstringstochars.html)) ([raw](?raw))
Convert string arrays to character arrays, leaving other arrays unaltered
Syntax
Description
When working with your own code, you can useconvertStringsToChars
to make your code accept string inputs. Then you do not have to make any other changes to code that you had written to work with character arrays.
[B](#d126e305051) = convertStringsToChars([A](#d126e305030))
converts A
to a character vector or a cell array of character vectors if A
is a string array. Otherwise,convertStringsToChars
returns A
unaltered.
[B1,...,Bn] = convertStringsToChars(A1,...,An)
converts any string arrays in A1,...,An
to character vectors or cell arrays of character vectors, and then returns them as the corresponding output arguments inB1,...,Bn
. If any of the argumentsA1,...,An
has any other data type, thenconvertStringsToChars
returns it unaltered.
Examples
Create a string scalar and convert it to a character vector.
chr = convertStringsToChars(str)
Convert a string array to a cell array of character vectors.
str = ["Venus","Earth","Mars"]
str = 1×3 string "Venus" "Earth" "Mars"
C = convertStringsToChars(str)
C = 1×3 cell {'Venus'} {'Earth'} {'Mars'}
Process an arbitrary number of input arrays of different types, converting only the string arrays to character arrays.
Create a set of numeric, character, and string arrays.
str = ["Mercury","Gemini","Apollo"]
str = 1×3 string "Mercury" "Gemini" "Apollo"
C = 1×2 cell {'volts'} {'amps'}
Convert the string array and return the other arrays unaltered.
[newA,newStr,newB,newC] = convertStringsToChars(A,str,B,C)
newStr = 1×3 cell {'Mercury'} {'Gemini'} {'Apollo'}
newC = 1×2 cell {'volts'} {'amps'}
Input Arguments
Input array, specified as an array of any size or data type.
Output Arguments
Output array. The data type of the output array depends on the data type of the input array, A
.
- If
A
is a string scalar, thenB
is a character vector. - If
A
is a string array of any other size, thenB
is a cell array of character vectors that has the same size. - If
A
has any other data type, thenB
is identical toA
.
If A
is a string array, thenconvertStringsToChars
converts any element that is:
- An empty string (displayed as
""
) to a0
-by-0
character array (displayed as''
) - A missing string (displayed as
<missing>
) to a0
-by-0
character array
If A
is an empty string array, thenB
is an empty cell array. An empty array has at least one dimension whose size is 0
.
Tips
- To enable your existing code to accept string arrays as input, add a call to
convertStringsToChars
at the beginning of your code.
For example, if you have defined a functionmyFunc
that accepts three input arguments, process all three inputs usingconvertStringsToChars
. Leave the rest of your code unchanged.
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 output arguments[a,b,c]
overwrite the input arguments in place. If any input argument is not a string array, then it is unaltered.
IfmyFunc
accepts a variable number of input arguments, then process all the arguments specified byvarargin
.
function y = myFunc(varargin)
[varargin{:}] = convertStringsToChars(varargin{:});
... - The
convertStringsToChars
function is more efficient when converting one input argument. If performance is a concern, then callconvertStringsToChars
on one input argument at a time, rather than calling it once on multiple inputs.
Extended Capabilities
Version History
Introduced in R2017b