Main Content

readfile

Read specified audio file from datastore

Since R2024a

    Description

    data = readfile(ads,I) returns audio extracted from the file in the datastore at index I.

    example

    [data,info] = readfile(ads,I) also returns a structure containing information about the audio file.

    example

    Examples

    collapse all

    Create an audioDatastore object.

    folder = fullfile(matlabroot,"toolbox","audio","samples");
    ads = audioDatastore(folder);

    Read the audio data and the file information from the fifth file in the datastore. Display the file name.

    [data,info] = readfile(ads,5);
    
    [path,name,ext] = fileparts(info.FileName);
    disp(name)
    Click-16-44p1-mono-0.2secs
    

    Use the sound function to listen to the audio.

    sound(data,info.SampleRate)

    Input Arguments

    collapse all

    Audio datastore, specified as an audioDatastore object.

    Audio file index, specified as a positive integer.

    Output Arguments

    collapse all

    Audio data, returned as an M-by-N matrix, where M is the total number of samples per channel in the file and N is the number of channels.

    File information, returned as a structure containing the following fields.

    • FileName — Name of the file.

    • Label — Label of the file. This field is present only if you set the Label property of the audioDatastore.

    • SampleRate — Sample rate of the audio file.

    Version History

    Introduced in R2024a