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!