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 Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by