Search code examples
matlabpathfilenamessubdirectory

How to put all the file names into an array in MATLAB


I plan to list all the file names of a current folder (include subfolder) and put them and their path into an array. I can use s=dir to put the names and path of all the files in the current folder, I can also use "dir **/." to show the files in the current folder and subfolders.

But when I use "s=dir **/.", Matlab gives me error and I am not able to proceed. Is there anyone can help me on this?

The reason why I want to do this is to compare two folders which may contain plenty of duplicate files. I want to use file name as the indicator and to find out the new adding or removed files, so that I can update the log excel we have.

Thank you for your help.


Solution

  • To list only the files and not the directories try

    file_names = dir('**/');
    file_names = file_names(~[file_names.isdir]);
    file_names = {file_names.name}