Storing strings in cell arrays, MATLAB in Engineering

Storing Strings in Cell Arrays:

The one good application of a cell array is to store strings of various lengths. As cell arrays can store various types of values in the elements, that means that the strings of various lengths can be stored in the elements.

>> names = {'Sue', 'Cathy', 'Xavier'}

names =

  'Sue'   'Cathy'   'Xavier'

 

This is very useful, as unlike vectors of strings generated using char or strvcat, such strings do not have extra trailing blanks.

The length of each string can be exhibited using a for loop to loop through the elements of the cell array:

 

>> for i = 1:length(names)

disp(length(names{i}))

  end

3

5

6

 

It is possible to convert a cell array of strings to a character array, and vice versa. The MATLAB has numerous functions which facilitate this. For illustration, the function cellstr converts from a character array padded with blanks to the cell array in which the trailing blanks have been eliminated.

>> greetmat = char('Hello','Goodbye');

>> cellgreets = cellstr(greetmat)

cellgreets =

'Hello'

'Goodbye'

 

The char function can convert a cell array to the character matrix:

>> names = {'Sue', 'Cathy', 'Xavier'};

>> cnames = char(names)

cnames =

Sue

Cathy

Xavier

>> size(cnames)

ans =

  3    6

Posted Date: 10/20/2012 5:50:25 AM | Location : United States







Related Discussions:- Storing strings in cell arrays, Assignment Help, Ask Question on Storing strings in cell arrays, Get Answer, Expert's Help, Storing strings in cell arrays Discussions

Write discussion on Storing strings in cell arrays
Your posts are moderated
Related Questions
Function rmfield - structure: The function rmfield eliminates a field from the structure. It returns a new structure with field eliminated, but does not modify the original st

Initializing the data structure - Function: Function is shown as:   >> printcylvols(cyls) Cylinder x has a volume of 169.6 Cylinder a has a volume of 100.5

Illustration of Matrix solutions: For illustration, consider the three equations below with 3unknowns x 1 ,x 2 , and x 3 : We can write this in the form Ax = b here A

Vectors of Structures: In numerous applications, involving database applications, information generally would be stored in the vector of structures, instead of in individual s

num2str function: The num2str function, that converts real numbers, can be called in many ways. If only the real number is passed to the num2str function, it will generate a s

Symbolic Variables and expressions: The MATLAB has a type known as sym for the symbolic variables and expressions; these work with strings. The illustration, to generate a sym

Illustration of Image processing: This displays that there are 64 rows, or in another word, 64 colors, in this specific colormap. It also displays that the first five colors a

Matrix solutions to systems of the linear algebraic equations: The linear algebraic equation is an equation of the form a 1 x 1 + a 2 x 2 + a 3 x 3    .  .  .  .  a n x n

Gauss-Jordan: The Gauss-Jordan elimination technique begins in similar way which the Gauss elimination technique does, but then rather than of back-substitution, the eliminati

str2num function - String: The function str2num does the opposite; it takes the string in which a number is stored and converts it to the type double: >> num = str2num('123.