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 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Mathieu NOE
on 16 Mar 2021
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 Comments
Mathieu NOE
on 17 Mar 2021
sorry
it's still unclear to me
can you provide some art files and what the result should be in the excel file ?
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!