How do I get Matlab to return a matrix with all the file names in a folder?

2 views (last 30 days)
I need to analyze a folder of .txt files. I want to create a for loop that analyzes one file in the folder at a time and organizes their data into a matrix of all the files data. I want the user to select the folder they would like to analyze. I tried this code.
F = dir(uigetdir('*.dat'))
But when I checked the names that it was returning, it was returning '.' for all of the files names. How do I make it return their real names, and why is it not reading the correct file names?

Accepted Answer

Sean de Wolski
Sean de Wolski on 2 Jun 2015
This should work:
F = dir(uigetdir('*.m'))
names = {F(:).name}
names will be a cell array with all file names with the .m extension in the folder.
NB: Changed to .m since I don't have any .dat files. If they have a .txt extension, you should be using that.

More Answers (0)

Categories

Find more on File Operations 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!