フィルターのクリア

Search a text in dat file

1 回表示 (過去 30 日間)
Seyhan Emre Gorucu
Seyhan Emre Gorucu 2013 年 11 月 5 日
コメント済み: Cedric 2013 年 11 月 5 日
Hello,
I have many dat files. I do not want to put my matlab code at the same place as the directory that contains the .dat files. Therefore, I change the directory as follows:
cd('D:Documents\Related Directory')
Now, I am at the directory that I want. I can open the files one by one. I know how to automize this.
fid=fopen('data1.dat');
Now that I have opened my dat file, I want to search for a particular text. This can be possible by reading it line by line until the end and searching for that text. The dat file has both number and alphabet. If it doesn't find that text, it goes to the next dat file and continues to search. If it ever finds that keyword, it notifies.
Can you please help me with that?
Thank you,
Emre
  1 件のコメント
Cedric
Cedric 2013 年 11 月 5 日
Could you give an example of what you are looking for?
If there are multiple occurrences, do you need the count or just the information that at least one occurrence was found?
Is it really the presence of a keyword that you need to know, or do you have content to extract before or after the keyword as well?

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

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 5 日
編集済み: Azzi Abdelmalek 2013 年 11 月 5 日
fid = fopen('filename.dat');
res={};
while ~feof(fid) &
res{end+1} =fgetl(fid);
end
fclose(fid);
s=regexp(res,yourtext);
r=find(cellfun('isempty',s))

その他の回答 (1 件)

Simon
Simon 2013 年 11 月 5 日
Hi!
% read in file
fid = fopen('filename.dat');
FC = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);
FC = FC{1};
% search for pattern, define as you like
pattern = 'searchme';
ind = ~cellfun('isempty', regexp(FC, pattern));

カテゴリ

Help Center および File ExchangeAudio and Video Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by