Your file is not easy to import. I’ve been working on this for a while.
Try this:
fidi = fopen('M1output1.txt','rt');
k1 = 1;
while ~feof(fidi)
C = textscan(fidi, '%*f%f%*f', 'HeaderLines',2, 'CollectOutput',true, 'CommentStyle',{' total', ' energy'});
M = cell2mat(C);
if isempty(M)
break
end
D{k1,:} = M;
fseek(fidi, 0, 0);
k1 = k1 + 1
end
fclose(fidi);
Column2 = cell2mat(D);
The textscan format descriptor reads only Column 2, ignoring the other two columns.
The loop is necessary because you have several text lines that interrupt the ordinary file reading process, so every time textscan encounters text where it expects a numeric value, it stops, snd it is necessary to use fseek to re-start it and read the next block of numbers. Your file also does not have a valid ‘end-of-file’ indicator, so to keep the loop from becoming infinite, it is necessary to test to see if the input is empty. If it is, then the loop breaks and file reading stops. Since this does not occur until all the data have been read, no data are lost.
The cell2mat call at the end concatenates the cells in ‘D’ into a single vector.
Best Answer