Main Content

convertStringsToChars

Convert string arrays to character arrays, leaving other arrays unaltered

Description

When working with your own code, you can use convertStringsToChars 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 = convertStringsToChars(A) converts A to a character vector or a cell array of character vectors if A is a string array. Otherwise, convertStringsToChars returns A unaltered.

example

[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 in B1,...,Bn. If any of the arguments A1,...,An has any other data type, then convertStringsToChars returns it unaltered.

example

Examples

collapse all

Create a string scalar and convert it to a character vector.

str = "Mercury"
str = 
"Mercury"
chr = convertStringsToChars(str)
chr = 
'Mercury'

Convert a string array to a cell array of character vectors.

str = ["Venus","Earth","Mars"]
str = 1x3 string
    "Venus"    "Earth"    "Mars"

C = convertStringsToChars(str)
C = 1x3 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.

A = [1 2 3]
A = 1×3

     1     2     3

str = ["Mercury","Gemini","Apollo"]
str = 1x3 string
    "Mercury"    "Gemini"    "Apollo"

B = [2 5; 7 6]
B = 2×2

     2     5
     7     6

C = {'volts','amps'}
C = 1x2 cell
    {'volts'}    {'amps'}

Convert the string array and return the other arrays unaltered.

[newA,newStr,newB,newC] = convertStringsToChars(A,str,B,C)
newA = 1×3

     1     2     3

newStr = 1x3 cell
    {'Mercury'}    {'Gemini'}    {'Apollo'}

newB = 2×2

     2     5
     7     6

newC = 1x2 cell
    {'volts'}    {'amps'}

Input Arguments

collapse all

Input array, specified as an array of any size or data type.

Output Arguments

collapse all

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, then B is a character vector.

  • If A is a string array of any other size, then B is a cell array of character vectors that has the same size.

  • If A has any other data type, then B is identical to A.

If A is a string array, then convertStringsToChars converts any element that is:

  • An empty string (displayed as "") to a 0-by-0 character array (displayed as '')

  • A missing string (displayed as <missing>) to a 0-by-0 character array

If A is an empty string array, then B 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 function myFunc that accepts three input arguments, process all three inputs using convertStringsToChars. 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.

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

    function y = myFunc(varargin)
        [varargin{:}] = convertStringsToChars(varargin{:});
        ...
  • The convertStringsToChars function is more efficient when converting one input argument. If performance is a concern, then call convertStringsToChars on one input argument at a time, rather than calling it once on multiple inputs.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2017b