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 CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by