loop through all files in folder and list those which end with .m in matlab

To loop through all files in a folder, we can use the dir function to get a list of all files in the folder, and then iterate over each file using a for loop. We can then use a conditional statement to check if the file ends with ".m" using the endsWith function. Finally, we can print out the names of the files that meet the condition.

Here is the code:

main.m
folder = 'path/to/folder';
files = dir(fullfile(folder, '*.m'));

for i = 1:numel(files)
    file = files(i);
    if endsWith(file.name, '.m')
        disp(file.name);
    end
end
180 chars
10 lines

Replace 'path/to/folder' with the actual path to the folder containing the files you want to check. The dir function returns a structure array containing information about each file in the folder, including the name, date, size, and whether it is a file or folder. We use the fullfile function to combine the folder path with the search pattern *.m, which matches all files with a ".m" extension.

The numel function is used to get the number of files in the array. We then use a for loop to iterate over each file. Inside the loop, we get the name field of the file structure using dot notation (file.name), and check if it ends with ".m" using the endsWith function. If it does, we print out the name using the disp function.

gistlibby LogSnag