简体   繁体   中英

Check wav file exists in Matlab folder

I need to check whether a wav file in Matlab work folder exists ou not. If it does, I need to load the file into a variable (file in my case), i use this code but it doesn't work.

      if strcmp(file,'\n')==0
          file='test.wav';           
      elseif findstr(file,'.')==''
          file=strcat(file,'.wav');
      end
      [TestWave,Fs] = audioread(file);

You don't say if you are trying to find a particular .WAV file, or just any .WAV file...

If you just want to know if a particular file (of any kind) exists, use the exist() function. It returns value 2 if a file exits:

myFileName = 'test.wav';
myDirectory = 'c:\temp';
filepath = fullfile(myFileName,myDirectory);
if exist(filepath,'file') == 2
    [TestWave,Fs] = audioread(file);
end

Otherwise, just search for the files you need using dir() :

myDirectory = 'c:\temp';
wildcard = '*.wav';

theseFiles = dir(fullfile(myDirectory,wildcard));
for i = 1:length(theseFiles)
    thisFilePath = fullfile(myDirectory,theseFiles(i).name);
    [TestWave,Fs] = audioread(thisFilePath);    % Load this file

    % Do something with the loaded file...
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM