convertCharsToStrings

Convert character arrays to string arrays, leaving other arrays unaltered

Syntax

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

Description

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.

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 {'Venus'} {'Earth'} {'Mars'} ```
`str = convertCharsToStrings(C)`
```str = 1x3 string "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×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 character array and leave the other arrays unaltered.

`[newA,newStr,newB,newC] = convertCharsToStrings(A,str,B,C)`
```newA = 1×3 1 2 3 ```
```newStr = 1x3 string "Mercury" "Gemini" "Apollo" ```
```newB = 2×2 2 5 7 6 ```
```newC = 1x2 string "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 the columns of `A` are concatenated and `B` is returned as a string scalar. For example, the 3-by-2 character array `['Xx';'Yy';'Zz']` is converted to `"XYZxyz"`.

• 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 all three inputs 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{:}); ...```
• The `convertCharsToStrings` function is more efficient when converting one input argument. If performance is a concern, then call `convertCharsToStrings` on one input argument at a time, rather than calling it once on multiple inputs.

Version History

Introduced in R2017b