Read file

19 ビュー (過去 30 日間)
Andrea Ferrari
Andrea Ferrari 2011 年 2 月 15 日
Hi all,
probably a very simple question..I need to read a text file consisting of a list of vectors with this format:
skip the first 22 line and then (x y z) (x y z) (x y z) ... (x y z)
there are 263853 line. How can i do that??
thanks for any help
andrea

回答 (3 件)

Jan
Jan 2011 年 2 月 15 日
The description of the file format is not exact enough. I'll show you a method for a well defined format and I assume you can adjust it to your needs:
I assume the file looks like this:
Header line 1
...
Header line 22
(0.1 0.2 0.3)
(0.17 19.5 231)
... <263853 of such lines>
This can be solved by TEXTSCAN or more low-levelish:
FID = fopen(FileName, 'r');
if FID <= 0, error('Cannot open file'); end
for iSkip = 1:22
dummy = fgetl(FID);
end
Data = fscanf(FID, '(%g %g %g)\n');
Data = transpose(reshape(Data, 3, []));
fclose(FID);
  2 件のコメント
Andrea Ferrari
Andrea Ferrari 2011 年 2 月 15 日
thanks Jan. It runs
andrea
Jan
Jan 2011 年 2 月 15 日
Thanking is very polite. Accpeting an answer is helpful for others in addition.

サインインしてコメントする。


Andrea Ferrari
Andrea Ferrari 2011 年 2 月 15 日
sorry
all vector are in column (not along a line).
andrea

Matt Tearle
Matt Tearle 2011 年 2 月 15 日
Or, using textscan (as Jan mentioned):
FID = fopen(FileName, 'r');
if FID <= 0, error('Cannot open file'); end
Data = textscan(FID, '(%f %f %f)','headerlines',22);
x = Data{1};
y = Data{2};
z = Data{3};
fclose(FID);

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by