%% Label Count when Labels Is Specified by Table
% 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)

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

%
% Copyright 2018 The MathWorks, Inc.