2016-04-01 98 views
1

我想從某個目錄讀取幾個文件。這裏是我的腳本:讀取多個文件(wavread)

prefix = '\wav\'; 
wav_arr = ['1.wav'; '2.wav'; '3.wav']; 
wav_arr = cellstr(wav_arr); 

len = length(wav_arr); 
sig = []; 
fs = []; 
for i = 1:len 
    path = strcat(pwd, prefix, wav_arr(i)); 
    [s, curr_fs] = audioread(path); 
    sig = [sig s]; 
    fs = [fs curr_fs]; 
end 

但我收到的錯誤:

Error using which 
Argument must contain a string. 

Error in audiovideo.internal.absolutePathForReading (line 7) 
whichFileName = which(filename); 

Error in audioread (line 74) 
filename = audiovideo.internal.absolutePathForReading(... 

Error in main_experiment (line 14) 
    [s, curr_fs] = audioread(path); 

的問題是,path1x1 cell,它不是一個string。對於path執行cellstr()不起作用。

+0

路徑是MATLAB函數避免使用這個名字作爲變量! – obchardon

回答

2
for i = 1:len 
    path = strcat(pwd, prefix, wav_arr(i)); 
    [s, curr_fs] = audioread(path); 
    sig = [sig s]; 
    fs = [fs curr_fs]; 
end 

成爲

for i = 1:len 
    path = strcat(pwd, prefix, wav_arr{i}); 
    [s, curr_fs] = audioread(path); 
    sig = [sig s]; 
    fs = [fs curr_fs]; 
end 

這就是全部,閱讀有關MATLAB中的細胞中的文檔,以瞭解細胞之間的差值(1)和細胞{1}

+1

而不是'strcat(pwd,prefix,wav_arr {i});'我會建議使用'fullfile(pwd,prefix,wav_arr {i});'而不是。意圖是更清晰恕我直言。此外,它允許使用'prefix ='wav',而不用擔心正確的斜槓。像你自己所建議的那樣,將'path'重命名爲'filename' –