1 / 17

Strings Arrays of Strings

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)

Download Presentation

Strings Arrays of Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. StringsArrays 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

  2. 1. Vectors of String • Building lists of strings (“vectors” of strings) • Building a list of strings is not as simple as it sounds: 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') <enter> X = abc defghi jk

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

  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, :)); (Each row, ALL columns) To reference each string, use matrix notation since the vector of strings is really a MATRIX of characters.

  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. (Each row, ALL columns) To reference each string, use matrix notation since the vector of strings is really a MATRIX of characters.

  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

  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)

  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)

  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.

  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…

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

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

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

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

  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)

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

More Related