create an array of audio files and their length in seconds in matlab

You can use the dir function to get a list of audio files in a specific directory, then loop through each file and use audioinfo function to get the audio length in seconds.

Here's an example code snippet:

main.m
% specify the directory containing audio files
dir_path = '/path/to/audio/files/';

% get a list of audio files in the directory
audio_files = dir(fullfile(dir_path,'*.wav'));

% loop through each audio file
for i = 1:length(audio_files)
    filename = fullfile(dir_path,audio_files(i).name);
    audio_info = audioinfo(filename);
    audio_length_sec(i) = audio_info.Duration;
end

% audio_length_sec is now an array containing lengths of each audio file in seconds
467 chars
15 lines

In the example above, we first specify the directory containing audio files using dir_path variable. We then use dir function to get a list of audio files in that directory using the pattern *.wav. You can change this pattern to match the audio format you are using.

We then loop through each audio file in the directory using a for loop. For each file, we use audioinfo function to get the audio information and store the audio length in seconds in an array called audio_length_sec.

Note that this code assumes all audio files in the directory have the same format and parameters. If this is not the case, you may need to check the audio information for each file to make sure they match.

related categories

gistlibby LogSnag