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.

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 inB1,...,Bn. If any of the argumentsA1,...,An has any other data type, thenconvertStringsToChars returns it unaltered.

example

Examples

collapse all

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

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 array, thenconvertStringsToChars converts any element that is:

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

Extended Capabilities

Version History

Introduced in R2017b