How to get line number in a text file with a specific word
61 ビュー (過去 30 日間)
古いコメントを表示
Jaffrey Hudson Immanuel Jeyakumar
2019 年 6 月 22 日
コメント済み: Jaffrey Hudson Immanuel Jeyakumar
2019 年 7 月 17 日
Hallo,
I have a fruit.txt file with data as follows,
apple
mango
Cherry
Watermelon
I want to write a script whcih will find the word 'apple' and return me it line number.
Can anyone help me ?
0 件のコメント
採用された回答
madhan ravi
2019 年 6 月 22 日
編集済み: madhan ravi
2019 年 6 月 22 日
No loops needed:
A = regexp(fileread('fruit.txt'),'\n','split');
whichline = find(contains(A,'apple'))
6 件のコメント
その他の回答 (1 件)
infinity
2019 年 6 月 22 日
Hello,
you could try this
fileID = fopen('fruit.txt','r');
A = textscan(fileID,'%s');
fclose(fileID);
n = size(A{:});
for i = 1:n
if strcmp(A{:}(i),'apple')
linenumber = i;
end
end
8 件のコメント
参考
カテゴリ
Help Center および File Exchange で Text Data Preparation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!