Convert string arrays at any level of cell array or structure


To make your code accept cell arrays and structures that contain strings as input arguments, add a call to convertContainedStringsToChars to the beginning of your code. Then you do not have to make any other changes to code that you had written to work with cell arrays or structures containing character arrays.


B = convertContainedStringsToChars(A) converts string arrays at any level in A.

  • If A is a string array, then B is a character vector or cell array of character vectors.

  • If A is a cell array or a structure, then string arrays in any cell or field of A become character vectors or cell arrays of character vectors in B. All other cells or fields of A are unaltered in B.

  • Otherwise, the function returns A unaltered.


[B1,...,Bn] = convertContainedStringsToChars(A1,...,An) converts the input arguments A1,...,An. For every input argument, there must be a corresponding output argument in B1,...,Bn.


collapse all

Create a cell array containing launch dates, spacecraft names, and planets visited. Some cells contain string arrays, and others do not.

C = {2004,"Messenger","Mercury"; ...
     1977,"Voyager 1",["Jupiter","Saturn"]; ...
     2006,"New Horizons","Pluto"}
C=3×3 cell array
    {[2004]}    {["Messenger"   ]}    {["Mercury"]}
    {[1977]}    {["Voyager 1"   ]}    {1x2 string }
    {[2006]}    {["New Horizons"]}    {["Pluto"  ]}

Convert the strings in C to character vectors.

C = convertContainedStringsToChars(C)
C=3×3 cell array
    {[2004]}    {'Messenger'   }    {'Mercury'}
    {[1977]}    {'Voyager 1'   }    {1x2 cell }
    {[2006]}    {'New Horizons'}    {'Pluto'  }

Note that the function converts the string array ["Jupiter","Saturn"] to a cell array of character vectors, contained in cell C(2,3). To access the contents of the cell, use curly brace indexing.

ans = 1x2 cell
    {'Jupiter'}    {'Saturn'}

Create a structure containing arrays of data, a title, and labels for a plot. Some of the fields contain strings, and others do not.

S.x = linspace(0,2*pi);
S.y = sin(S.x);
S.title = "y = sin(x)";
S.axislabels = ["x (radians)","y"]
S = struct with fields:
             x: [1x100 double]
             y: [1x100 double]
         title: "y = sin(x)"
    axislabels: ["x (radians)"    "y"]

Convert the strings in S.

S = convertContainedStringsToChars(S)
S = struct with fields:
             x: [1x100 double]
             y: [1x100 double]
         title: 'y = sin(x)'
    axislabels: {'x (radians)'  'y'}

Process an arbitrary number of input arrays of different types.

Create an array of doubles, a structure, and a cell array. The structure and cell array contain strings, specified using double quotes.

x = linspace(0,2*pi,8);
S.y = sin(x);
S.title = "y = sin(x)";
C = {{sin(x),"y = sin(x)"},{cos(x),"y = cos(x)"}};

Convert the strings and return all other data unaltered.

[x,S,C] = convertContainedStringsToChars(x,S,C)
x = 1×8

         0    0.8976    1.7952    2.6928    3.5904    4.4880    5.3856    6.2832

S = struct with fields:
        y: [0 0.7818 0.9749 0.4339 -0.4339 -0.9749 -0.7818 -2.4493e-16]
    title: 'y = sin(x)'

C=1×2 cell array
    {1x2 cell}    {1x2 cell}

Display the contents of the cell array contained in the first cell, C(1). The function converts strings that are in nested cells or nested structures to character vectors. Note that character vectors display with single quotes.

ans=1×2 cell array
    {1x8 double}    {'y = sin(x)'}

Input Arguments

collapse all

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

If A is a cell array or a structure, then it can be nested. A cell can contain another cell array, and a structure can have a field that is another structure. convertContainedStringsToChars converts every element or field value that is a string into a corresponding character vector or cell array of character vectors. The function converts each element or field value using the rules defined by the convertStringsToChars function, traversing every level and combination of nested containers.

Introduced in R2018b