Storing strings in cell arrays, MATLAB in Engineering

Assignment Help:

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


Related Discussions:- Storing strings in cell arrays

Plotting from a function, Plotting from a Function: The following func...

Plotting from a Function: The following function creates a Figure Window as shown in figure, which shows various types of plots for similar y vector. The vector is passed as a

Structures, Structures: The Structures are data structures which group...

Structures: The Structures are data structures which group together values which are logically related in what are known as the fields of structure. The benefit of structures

Matrix solutions of the linear algebraic equation, Matrix solutions to syst...

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

Use of built-in colormaps - image processing, Use of built-in colormaps: ...

Use of built-in colormaps: MATLAB has built-in colormaps, it is also possible to generate others by using combinations of any colors. For illustration, the following generates

Cross product of matrix, Cross Product: The cross or outer product a ×...

Cross Product: The cross or outer product a × b of two vectors a and b is defined only whenever both a and b are the vectors in three-dimensional space, that means that they b

Function rmfield - structure, Function rmfield - structure: The functi...

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 used in binary search, Function used in binary search: The fu...

Function used in binary search: The function below implements this binary search algorithm. It receives two arguments: the sorted vector and a key (on the other hand, the func

Expanding a function, Expanding a function: The expand function will m...

Expanding a function: The expand function will multiply out terms, and factor will do the opposite: >> expand((x+2)*(x-1)) ans = x^2 x-2 >> factor(ans)

Deblank function, deblank function: The deblank function eliminates on...

deblank function: The deblank function eliminates only trailing blanks from the string, not leading the blanks. The strtrim function will eliminate both the leading and traili

Illustration of sound signals, Illustration of Sound files: For illust...

Illustration of Sound files: For illustration, the following script generates a subplot which shows the signals from chirp and from train, which is as shown in figure:

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd