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
Example of Interpolation and extrapolation: The MATLAB has a function to do this, known as polyfit. The function polyfit finds the coefficients of the polynomial of the partic

Function fieldnames - structure functions: The function fieldnames will return the names of the fields which are contained in the structure variable. >> pack_fields = fiel

Dot Product: The dot or inner product of two vectors a and b is written as a • b and is defined as  In another words, this is like matrix multiplication when multiplyi

Algorithm for expfn function: The algorithm for expfn function is as shown:  receives the value of x as the input argument.  Prints the value of exp(x).  assigns a

Illustration of Sorting strings: To sort on the rows rather than second dimension should be specified. >> sort(words,2) ans = Hello Hdowy Hi Gbdeo

IS Functions in Matlab: There are many functions which are built into MATLAB which test whether or not something is true; these function names start with the word is. As these

Gauss Elimination: The Gauss elimination technique consists of:    Generating the augmented matrix [A b]    Applying EROs to augmented matrix to obtain an upper trian

Converting between the String and Number types: The MATLAB has many functions which convert numbers to strings in which each character element is a separate digit, and vice ve

Displaying expressions: The good-looking function will show such expressions by using exponents; for illustration, >> b = sym('x^2') b = x^2 >> pretty(b)

calcrectarea subfunction: function call: area = calcrectarea(len,wid); function header: function area = calcrectarea(len, wid)   In the function call, the two arg