can not find a file list with fonction ( dir)

15 views (last 30 days)
hello
I used a function (dir) to get a list of wav files, but it returns an empty list
Folder = 'the folder which contain files';
List= dir([folder '*.wav']);
Why the list is empty?
thank you
  3 Comments
ChihabEddine CHIHEB
ChihabEddine CHIHEB on 28 Mar 2023
thank you , it was it
list = dir([folder ,'/*.wav']);
Stephen23
Stephen23 on 28 Mar 2023
Best avoided. Using FULLFILE is more robust (e.g. it will take into acount if the path already has a trailing path separator character, which your code does not do). The best approach by far is to use FULLFILE. You should use FULLFILE.

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 27 Mar 2023
Variables in MATLAB are case sensitive. So Folder is not the same as folder. You define Folder, but then use folder in your dir command.
If that is just a typo here, then the next most likely reason is that you have forgotten to include backslash (or forward slash, depending on your system) at the end of your folder path. A good way to avoid this is by using fullfile to build your path. Here's a simple example using a relative folder path.
Folder = '.\MATLAB Answers'
dir(fullfile(Folder, '*.wav'))
  8 Comments
Cris LaPierre
Cris LaPierre on 28 Mar 2023
Edited: Cris LaPierre on 28 Mar 2023
Use the Copy option from my post rather than typing the code yourself.
You are erroneously including square brackets "[]", which is preventing fullfile from doing its thing.
dir(fullfile(folder,'*.wav'));
Stephen23
Stephen23 on 28 Mar 2023
Edited: Stephen23 on 29 Mar 2023
@ChihabEddine CHIHEB: compare:
dir(fullfile(Folder, '*.wav')) % the correct code, as Chris LaPierre showed you.
dir(fullfile([folder ,'*.wav'])); % your code, with square brackets.
% ^ ^ why do you keep adding these?

Sign in to comment.

More Answers (0)

Categories

Find more on File Name Construction in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!