strings arrays of strings

17
1. Normal arrays of characters 2. Converting a list of strings to a cell-arrays of strings 3. Converting a cell-array of strings to a list of strings Strings Arrays of Strings 1

Upload: makenna-goulding

Post on 31-Dec-2015

57 views

Category:

Documents


3 download

DESCRIPTION

Strings Arrays of Strings. Normal arrays of characters Converting a list of strings to a cell-arrays of strings Converting a cell-array of strings to a list of strings. 1. Vectors of String. Building lists of strings (“vectors” of strings) - PowerPoint PPT Presentation

TRANSCRIPT

1. Vectors of String

Building lists of strings (“vectors” of strings)

Building a list of strings is not as simple as it sounds:

2

A = ‘abcd’;B = ‘efg’;C = [A, B];

C =

abcdefg

A = ‘abcd’;B = ‘efg’;C = [A; B];

??? Error using ==> vertcatCAT arguments dimensions are not consistent.

1. Vectors of String, cont.

strvcat() – makes a “column vector” of strings

>> X = strvcat('abc', 'defghi', 'jk') <enter>

X =

abc defghijk

4

Not really a vector – strings are vectors of characters. If combined into a group, it is now a matrix of characters.

Although the 3 strings appear to be of different length, they are not. strvcat() makes each strings identical in length:

a b c

d e f g h i

j k

1. Vectors of String, cont.

Proof by using fprintf()

fprintf('*%s*\n', x(1, :));fprintf('*%s*\n', x(2, :));fprintf('*%s*\n', x(3, :));

5

To reference each string, use matrix notation since the vector of strings is really a MATRIX of characters.

(Each row, ALL columns)

1. Vectors of String, cont.

Proof by using fprintf()

fprintf('*%s*\n', x(1, :));fprintf('*%s*\n', x(2, :));fprintf('*%s*\n', x(3, :));

*abc **defghi**jk *

strvcat() “pads” each string with trailing spaces to make them all fit into one rectangular matrix of characters.

6

To reference each string, use matrix notation since the vector of strings is really a MATRIX of characters.

(Each row, ALL columns)

Example: data base

Prompt the user for names of AE companies. Store all names in one list of strings. Stop prompting when user enters the string ‘000’.

% initialize all data% ask for a string% while string is not zero-zero-zero

%store at bottom of the list%ask for the next string

% display list of strings

7

8

Example: data base% create an empty listlistCpnies = [];

% ask for a stringcpnyName = input('AE company name (Enter 000 to exit): ', 's');

% while string is not zero-zero-zerowhile

%store at bottom of the list (“vertically concatenate”) %ask for the next stringcpnyName = input('next AE Company name: ', 's');

end

% display list of stringsdisp(listCpnies)

9

Example: data base% create an empty listlistCpnies = [];

% ask for a stringcpnyName = input('AE company name (Enter 000 to exit): ', 's');

% while string is not zero-zero-zerowhile ~strcmp(cpnyName, '000')

%store at bottom of the list (“vertically concatenate”)listCpnies = strvcat(listCpnies , cpnyName);%ask for the next stringcpnyName = input('next AE Company name: ', 's');

end

% display list of stringsdisp(listCpnies)

2. Cell-Arrays of String

Why convert a matrix-of-characters to a cell-array-of-strings?

Because many functions (like dialog boxes) require cell-arrays-of-strings as arguments!

cellstr() is a built-in function that easily converts a matrix (or vector) of character to a cell-array of strings.

10

Wrapping Up

Use strvcat() to create lists of strings. Matlab automatically pads with spaces any string to the length it needs to fit in the matrix.

Use cellstr() to convert a list of strings to one cell-array of strings.

Use char() to convert a cell-array of strings to a list of strings.

Examples shown: Prompt use till ‘000’ entered. Add to a list. Use listdlg()

17