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.

Cell Arrays of Character Vectors

Convert to Cell Array of Character Vectors

When you create character arrays from character vectors, all the vectors must have the same length. This often means that you have to pad blanks at the end of character vectors to equalize their length. However, another type of MATLAB® array, the cell array, can hold different sizes and types of data in an array without padding. A cell array of character vectors is a cell array where every cell contains a character vector. Cell array of strings is another frequently-used term for such a cell array. Cell arrays of character vectors provide a more flexible way to store character vectors of varying lengths.

Convert a character array to a cell array of character vectors. data is padded with spaces so that each row has an equal number of characters. Use cellstr to convert the character array.

data = ['Allison Jones';'Development  ';'Phoenix      '];
celldata = cellstr(data)
celldata = 

    'Allison Jones'
    'Development'
    'Phoenix'

data is a 3-by-13 character array, while celldata is a 3-by-1 cell array of character vectors. cellstr also strips the blank spaces at the ends of the rows of data.

The iscellstr function determines if the input argument is a cell array of character vectors. It returns a logical 1 (true) in the case of celldata:

iscellstr(celldata)
ans =

     1

Use char to convert back to a padded character array.

chr = char(celldata)
chr =

Allison Jones
Development  
Phoenix      
length(chr(3,:))
ans =

    13

For more information on cell arrays, see Access Data in a Cell Array.

Functions for Cell Arrays of Character Vectors

This table describes the MATLAB functions for working with cell arrays of character vectors.

Function

Description

cellstr

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

char

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

deblank

Remove trailing blanks from a character array.

iscellstr

Return true for a cell array of character arrays.

sort

Sort elements in ascending or descending order.

strcat

Concatenate character arrays or cell arrays of character arrays.

strcmp

Compare character arrays or cell arrays of character arrays.

You can also use the following set functions with cell arrays of character vectors.

Function

Description

intersect

Set the intersection of two vectors.

ismember

Detect members of a set.

setdiff

Return the set difference of two vectors.

setxor

Set the exclusive OR of two vectors.

union

Set the union of two vectors.

unique

Set the unique elements of a vector.

Was this topic helpful?