Hi,
I have a .txt file consisting of a header section and data section (attached). I am interested in only reading the header portion of the file, but what is the best way to do this? I would like the final output to be stored as a char as shown in the picture below, and have tried fscanf to do so but can't quite figure it out.
I am quite a novice with Matlab so any help is greatly appreciated!

 採用された回答

Image Analyst
Image Analyst 2017 年 1 月 17 日

0 投票

Try this:
str = fileread('test.txt')
index = strfind(str, '[Data]');
header = str(1:(index-1));

5 件のコメント

Walter Roberson
Walter Roberson 2017 年 1 月 17 日
You might perhaps want to go one step further and use
header(ismember(header, [10 13])) = '';
if you really want to remove all carriage returns and linefeeds as implied in your image. Your data would probably be easier to parse if you did not remove them.
Dhani Dharmaprani
Dhani Dharmaprani 2017 年 1 月 17 日
Thank you so much, this works perfectly!
If you don't mind me asking, does this just use strfind to find the index of '[Data]' in the file, and then reads from the beginning of the file until the value before '[Data]'?
Thank you so kindly for your help!
Walter Roberson
Walter Roberson 2017 年 1 月 17 日
No, the fileread() reads the entire content of the file. Then the location of [Data] is found in the resulting string, and only the part of the string up to there is kept.
leydy Garcia
leydy Garcia 2020 年 2 月 6 日
what is [Data] in this example?
Walter Roberson
Walter Roberson 2020 年 2 月 6 日
[Data] is line 295 of the user's file test.txt . It is the header that marks the end of the section the user wants to extract; the user wants the header section of the data rather than the numeric data.

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

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by