Main Content

countEachLabel

Count number of unique labels

Description

tbl = countEachLabel(ADS) counts the number of times each unique label occurs in the datastore. In other words, it counts the number of files with each unique label. The output tbl is a table with variable names Label and Count.

example

tbl = countEachLabel(ADS,'TableVariable',VariableName) counts the number of times each unique label occurs in the datastore. When the datastore Labels property is specified by a table, you must specify VariableName. VariableName is the table variable (column) name you want to count.

example

Examples

collapse all

Specify the file path to the audio samples included with Audio Toolbox™.

folder = fullfile(matlabroot,'toolbox','audio','samples');

Create an audio datastore that points to the specified folder. Specify the LabelSource property as foldernames, so that the label associated with each file is set to the folder name that contains the file.

ads = audioDatastore(folder,'Labelsource','foldernames')
ads = 
  audioDatastore with properties:

                       Files: {
                              ' .../matlab/toolbox/audio/samples/Ambiance-16-44p1-mono-12secs.wav';
                              ' .../matlab/toolbox/audio/samples/AudioArray-16-16-4channels-20secs.wav';
                              ' .../toolbox/audio/samples/ChurchImpulseResponse-16-44p1-mono-5secs.wav'
                               ... and 36 more
                              }
                     Folders: {
                              ' .../Bdoc24b.2725827/build/runnable/matlab/toolbox/audio/samples'
                              }
                      Labels: [samples; samples; samples ... and 36 more categorical]
    AlternateFileSystemRoots: {}
              OutputDataType: 'double'
           OutputEnvironment: 'cpu'
      SupportedOutputFormats: ["wav"    "flac"    "ogg"    "opus"    "mp3"    "mp4"    "m4a"]
         DefaultOutputFormat: "wav"

Call countEachLabel to count the number of times each unique label occurs.

tbl = countEachLabel(ads)
tbl=1×2 table
     Label     Count
    _______    _____

    samples     39  

If the Labels property of an audio datastore is specified as a table, you must specify the table variable name when counting labels.

Specify the file path to the audio samples included with Audio Toolbox™.

folder = fullfile(matlabroot,'toolbox','audio','samples');

Create an audio datastore that points to the specified folder.

ADS = audioDatastore(folder)
ADS = 
  audioDatastore with properties:

                       Files: {
                              ' .../matlab/toolbox/audio/samples/Ambiance-16-44p1-mono-12secs.wav';
                              ' .../matlab/toolbox/audio/samples/AudioArray-16-16-4channels-20secs.wav';
                              ' .../toolbox/audio/samples/ChurchImpulseResponse-16-44p1-mono-5secs.wav'
                               ... and 36 more
                              }
                     Folders: {
                              ' .../Bdoc24b.2725827/build/runnable/matlab/toolbox/audio/samples'
                              }
    AlternateFileSystemRoots: {}
              OutputDataType: 'double'
           OutputEnvironment: 'cpu'
                      Labels: {}
      SupportedOutputFormats: ["wav"    "flac"    "ogg"    "opus"    "mp3"    "mp4"    "m4a"]
         DefaultOutputFormat: "wav"

The file names contain information about the files. Parse the file names to collect information about whether a file is mono or stereo and whether a file is longer than thirty seconds. Create a table containing the parsed information and then set the Labels property of the audio datastore to the label table.

numFiles = numel(ADS.Files);

numChannels = cell(numFiles,1);
isLong = cell(numFiles,1);

for i = 1:numFiles
    if ~isempty(strfind(ADS.Files{i},'mono'))
        numChannels{i} = 'mono';
    elseif ~isempty(strfind(ADS.Files{i},'stereo'))
        numChannels{i} = 'stereo';
    else
        numChannels{i} = 'unknown';
    end
    
    secs = str2double(regexp(ADS.Files{i}, '-(\d+)secs', 'tokens', 'once'));
    if secs > 30
        isLong{i} = true;
    elseif secs <= 30
        isLong{i} = false;
    else
        isLong{i} = 'unknown';
    end
end
labelTable = table(numChannels,isLong, ...
    'VariableNames',{'NumberOfChannels','IsLongerThan30Seconds'});

ADS.Labels = labelTable;

Call countEachLabel on the audio datastore and specify the TableVariable as NumberOfChannels. Call countEachLabel and specify the TableVariable as IsLongerThan30Seconds.

countNumberOfChannelLabels = countEachLabel(ADS,'TableVariable','NumberOfChannels')
countNumberOfChannelLabels=3×2 table
    NumberOfChannels    Count
    ________________    _____

        mono             26  
        stereo           10  
        unknown           3  

countDurationLabels = countEachLabel(ADS,'TableVariable','IsLongerThan30Seconds')
countDurationLabels=3×2 table
    IsLongerThan30Seconds    Count
    _____________________    _____

           false              28  
           true                6  
           unknown             5  

Input Arguments

collapse all

Specify ADS as an audioDatastore object.

Label table variable name, specified as a character vector or string that corresponds to a table variable of the Label property.

This syntax is required if the Label property of audioDatastore is specified by a table.

Data Types: char | string

Output Arguments

collapse all

Table of label counts, returned as a two-column table containing the name of each label in ADS and the number of files associated with each label.

Data Types: table

Version History

Introduced in R2018b