Yelin
Yelin

Reputation: 1

MATLAB - faster writing to file

I'm executing the following code to filter groups of four lines taken from one file, by the value of all characters in one of the lines (designated line q), and write the filtered lines to a new file.

fin = fopen(fname_in,'r') ;  
fout = fopen(fname_out,'w') ;  
y = 0 ;  
w = 0 ;  
fprintf(1,'Filtering') ;  
while ~feof (fin)  
  w=w+1 ;  
  if ~mod(w,50000) 
    fprintf(1,'.'); 
  end  
  t = fgets(fin) ;  
  s = fgets(fin) ;  
  p = fgets(fin) ;   
  q = fgets(fin) ;  
  if all(q(1:len) >= phred+phred_code)  
    y = y + 1 ;  
    fwrite(fout,[t s([1:len end]) p q([1:len end])]) ;  
  end  
end  

fclose(fin) ;  
fclose(fout) ;  

The code is working, but it takes a very long time to run.

What other approach could accelerate the code?

thanks

Upvotes: 0

Views: 1570

Answers (1)

shoelzer
shoelzer

Reputation: 10718

Usually it's faster to do fewer, but larger, reads and writes. If the file is small enough, you could read the entire file, process the lines, then write the entire file. If the file is too big for that, you can loop to read, process, and write chunks of the file instead of individual lines.

Upvotes: 1

Related Questions