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?
댓글 수: 0
채택된 답변
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
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 Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!