How to search a keyword in text files and if it is found,write the string next to it in an excel file under a column ?

3 ビュー (過去 30 日間)
Hello all,
I want to search a keyword "import from" from various .art files present in a folder.if found,i want to write a string which is written just next to it in an excel file under a column name ABC. For ex- Import from pqrst.art Here after searching becomes succesful for import from keyword,i want to write pqrst.art in an excel file under column ABC. How can i do that?

採用された回答

Mathieu NOE
Mathieu NOE 2021 年 3 月 16 日
hello
this is a first code to show the basic functionnalities (getting a 1 or 0 result depending of presence of "import from" from various .art files
then I export the results , but your query is a bot unclear to me - what do you want to export : Import from pqrst.art or pqrst.art - and why it should be on 3 rows ABC ?
code below , dummy art files attached + the resulting excel file
clc
clearvars
% range = 'C2:D10'; % Write to specific range of data:
file_list = dir('*.art'); % list all xlsx files in current directory
for i = 1:length(file_list)
filename = file_list(i).name
result = extract_data(filename)
if result>0
out_string{i} = ['Import from ' filename];
end
end
% export to excel
writecell(out_string','out.xlsx');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function result = extract_data(Filename)
fid = fopen(Filename);
tline = fgetl(fid);
result = 0;
while ischar(tline)
% retrieve line Date/Time
if contains(lower(tline),'import from') % lower make matlab not case sensitive
result = 1;
end
tline = fgetl(fid);
end
fclose(fid);
end
  3 件のコメント
Rohit P
Rohit P 2021 年 3 月 16 日
So basically, once we find out the file has import from keyword written inside it..we have to write the part just after it in the excel file under a column.( Here pqrst.art). So please help me on how to do that
Mathieu NOE
Mathieu NOE 2021 年 3 月 17 日
sorry
it's still unclear to me
can you provide some art files and what the result should be in the excel file ?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by