MATLAB: Textscan difficulties

textscan

Hello. How can i use textscan to read in a certain table that is under a user defined heading.
# Data150
A B C D E
A 1 21 7 6 4
B 26 2545 8 7 1
C 10 12 2453 12 2
D 19 24 27 3046 6
E 0 0 0 0 0
My text file has a few headerlines at the top but is then listed in the above format sequentially, so theres another table underneath that has a heading # Data149. so how could I search for the heading e.g. ~ Data150 and just pick up the numbers in the table (they are seperated by tabs.
thanks

Best Answer

Assuming that the data index in "# Data%d" starts at 1:
function Data = readTheData(FileName)
FID = fopen(FileName, 'r');
if FID == -1, error('Cannot open file %s.', FileName);
CC = textscan(FID, '%s', 'Delimiter', '\n');
% perhaps this in addition: 'WhiteSpace', ''
fclose(FID);
C = CC{1};
index = find(strncmp(C, '#', 1));
Len = numel(index);
Data = zeros(5, 5, Len);
for i = 1:Len
iLine = index(i) + 1; % Skip the header line
for j = 1:5 % Read 5 lines
aData = sscanf(C{iLine + 1}, ' *c %d %d %d %d %d');
Data(j, :, i) = transpose(aData);
end
end