This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Convert string arrays to character arrays, leaving other arrays unaltered

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)
[B1,...,Bn] = convertStringsToChars(A1,...,An)



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.


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


collapse all

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

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

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

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

C = convertStringsToChars(str)
C = 1x3 cell array
    {'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     2     3

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

B = [2 5; 7 6]
B = 

     2     5
     7     6

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

Convert the string array and return the other arrays unaltered.

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

     1     2     3

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

newB = 

     2     5
     7     6

newC = 1x2 cell array
    {'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.


  • 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 them all using convertStringsToChars. Leave the rest of your code unchanged.

    function y = myFunc(a,b,c)
        [a,b,c] = convertStringToChars(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{:});

Extended Capabilities

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

Introduced in R2017b

Was this topic helpful?