MATLAB: Efficiently Read in Text file with Headers Throughout File

fscanftextread

Hello,
I am trying to read dispersion outputs from the GULP lattice dynamics program. The format of the output file is shown in the attached file. Basically, it has 3 header lines and then two columns of data then three header lines and two columns of data repeating roughly 300 times (depends of the simulation parameters). I want to efficiently read this output without testing every line. My current code shown below works fine but it takes 30 seconds for a small output file and I want to optimize how this data is read in preparation for much larger files. Any suggestions?
fid = fopen([file]);
idx = 0;
idx2 = 1;
z = 0;
tic
while ~feof(fid) %Check every line in the file
dummy = fgetl(fid);
test = dummy(1);
if ~strcmp(test,'#')
dummy2 = strsplit(dummy,' ');
C(idx2,1) = str2double(cell2mat(dummy2(1,2)));
C(idx2,2) = str2double(cell2mat(dummy2(1,3)));
idx2 = idx2 + 1;
else
Header(idx+1,1) = {dummy};
idx = idx + 1;
end
end
toc

Best Answer

This solution works great if you know the length of data between headers. I had to write a code to determine this from the GULP input file.
tic
N = nBranch*n1Max; %length of data between headers
formatSpec = '%f %f';
C = [];
while ~feof(fid)
s = textscan(fid,formatSpec,N,'CommentStyle','#','Delimiter','\t');
C = [C; s{1,1}, s{1,2}];
end
toc