We talked how to open, read from, close a text file in MATLAB. We also talked about how to use the SPLIT functionality of the MATLAB built-in function 'regexp'. Some part of the MATLAB session we worked on during the lecture is pasted below. Note that the beginning part of the MATLAB session is missing. Prof. Aydin ...... >> !more nflTeams.txt >> ls elementNames.txt* nestedLoop.m* readNflTeams.m* elements.csv* nestedLoop2.m* setUpNflCellArray.m* input.txt* nflTeams.csv* track.dat* katrina.dat* nflTeams.txt* myFunc.m* readInputFile.m* >> !more input.txt Eagles 70000 50 1 Giants 65000 55 3 Cowboys 80000 60 4 >> !cp input.txt nflTeams.txt >> !more nflTeams.txt Eagles 70000 50 1 Giants 65000 55 3 Cowboys 80000 60 4 >> clear >> clear >> clc >> [fid message] = fopen('nflTeams.txt', 'a') fid = 7 message = '' >> !more nflTeams.txt Eagles 70000 50 1 Giants 65000 55 3 Cowboys 80000 60 4 >> [fid message] = fopen('nflTeams.txt', 'a+') >> flose(fid) ??? Undefined function or method 'flose' for input arguments of type 'double'. >> whos Name Size Bytes Class Attributes fid 1x1 8 double message 0x0 0 char >> clc >> fclose(fid) ans = 0 >> help fclose FCLOSE Close file. ST = FCLOSE(FID) closes the file associated with file identifier FID, which is an integer value obtained from an earlier call to FOPEN. FCLOSE returns 0 if successful or -1 if not. If FID does not represent an open file, or if it is equal to 0 (standard input), 1 (standard output), or 2 (standard error), FCLOSE throws an error. ST = FCLOSE('all') closes all open files, except 0, 1 and 2. See also FOPEN, FERROR, FPRINTF, FREAD, FREWIND, FSCANF, FTELL, FWRITE. Overloaded functions or methods (ones with the same name in other directories) help serial/fclose.m >> fclose(fid) ??? Error using ==> fclose Invalid file identifier. Use fopen to generate a valid file identifier. >> clc >> [fid message] = fopen('nflTeams.txt') fid = 7 message = '' >> fgets(fid) >> !more nflTeams.txt Eagles 70000 50 1 Giants 65000 55 3 Cowboys 80000 60 4 >> fgets(fid) ans = Eagles 70000 50 1 >> fgets(fid) ans = Giants 65000 55 3 >> fgets(fid) ans = Cowboys 80000 60 4 >> fgets(fid) ans = -1 >> fclose(fid) ans = 0 >> [fid message] = fopen('nflTeams.txt') fid = 7 message = '' >> fgetl(fid) ans = Eagles 70000 50 1 >> !more input.txt Eagles 70000 50 1 Giants 65000 55 3 Cowboys 80000 60 4 >> ls elementNames.txt* nestedLoop.m* readNflTeams.m* elements.csv* nestedLoop2.m* setUpNflCellArray.m* input.txt* nflTeams.csv* track.dat* katrina.dat* nflTeams.txt* myFunc.m* readInputFile.m* >> clc >> whos Name Size Bytes Class Attributes ans 1x17 34 char fid 1x1 8 double message 0x0 0 char >> fclose(fid) ans = 0 >> readInputFile >> whos Name Size Bytes Class Attributes ans 1x1 8 double fid 1x1 8 double fileIsOpen 1x1 1 logical lines 1x3 284 cell message 0x0 0 char numLinesRead 1x1 8 double thisLine 1x1 8 double >> celldisp(lines) lines{1} = Eagles 70000 50 1 lines{2} = Giants 65000 55 3 lines{3} = Cowboys 80000 60 4 >> lines{1} ans = Eagles 70000 50 1 >> lines{1,2} ans = Giants 65000 55 3 >> lines{1,3} ans = Cowboys 80000 60 4 >> lines{2,3} ??? Index exceeds matrix dimensions. >> whos lines Name Size Bytes Class Attributes lines 1x3 284 cell >> help regex regex.m not found. Use the Help browser Search tab to search the documentation, or type "help help" for help command options, such as help for methods. >> help regexp REGEXP Match regular expression. S = REGEXP(STRING,EXPRESSION) matches the regular expression, EXPRESSION, in the string, STRING. The indices of the beginning of the matches are returned. In EXPRESSION, patterns are specified using combinations of metacharacters and literal characters. There are a few classes of metacharacters, partially listed below. More extensive explanation can be found in the Regular Expressions section of the MATLAB documentation. The following metacharacters match exactly one character from its respective set of characters: Metacharacter Meaning --------------- -------------------------------- . Any character [] Any character contained within the brackets [^] Any character not contained within the brackets \w A word character [a-z_A-Z0-9] \W Not a word character [^a-z_A-Z0-9] \d A digit [0-9] \D Not a digit [^0-9] \s Whitespace [ \t\r\n\f\v] \S Not whitespace [^ \t\r\n\f\v] The following metacharacters are used to logically group subexpressions or to specify context for a position in the match. These metacharacters do not match any characters in the string: Metacharacter Meaning --------------- -------------------------------- () Group subexpression | Match subexpression before or after the | ^ Match expression at the start of string $ Match expression at the end of string \< Match expression at the start of a word \> Match expression at the end of a word The following metacharacters specify the number of times the previous metacharacter or grouped subexpression may be matched: Metacharacter Meaning --------------- -------------------------------- * Match zero or more occurrences + Match one or more occurrences ? Match zero or one occurrence {n,m} Match between n and m occurrences Characters that are not special metacharacters are all treated literally in a match. To match a character that is a special metacharacter, escape that character with a '\'. For example '.' matches any character, so to match a '.' specifically, use '\.' in your pattern. Example: str = 'bat cat can car coat court cut ct caoueouat'; pat = 'c[aeiou]+t'; regexp(str, pat) returns [5 17 28 35] When one of STRING or EXPRESSION is a cell array of strings, REGEXP matches the string input with each element of the cell array input. Example: str = {'Madrid, Spain' 'Romeo and Juliet' 'MATLAB is great'}; pat = '\s'; regexp(str, pat) returns {[8]; [6 10]; [7 10]} When both STRING and EXPRESSION are cell arrays of strings, REGEXP matches the elements of STRING and EXPRESSION sequentially. The number of elements in STRING and EXPRESSION must be identical. Example: str = {'Madrid, Spain' 'Romeo and Juliet' 'MATLAB is great'}; pat = {'\s', '\w+', '[A-Z]'}; regexp(str, pat) returns {[8]; [1 7 11]; [1 2 3 4 5 6]} REGEXP supports up to six outputs. These outputs may be requested individually or in combinations by using additional input keywords. The order of the input keywords corresponds to the order of the results. The input keywords and their corresponding results in the default order are: Keyword Result --------------- -------------------------------- 'start' Row vector of starting indices of each match 'end' Row vector of ending indices of each match 'tokenExtents' Cell array of extents of tokens in each match 'match' Cell array of the text of each match 'tokens' Cell array of the text of each token in each match 'names' Structure array of each named token in each match Example: str = 'regexp helps you relax'; pat = '\w*x\w*'; m = regexp(str, pat, 'match') returns m = {'regexp', 'relax'} Tokens are created by parenthesized subexpressions within EXPRESSION. Example: str = 'six sides of a hexagon'; pat = 's(\w*)s'; t = regexp(str, pat, 'tokens') returns t = {{'ide'}} Named tokens are denoted by the pattern (?...). The 'names' result structure will have fields corresponding to the named tokens in EXPRESSION. Example: str = 'John Davis; Rogers, James'; pat = '(?\w+)\s+(?\w+)|(?\w+),\s+(?\w+)'; n = regexp(str, pat, 'names') returns n(1).first = 'John' n(1).last = 'Davis' n(2).first = 'James' n(2).last = 'Rogers' By default, REGEXP returns all matches. To find just the first match, use REGEXP(STRING,EXPRESSION,'once'). REGEXP supports international character sets. See also REGEXPI, REGEXPREP, STRCMP, STRFIND, FINDSTR, STRMATCH. >> lines{1} ans = Eagles 70000 50 1 >> clc >> regexp(lines{1}, ' ', 'split') ans = 'Eagles' '70000' '50' '1' >> lines{1} ans = Eagles 70000 50 1 >> mystr= lines{1} mystr = Eagles 70000 50 1 >> whos mystr Name Size Bytes Class Attributes mystr 1x17 34 char >> myout = regexp(lines{1}, ' ', 'split') myout = 'Eagles' '70000' '50' '1' >> whos myout Name Size Bytes Class Attributes myout 1x4 268 cell