This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

countEachLabel

Count number of unique labels

Syntax

tbl = countEachLabel(ADS)
tbl = countEachLabel(ADS,'TableVariable',VariableName)

Description

example

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.

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: {
                              ' .../build/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 26 more
                              }
                      Labels: [samples; samples; samples ... and 26 more categorical]
    AlternateFileSystemRoots: {}
              OutputDataType: 'double'

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

tbl = countEachLabel(ads)
tbl =

  1x2 table

     Label     Count
    _______    _____

    samples     29  

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: {
                              ' .../build/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 26 more
                              }
    AlternateFileSystemRoots: {}
              OutputDataType: 'double'
                      Labels: {}

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')

countDurationLabels = countEachLabel(ADS,'TableVariable','IsLongerThan30Seconds')
countNumberOfChannelLabels =

  3x2 table

    NumberOfChannels    Count
    ________________    _____

        mono             16  
        stereo           11  
        unknown           2  


countDurationLabels =

  3x2 table

    IsLongerThan30Seconds    Count
    _____________________    _____

           false              18  
           true                7  
           unknown             4  

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

Introduced in R2018b