Can Matlab search through the current directory to find and import a file?

9 views (last 30 days)
I have some scripts that do data analysis and I am hoping to write some preliminary code to go through the current folder and find a .csv file and then import it and then I would like to go through another folder and search for a '...SizeChem.dat' file without knowing the full name of the file only knowing that it is the only file that ends with SizeChem.dat. I am not sure if this is possible or not but would greatly appreciate the help.

Accepted Answer

Cam Salzberger
Cam Salzberger on 13 Jun 2019
Hey Noah,
You can use the dir function to get information on the contents of a folder. You can use wildcards (* or **) to match multiple possible files. For example, dir('*.csv') will match all CSV files in the current directory. And you could do dir('path/to/other/dir/*SizeChem.dat') to match your other file (if you know the folder it should be in). If you need to search through multiple folders, you can simply loop through them, checking if any matches were found (with dir or even exist).
-Cam

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!