MATLAB: Reading only a portion of a text file

doit4mereading portion of text files

I would like to read in a text file that contains a header and footer of information, where the number of rows of the header/footer, and number of rows to read can vary.
Here is an example of a row of data I would like to read from the text file.
All rows start with the ARR++ and are delineated with ':' I now I need i most likely need to use fopen / fprintf / fget1 / textscan but hoping someone can help set this up.
One other thing with the rows of data I would like to read in, there is date information like: 2013320133. I would ideally like to read only the first 5 digits of that date and separate the year and quarter into separate columns — 20133 –> 2013 3
Here is a better example of the type of file I would like to read in. I am only interested in the ARR++ lines. I would be interested to have only the first 5 digits of 2013420134. Thanks a lot.
UNA:+.? ' UNB+UNC:140305:1444++' UNH+:2:1:E6' BGM+74' NA+Z02+' NAD+M+50' ND+MS+C2' STS+3+7' DM+242:20144:203' GI+AR3' GS+1:::-' ARR++Q:S:C:A:1N:2013420134:708:1234.323:A:N' ARR++Q:S:C:A:1N:2013420134:708:12234.323:A:N' ARR++Q:S:C:A:1N:2013420134:708:133234.323:A:N' ARR++Q:S:C:A:1N:2013420134:708:132234.323:A:N' ARR++Q:S:C:A:1N:2013420134:708:123334.323:A:N' ARR++Q:S:C:A:1N:2013420134:708:1232134.323:A:N' ARR++Q:S:C:A:1N:2013420134:708:123324.323:A:N' ARR++Q:S:C:A:1N:2013420134:708:123234.323:A:N' UNT+16+' UNZ+1+I3800'

Best Answer

fid = fopen('mydata.txt', 'r'); tline = fgetl(fid); k=1;
while ischar(tline) disp(tline) findrows = strfind(tline, 'ARR++'); if ~isempty(findrows) Data{k,:} = tline(:); k=k+1; end tline = fgetl(fid); end
fclose(fid);
filename = 'mydata_1.txt';
%this function converts the cell array back to a text file. found here: cell array to text file file;
cell2text(filename,Data);
%open file fid = fopen(filename, 'r'); %figure out how many columns are there firstline = fgetl(fid); ncol = 1 + sum(firstline == ':'); %reset to beginning of file fseek(fid,0,0); %read data data = textscan(fid,repmat('%s',1,ncol),'Delimiter',':','CollectOutput',1); data = data{:,:}; data = data(:,[3:7 9:12 14 16]);
[m,n]=size(data);
for i = 1:m data1{i,9} = data{i,9}(1:5); data{i,9} = {}; data{i,9} = data1{i,9}; end
%close file fclose(fid);