1

wav ファイルを読み取り、そこからいくつかの機能を抽出して保存し、次のファイルを選択して同じ手順を繰り返す必要があるプログラムを作成しようとしています。選択するウェーブ ファイルの数が 100 を超えています。wav ファイルを次々と読み込む方法を教えてください。(ファイルの名前は e1.wav、e2.wav などとします)。誰か私を助けてください

4

1 に答える 1

1

dirコマンドはここで非常に役立ちます。ディレクトリの内容全体を表示しますが、グロブを指定してファイルのサブセットのみを返すこともできますdir('*.wav')nameこれは、datebytes、などのファイル情報を含む構造体配列を返しますisdir

開始するには、次のことを試してください。

filelist = dir('*.wav');
for file = filelist
    fprintf('Processing %s\n', file.name);
    fid = fopen(file.name);
    % Do something here with your file.
    fclose(fid);
end

編集 1:二重引用符を単一引用符に変更します (thx to user1540393 )。

編集 2 ( amroによる提案): 処理結果をファイルごとに保存する必要がある場合、次のパターンをよく使用します。通常、ファイルリストと同じサイズの配列、構造体配列、またはセル配列を事前に割り当てます。次に、整数インデックスを使用してファイル リストを反復処理します。これは、出力の書き込みにも使用できます。格納する情報が同種 (ファイルごとに 1 つのスカラーなど) の場合は、配列または構造体配列を使用します。ただし、情報がファイルごとに異なる場合 (ベクトルや行列のサイズが異なる場合など) は、代わりにセル配列を使用してください。

通常の配列を使用した例:

filelist = dir('*.wav');
% Pre-allocate an array to store some per-file information.
result = zeros(size(filelist));
for index = 1 : length(filelist)
    fprintf('Processing %s\n', filelist(index).name);
    % Read the sample rate Fs and store it.
    [y, Fs] = wavread(filelist(index).name);
    result(index) = Fs;
end
% result(1) .. result(N) contain the sample rates of each file.

cell 配列を使用した例:

filelist = dir('*.wav');
% Pre-allocate a cell array to store some per-file information.
result = cell(size(filelist));
for index = 1 : length(filelist)
    fprintf('Processing %s\n', filelist(index).name);
    % Read the data of the WAV file and store it.
    y = wavread(filelist(index).name);
    result{index} = y;
end
% result{1} .. result{N} contain the data of the WAV files.
于 2012-07-20T10:18:33.777 に答える