Documentation

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.

convertCharsToStrings

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.

Syntax

B = convertCharsToStrings(A)
[B1,...,Bn] = convertCharsToStrings(A1,...,An)

Description

example

B = convertCharsToStrings(A) converts A to a string array if A is a character array or a cell array of character vectors. If A has any other data type, then convertCharsToStrings returns A unaltered.

example

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

Examples

collapse all

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

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

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

C = {'Venus','Earth','Mars'}
C = 1x3 cell array
    {'Venus'}    {'Earth'}    {'Mars'}

str = convertCharsToStrings(C)
str = 1x3 string array
    "Venus"    "Earth"    "Mars"

Process an arbitrary number of input arrays of different types, converting only the character arrays to string 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 character array and leave the other arrays unaltered.

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

     1     2     3

newStr = 1x3 string array
    "Mercury"    "Gemini"    "Apollo"

newB = 

     2     5
     7     6

newC = 1x2 string 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 character vector, then B is a string scalar.

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

  • If A is a character array with multiple rows, then all the rows are concatenated and B is returned as a string scalar. For example, ['fi';'le'] is returned as "flie".

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

Tips

  • 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 them all 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{:});
        ...

Extended Capabilities

Introduced in R2017b

Was this topic helpful?