Check whether this approach is fast enough. SSCANF used to be faster than DATEVEC to convert date strings with identical format for all lines and only numbers. Might not be the case anymore.
I copied your data to cssm.dat.
fid = fopen( '.\cssm.dat', 'r' );
cac = textscan( fid, '%s%s%f%f%f%f%f%f', 'Delimiter', ',' );
sts = fclose( fid );
vec = zeros( numel( cac{1,1} ), 6 );
[ vec(:,1), vec(:,2), vec(:,3), ~, ~, ~ ] = datevec( cac{1,1}, 'mm/dd/yyyy' );
[ ~, ~, ~, vec(:,4), vec(:,5), ~ ] = datevec( cac{1,2}, 'HHMM' );
dat = [cac{1,3:end}];
m1 = 4;
m2 = 7;
d06 = dat( vec(:,4)==6 & vec(:,5)>=m1 & vec(:,5)<=m2, : );
v06 = vec( vec(:,4)==6 & vec(:,5)>=m1 & vec(:,5)<=m2, : );
Use the same approach for your second case.
Rounding errors should not be a problem in this example since DATEVEC returns flint (floating integers)
FSCANF (with 64bit R2011a) seems to be twice(?) as fast
fid = fopen( '.\cssm.dat', 'r' );
num = fscanf( fid, '%2u/%2u/%4u,%2u%2u,%f,%f,%f,%f,%f,%f', [11,inf] );
sts = fclose( fid );
num = transpose( num );
vec(:,1:5) = num(:,[3,2,1,4,5]);
vec(:,6) = 0;
dat = num(:,6:end);
However, this requires a bit more care and a file that adheres to the format.
The code above assumes that all data fits in memory.
Surprisingly, the code below is another 20% faster (with 64bit R2011a). I see three advantages with this code. Easy to handle header lines. Easier do debug when the text file is "corrupted". One additional line of code will handle comma as decimal separator. And it's a bit faster.
fid = fopen( '.\cssm.dat', 'r' );
cac = textscan( fid, '%s', 'Whitespace','', 'Delimiter','\n' );
sts = fclose( fid );
str = transpose( char( cac{:} ) );
str = cat( 1, str, repmat( ',', 1, size(str,2) ) );
num = sscanf( str, '%2u/%2u/%4u,%2u%2u,%f,%f,%f,%f,%f,%f,', [11,inf] );
vec(:,1:5) = num(:,[3,2,1,4,5]);
vec(:,6) = 0;
dat = num(:,6:end);
0 Comments
Sign in to comment.