textscan difficulties

조회 수: 6 (최근 30일)
Jason
Jason 2011년 10월 28일
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
  댓글 수: 2
Jason
Jason 2011년 10월 28일
Just to add, I have say 150 of these tables, each on eunder its heading and descending, so at the end of the text file is Data1.
I eventually want to average all data tables say from table 1 - 50.
Jan
Jan 2011년 10월 28일
what have you tried so far to solve your problem?
I assume TEXTSCAN is not sufficient. I'd use FOPEN/FGETL/FSCANF and a WHILE loop instead.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2011년 10월 31일
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
  댓글 수: 2
Jason
Jason 2011년 10월 31일
Hi, many thamks. Mt data index actually counts backwards so it will start say feom "# Data150" and at the end of the page have the last table with "# Data1#". I also notice when I use textscan to read in, when it reads in the numbers from the table, it just puts them all together in a single line with no spacing or tabs between them. Is it possible to tell textscan to do this -
i couldn't find it in the help.
Thankyou very much.
Jason
Jan
Jan 2011년 10월 31일
@Jason: Then you can either reorder Data afterwards: "Data = Data(:, :, end:-1:1)". Or you can consider this during the creation: "Data(j, :, Len+1-i) = ...".
Please show the inputs of TEXTSCAN. It matters if you are readibng a line using the '%s' or '%g' formats. Did you set the WhiteSpace property correctly?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jason
Jason 2011년 10월 31일
Yes, I have finally got your solution to work - brilliant.
Now I have a set of matrixes called "Data". Is it possible to perform a vector sum, i.e. create another matrix where each element has been summed.
Thanks
  댓글 수: 1
Jan
Jan 2011년 10월 31일
Yes. See SUM(Data, Dim).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by