countEachLabel
Count number of unique labels
Description
counts the number of times each unique label occurs in the datastore. When the datastore
tbl
= countEachLabel(ADS
,'TableVariable',VariableName
)Labels
property is specified by a table, you must specify
VariableName
. VariableName
is the table variable
(column) name you want to count.
Examples
Label Count
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
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)
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
ADS
— Audio datastore
audioDatastore
object
Specify ADS
as an audioDatastore
object.
VariableName
— Label table variable name
character vector | string
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
tbl
— Table of label counts
two-column table
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
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)