Main Content

convertContainedStringsToChars

Convert string arrays at any level of cell array or structure

Description

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.

example

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

example

Examples

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"   ]}    {["Jupiter"    "Saturn"]}
    {[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.

C{2,3}
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: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 ... ] (1x100 double)
             y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 ... ] (1x100 double)
         title: "y = sin(x)"
    axislabels: ["x (radians)"    "y"]

Convert the strings in S.

S = convertContainedStringsToChars(S)
S = struct with fields:
             x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 ... ] (1x100 double)
             y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 ... ] (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.

C{1}
ans=1×2 cell array
    {[0 0.7818 0.9749 0.4339 -0.4339 -0.9749 -0.7818 -2.4493e-16]}    {'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.

Extended Capabilities

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2018b