How to use few files from audioDatastore after reading the whole wav files?

I am using audioDatastore for reading wav files from a folder then assign lables to each file. Now I want to use only few of those labels to train my model not all. My labels are there in ads.Labels. Could any one tell me how to do that, before splitting using them as:
[adsTrain,adsTest] = splitEachLabel(ads,0.8);
Here ads should have only some selected labels say 1, 2, 3 out of 1,2,3,4,5,6,7.
I am not able to write that conditional statement here for object ads.
Thanks

 Accepted Answer

Hi Krishna,
You can use subset for this. Something like:
ads = subset(ads,ads.Labels==1 || ads.Labels==2)

12 Comments

@jibrahim Thank you for subset. However here my wav files are random and I have coded the emotion labels from them as 01,02,.... and replace into strings as below:
emotions = replace(emotionCodes,{'01','03','04','05','07'}, {'Neutral','Happy','Sad','Angry','Happy'});
ads.Labels = emotions;
ads = subset(ads,ads.Labels==Neutral|| ads.Labels==Happy||ads.Labels==sad ||ads.Labels==Angry||ads.Labels==Happy);
I am getting en error: Unrecognized function or variable 'Neutral'.
however the lable is there. Tried this also, without replacing by strings:
ads.Labels = emotionCodes;
ads = subset(ads,ads.Labels==01 || ads.Labels==03 ||ads.Labels==04 ||ads.Labels==05||ads.Labels==07);
Then the error is:
Operator '==' is not supported for operands of type 'cell'.
Please guide
Hi Krisna,
If the label is a string or char, then make sure you use a string or char in your comparison. For example:
emotions = replace(emotionCodes,{'01','03','04','05','07'}, {'Neutral','Happy','Sad','Angry','Happy'});
ads = subset(ads,strcmp(ads.Labels,'Happy')||strcmp(ads.Labels,'sad') ||strcmp(ads.Labels,'Angry'));
Sir my emotions is a cell datatypes so now its throwing error:
Operator '==' is not supported for operands of type 'cell'.
Tried cell to categorical but not working.
emotions = replace(emotionCodes,{'01','03','04','05','07'}, ...
{'Neutral','Happy','Sad','Angry','Happy'});
ads = subset(ads,strcmp(ads.Labels=='Neutral')|| strcmp(ads.Labels,'Happy')||strcmp(ads.Labels,'sad') ||strcmp(ads.Labels,'Angry'));
Here is a simple example with some files from the folder toolbox/audio/samples:
cd(matlabroot)
cd toolbox/audio/samples/
ads = audioDatastore(pwd);
% Just use the first three files
ads = subset(ads,1:3);
% Assign some random labels. The Labels are in a cell array
ads.Labels = {'a','b','c'};
% Now pick just the files with Label equal to 'a'
ads = subset(ads,strcmp(ads.Labels,'a'))
Notice there is no use of the '==' sign.
You can use the == sign if the Labels are categoricals. for example:
cd(matlabroot)
cd toolbox/audio/samples/
ads = audioDatastore(pwd,"LabelSource","foldernames");
class(ads.Labels) % categorical
ads = subset(ads,ads.Labels=='samples') % this picks all the files, as they all have the same label here
Thank you again, But noe of this is working for me. Let me eloborate the issue. Below is the code I am using to create audioDatastore;
clc;clear all;close all;
ads=audioDatastore(fullfile('/folder/'));
filepaths = ads.Files;
new=split(filepaths,'/');
emotionCodes=(new);
emotions = replace(emotionCodes,{'01','03','04','05','07'},...
{'Neutral','Happy','Sad','Angry','Happy'});
ads.Labels=emotions;
ads = subset(ads,ads.Labels=='Happy'); %here emotions and emotionCodes all datatypes are cell, For example 1000X1 cell
The error for above code is : Operator '==' is not supported for operands of type 'cell'.
There are total 09 labels I am using only 05, out of these five, two are having same labels 'Happy'. I want only these four label files in ads. ie. 'Neutral','Happy','Sad','Angry','Happy'. If I am extending subset for other labels
the error is same as above.
ads =subset(ads,strcmp(ads.Labels=='Happy'),strcmp(ads.Labels=='Neutral'),...
strcmp(ads.Labels=='Angry'),strcmp(ads.Labels=='Sad'));
Regards
You are mixing strcmp and ==, which is incorrect.
This pattern should work.
% Just use these files as an example
cd(matlabroot)
cd('toolbox/audio/samples');
ads=audioDatastore(pwd);
% Select a few
ads = subset(ads,1:5);
emotionCodes = {'01' '03' '04' '05' '07'};
emotions = replace(emotionCodes,{'01','03','04','05','07'},...
{'Neutral','Happy','Sad','Angry','Happy'});
ads.Labels=emotions;
ads = subset(ads,strcmp(ads.Labels,'Happy'));
ok sir got it, however its working only for a single label, not for all four labels:
ads = subset(ads,strcmp(ads.Labels,'Happy')||strcmp(ads.Labels,'Sad') ||strcmp(ads.Labels,'Angry'));
Error:Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar values. Use the ANY or ALL functions to reduce operands to logical scalar values.
Did I overlook where you attached your data? I can't find it. Where is it?
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
ads = subset(ads,strcmp(ads.Labels,'Happy')|strcmp(ads.Labels,'Sad')|strcmp(ads.Labels,'Angry'));
@Walter Roberson Thank you sir, you are awalys a life saver. So finally this is a partial answer with the initial accepted answer of this problem.
@jibrahim Thanks you so much sir.
@Image Analyst Thats a genral question I think, no specific data is needed.
My question was can I use the ads objcet for a specifca set of labels?
these labels are cell sturcture.
Regards

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!