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

function imread: The function imread can read an image file, for illustration a JPEG (.jpg) file. The function reads color images into a 3-dimensional matrix. >> myimage1

Uses of Function handles: The Function handles can also be generated for functions other than anonymous functions, both built-in & user-defined functions. For illustration, th

Use of Nested if-else statements: By using the nested if-else to select from among the three possibilities, not all the conditions should be tested. In this situation, if x is

Function cirarea - Anonymous functions: The function handle name is cirarea. The one argument is passed to the input argument radius. The body of the function is an expression

Polyhedron - graphics objects: The field polyhedron.vertices is a matrix in which each row presents (x,y,z) points. The field polyhedron.faces defines the faces: for illustrat

Help command: The help command is used with the script rectarea, the function readlenwid, and the major function printrectarea. To see the first comment in the subfunction, as

Replacing, Finding, and separating strings: There are numerous functions which find and replace the strings, or parts of strings, within the other strings and functions which

Binary Search: The binary search supposes that the vector has been sorted first. The algorithm is just similar to the way it works whenever looking for a name in a phone direc

analyzing traffic; determine motion of flow; calculate tracklets; detect abnormalities;