how to search for a string inside a file in matlab

15 ビュー (過去 30 日間)
lafnath p
lafnath p 2016 年 10 月 22 日
コメント済み: Jan 2016 年 10 月 26 日
to search a word inside a .dat file to read data under that line
  1 件のコメント
Jan
Jan 2016 年 10 月 22 日
It depends on the format of the .dat file. There is no standard for this file type, so we cannot guess the details.

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

採用された回答

Jan
Jan 2016 年 10 月 22 日
編集済み: Jan 2016 年 10 月 26 日
Guessed, tha the .dat file is a text file and you want to search fpr the occurence of a string anywhere in a line:
fid = fopen(FileName, 'r');
if fid == -1
error('Cannot open file: %s', FileName);
end
key = 'YourStr';
data = 'not found';
ready = false;
lineNo = 0; % [EDITED]
while ~eof(fid) && ~ready
S = fgetl(fid);
lineNo = lineNo + 1; % [EDITED]
if any(strfind(S, key))
data = YourReadMethod;
ready = true;
end
end
fclose(fid);
  3 件のコメント
Image Analyst
Image Analyst 2016 年 10 月 22 日
Put a line counter variable inside the loop that you increment.
Jan
Jan 2016 年 10 月 26 日
@lafnath p: See [EDITED] in the code. You see, it is not complicated.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by