Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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 ==> vertcat CAT arguments dimensions are not consistent.

3 1. Vectors of String, cont. strvcat() – makes a “column vector” of strings >> X = strvcat('abc', 'defghi', 'jk') X = abc defghi jk 3

4 1. Vectors of String, cont. strvcat() – makes a “column vector” of strings >> X = strvcat('abc', 'defghi', 'jk') X = abc defghi jk 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: abc defghi jk

5 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)

6 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)

7 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 list listCpnies = []; % ask for a string cpnyName = input('AE company name (Enter 000 to exit): ', 's'); % while string is not zero-zero-zero while %store at bottom of the list (“vertically concatenate”) %ask for the next string cpnyName = input('next AE Company name: ', 's'); end % display list of strings disp(listCpnies) 8

9 Example: data base % create an empty list listCpnies = []; % ask for a string cpnyName = input('AE company name (Enter 000 to exit): ', 's'); % while string is not zero-zero-zero while ~strcmp(cpnyName, '000') %store at bottom of the list (“vertically concatenate”) listCpnies = strvcat(listCpnies, cpnyName); %ask for the next string cpnyName = input('next AE Company name: ', 's'); end % display list of strings disp(listCpnies) 9

10 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

11 Example: listdlg() From the list of AE companies, convert to a cell-array and ‘pass’ it to a listdlg() function. %convert list of companies to cell-array %create dialog box with the list %continue with code… 11

12 Example: listdlg(), cont. From the list of AE companies, convert to a cell-array and ‘pass’ it to a listdlg() function. 12

13 Example: listdlg(), cont. From the list of AE companies, convert to a cell-array and ‘pass’ it to a listdlg() function. Output: 13 Argument MUST be a cell-array, hence the conversion necessary.

14 Example: listdlg(), end Continue with code.. 14 or

15 Example: listdlg(), end Continue with code.. Use curly braces {} to refer to the selection number the user chose INSIDE the cell array. 15

16 3. Cell-Arrays to a list Note that char() is the reverse of cellstr(), it converts a cell-array-of-strings into a list-of-strings (identical length, ended with spaces) 16

17 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


Download ppt "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."

Similar presentations


Ads by Google