iterate through all files in folder, open them and check if contains the string "abc" inside, then list all files that do in matlab

To iterate through all files in a folder, you can use the dir function to get a list of all files in the folder, and then loop over the list to open each file, check if it contains the string "abc", and if so, add it to a list of matching files. Here's an example code block that does this:

main.m
folder = '/path/to/folder'; % replace with your folder path
files = dir(fullfile(folder, '*.txt')); % replace '*.txt' with your file extension
matching_files = {};

for i = 1:length(files)
    file_path = fullfile(folder, files(i).name);
    file_contents = fileread(file_path);
    
    if contains(file_contents, 'abc')
        matching_files{end+1} = file_path;
    end
end

disp(matching_files);
400 chars
15 lines

This code defines the folder path and uses dir to get a list of all .txt files in the folder. It then loops over each file, reads its contents with fileread, and checks if it contains the string "abc" using the contains function. If it does, it adds the file path to the matching_files cell array. Finally, it displays the list of matching files using disp. You can replace *.txt with your own file extension to match the files you want to search through.

gistlibby LogSnag