Getting Wavread to import an audio file via a variable name
이전 댓글 표시
Hi
I'm trying to get MATLAB to load a random audio file from a folder (i.e. to load a random 1 from the x audio files in the folder). I've created some code that creates a list of all the file names and randomly selects one, such that the filename of the .wav file is in a string variable (FileToPlay). However when I pass that string variable to my wavread function I get the following errors.
??? Error using ==> fileparts at 17 Input must be a row vector of characters.
Error in ==> wavread>open_wav at 193 [pat,nam,ext] = fileparts(file);
Error in ==> wavread at 65 [fid,msg] = open_wav(file);
Error in ==> get_random_file at 42 [File_Data,FS,NBITS,OPTS]=wavread(FileToPlay);
This is the relevant code. The CurrFold variable is just the name of the directory where the files are.
%Put metadata for folder in variable (not really sure what the index bit
%does)
FoldData = dir(CurrFold);
FoldIndex = [FoldData.isdir];
%Gets list of files in folder
FileList = {FoldData(~FoldIndex).name};
%Transpose resultant row vector into column vector
FileList = FileList';
%Get number of files
[FileCount] = size(FileList,1);
%create random number with upper limit of nbr of files (i.e. 1 - max number
%of files
n = ceil(FileCount*rand(1,1));
%Select file based on random number generated
FileToPlay = FileList(n);
%Remove any extra space
FileToPlay = strtrim(FileToPlay);
%Import file
[File_Data,FS,NBITS,OPTS]=wavread(FileToPlay);
If I replace FileToPlay in the wavread statement with the name of a wav file in quotations (i.e. 'noise.wav') then it works fine, so it's not that it can't find the location of the wav file, or some basic coding error. It must be that FileToPlay isn't holding the name of the file properly. Removing the strtrim function doesn't help either.
Thanks Rob
채택된 답변
추가 답변 (1개)
Rob H
2011년 5월 18일
0 개 추천
댓글 수: 1
Jan
2011년 5월 18일
CellString = {'1st', '2nd', '3rd', 'different lengths'};
CellString(1) is {'1st'} then: a list of strings with length 1. CellString(1:2) is {'1st', '2nd'). Cellstring{1} is the string '1st', which can be used for printing etc. See "docsearch('cell string')".
카테고리
도움말 센터 및 File Exchange에서 Audio and Video Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!