file2strmat.m (3166B)
1 function strmat = file2strmat(file,flag,N) 2 3 % STRMAT = FILE2STRMAT(FILE,FLAG,N) 4 % 5 % FILE2STRMAT Converts a text file into a string matrix 6 % (each line of a file corresponds to a row in a matrix) 7 % FILE2STRMAT(FILE) reads each line of the file FILE 8 % into a string matrix. 9 % Comments (all characters after '%' in each line) 10 % are normally excluded, although the line numbering 11 % is always preserved. 12 % To include them use FLAG: 13 % FILE2STRMAT(FILE,FLAG), where legal FLAG strings are 14 % 'all', 'include', 'comments'. 15 % Other strings: 16 % 'exclude', 'nocomments' 17 % again exclude them (equavalent to no flag at all). 18 % FILE2STRMAT(FILE,FLAG,N) reads N elements from FILE. 19 20 % Kirill Pankratov, Feb. 5 1994 21 22 % Handle input ................................................. 23 iscmex = 1; % Default specifying that comments are excluded 24 numb = inf; % Default for number of bytes to read (to the end of file) 25 26 if nargin == 0 27 disp([10 ' You must specify the file name as an argument' 10]) 28 return 29 end 30 if nargin == 1, iscmex=1; end 31 if nargin == 2 32 if isstr(flag) 33 if strcmp(flag,'all')|strcmp(flag,'include')|strcmp(flag,'comments') 34 iscmex = 0; 35 elseif strcmp(flag,'exclude')|strcmp(flag,'nocomments') 36 iscmex = 1; 37 else 38 disp([10 ' Sorry, we haven''t understood the second argument' 10]) 39 disp([setstr(flag(1:size(flag,2))) ' ?']) 40 end 41 else, numb = flag(1); 42 end 43 end 44 if nargin == 3, if ~isstr(N), numb = N; end, end 45 46 % Set relevant characters and numbers 47 chnl = 10; % # of character for a new line 48 chcm = abs('%'); % Character for comments 49 chfl = abs(' '); % Character to fill the blanks 50 51 [fid,msg] = fopen(file,'r'); % Open file to read 52 if fid==-1, disp(msg), return, end 53 f = fread(fid,numb,'char'); % Read file 54 55 if iscmex % If comments excluded ```````````````````````````` 0 56 fnd = find(f==chnl); % ## of new line characters 57 lfnd = length(fnd); 58 fnd1 = find(f==chcm); % Beginnings of comments 59 numd = zeros(size(f)); 60 numd(fnd1) = ones(size(fnd1)); 61 numd = cumsum(numd); 62 numnl = numd(fnd); 63 numnl(2:lfnd) = numnl(2:lfnd)-numnl(1:lfnd-1); 64 numd = zeros(size(f)); 65 numd(fnd1) = ones(size(fnd1)); 66 numd(fnd) = -numnl; 67 numd = cumsum(numd); 68 f = f(numd==0); % String without comments 69 end % End comments excluded ''''''''''''''''''''''''''''''''' 0 70 71 % Now the main procedure ....................................... 72 fnd = find(f==chnl); % ## of new line characters 73 nlines = length(fnd); 74 llines = [fnd(1); fnd(2:nlines)-fnd(1:nlines-1)]; 75 l1line = max(llines); 76 numd = ones(size(f)); 77 numd(fnd) = 1+l1line-llines; % Add numb. to make all lines equal 78 numd = cumsum(numd); % ## in STRMAT for each character in F 79 80 strmat = ones(l1line,nlines+1)*chfl; % Create output matrix STRMAT 81 strmat(numd) = f; % Put all characters in proper places 82 fnd = find(strmat==chnl); 83 strmat(fnd) = ones(size(fnd))*chfl; % Replace newlines with blanks 84 strmat = setstr(strmat'); % Make string out of the whole thing 85 86 fclose(fid); % Close file