strings ------------------------------------------------------------------------------- (1) Characters + MATLAB's alphabet is essentially keyboard characters - see kb.gif and characters.txt from Lecture 3 + reminders: - characters have integer value from 0 to 65355 - our printable KB characters have values from char(33:126) (ASCII is 0 to 127) - use CHAR(int) to convert the integer to actual character rep - use DOUBLE(string) to convert the string to its integer value ------------------------------------------------------------------------------- (2) Strings + MATLAB Strings - array of characters - in other languages, strings might be a different type of collection!!! + many useful functions see MATLAB's help: HELP STRINGS and HELP STRFUN ------------------------------------------------------------------------------- (3) Empty String + Empty? '' (two single quotes with nothing between) >> size('') % gives 0 rows and 0 cols Note: >> $''==[]$ actually returns an error use ISEMPTY ------------------------------------------------------------------------------- (4) Creating Strings + Creating 1D: - use CHAR (a bit tough) or single quotes ($'$) >> s = 'abc' - see also STRCAT + Creating 2D: - strings split into individual characters for rows/cols >> s = ['abc' ; 'def'] % OK >> s = ['abc' ; 'de'] % not OK - use CHAR to add padding and DEBLANK to remove padding - see also STRVCAT ------------------------------------------------------------------------------- (5) String Operations + You can use arithmetic operators - why? strings are effectively arrays of ints in MATLAB - using ops forces strings to become ints again: >> 'abc' + 'def' + some specific values: double('a') --> 97 double('A') --> 65 >> 'a'-'A' --> 32 >> 'Z'-'A' --> 25 >> 'z'-'a' --> 25 >> double('0123456789') --> [48 49 50 51 52 53 54 56 57] + can use these values to help do things! >> char('b' - ('a'-'A') ) ------------------------------------------------------------------------------- (6) String Comparisons + use STRCMP to compare two strings + why? >> 'abc'=='abc' % output is [1 1 1] >> strcmp('abc','abc') % output is 1 ------------------------------------------------------------------------------- (7) String Functions Taken directly from MATLAB! >> help strfun General. char - Create character array (string). double - Convert string to numeric character codes. cellstr - Create cell array of strings from character array. blanks - String of blanks. deblank - Remove trailing blanks. eval - Execute string with MATLAB expression. String tests. ischar - True for character array (string). iscellstr - True for cell array of strings. isletter - True for letters of the alphabet. isspace - True for white space characters. String operations. strcat - Concatenate strings. strvcat - Vertically concatenate strings. strcmp - Compare strings. strncmp - Compare first N characters of strings. strcmpi - Compare strings ignoring case. strncmpi - Compare first N characters of strings ignoring case. findstr - Find one string within another. strjust - Justify character array. strmatch - Find possible matches for string. strrep - Replace string with another. strtok - Find token in string. upper - Convert string to uppercase. lower - Convert string to lowercase. String to number conversion. num2str - Convert number to string. int2str - Convert integer to string. mat2str - Convert matrix to eval'able string. str2double - Convert string to double precision value. str2num - Convert string matrix to numeric array. sprintf - Write formatted data to string. sscanf - Read string under format control. Base number conversion. hex2num - Convert IEEE hexadecimal to double precision number. hex2dec - Convert hexadecimal string to decimal integer. dec2hex - Convert decimal integer to hexadecimal string. bin2dec - Convert binary string to decimal integer. dec2bin - Convert decimal integer to binary string. base2dec - Convert base B string to decimal integer. dec2base - Convert decimal integer to base B string. -------------------------------------------------------------------------------