MATLAB: Extract value from .txt. Weird lay out.

doubleextractMATLABstringtxtvalue

Hi, so here is my problem: i'd like to extract values (double) from a file where sometimes different columns are not separated by any kind of delimiter.
The text file can look like this :
if true
% code

1994010103
12.05 54.60 38.00 0.28
12.10 54.60 43.00 0.30
13.10 54.60 99.00 0.33
13.15 54.60100.00 0.34
13.20 54.60 0.00 0.00
13.25 54.60128.00 0.16
end
and i'm interested in the values in the third column. The first row is a date/time and i should get rid of it.
My solution to this problem is :
if true
% code
fid = fopen('file');
T = textscan(fid,'%s','delimiter',{'\n'});
fclose(fid);
ngx=39;
ngy=34;
n=ngx*ngy;
t=5839;
for i =1:t
T{1}((i-1)*n+1)=[]; %get rid of the date/time which occurs every nth row
end
interest = zeros(length(T{1}),1);
for i =1:length(T{1})
interest(i) = str2double(T{1}{i}(12:18)); %extract the interesting characters from every row and convert them into a double
end
end
This code works, but i'm dealing with millions of rows and the loop makes the computation time really long..
If you have any idea of how to reduce the computation time, that'd be great !
Thanks

Best Answer

This has been a point of contention of mine "since forever" -- there's no automatic way to read fixed-width column data in C (and hence Matlab). TMW definitely needs to add a feature so I recommend to add your voice to the feature enhancement list for it. Maybe in 30 more years...
But, with tools as are--
c=textscan(fid,'%s','delimiter','\n'); c=char(c{:}); % read, to char array
c(1:nSkip:end,:)=[]; % delete the date rows
data=str2num(c(:,12:18)); % convert the desired columns
If need more columns, either duplicate above or use the known field widths and insert a delimiter in the desired locations then use textscan on the array.
ADDENDUM:
The best thing for Matlab if you can is to change the form in which the files are generated going forward to use a delimiter or at least increase the field width. But, of course, sometimes it isn't feasible to do so..