%% Split Using Fraction and Label Table
% Specify the file path to the audio samples included with Audio Toolbox™. Create an audio datastore that points to the specified folder.

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

%
% Create a label table with two variables:
% containsMusic -- Can be either true or false.
% instrument -- Can be Guitar, Drums, or Unknown.

containsGuitar = contains(ADS.Files,'guitar','IgnoreCase',true);
containsDrums = contains(ADS.Files,'drum','IgnoreCase',true);
containsMusic = or(containsGuitar,containsDrums);

instrument = strings(size(ADS.Files));
instrument(:) = "Unknown";
instrument(containsGuitar) = "Guitar";
instrument(containsDrums) = "Drums";

%
% Assign the label table to the Labels property of audio datastore to associate the rows of the label table with the rows of the datastore. Call countEachLabel to determine the incidences of containsMusic and instrument.

labels = table(containsMusic,instrument);
ADS.Labels = labels;

containsMusicCount = countEachLabel(ADS,'TableVariable','containsMusic')
instrumentCount = countEachLabel(ADS,'TableVariable','instrument')

%
% Split the datastore ADS into two, based on whether the audio file contains music. ADS1 contains 70% of the audio files that contain music, and ADS2 contains the rest. Call countEachLabel to verify that the ratio of containsMusic == true to containsMusic == false is preserved for the new datastores, within rounding.

[ADS1,ADS2] = splitEachLabel(ADS,0.7,'TableVariable','containsMusic');
ADS1_containsMusicCount = countEachLabel(ADS1,'TableVariable','containsMusic')
ADS2_containsMusicCount = countEachLabel(ADS2,'TableVariable','containsMusic')

%
% Split the datastore ADS into two, based on the type of instrument present in the audio file. ADS3 contains 25% of the audio files that have an instrument label, and ADS4 contains the rest. Call countEachLabel to verify that the ratio of instrument == "drums" to instrument == "guitar" is preserved for the new datastores, within rounding.

[ADS3,ADS4] = splitEachLabel(ADS,0.25,'TableVariable','instrument');
ADS3_instrumentCount = countEachLabel(ADS3,'TableVariable','instrument')
ADS4_instrumentCount = countEachLabel(ADS4,'TableVariable','instrument')

%
% Copyright 2018 The MathWorks, Inc.