Reading file names from a certain directory into a cell array to compare it with a different cell array
13 views (last 30 days)
Show older comments
I have a folder with a few .wav files in them. The name of the file is the word the file contain. I need to compare an entered string to the file names and I need to be able to highlight which of those were not used in the string. This chunk of code will be written into this function
global Speaker1WordList Speaker2WordList Speaker3WordList; % These are string containing all the words that was entered by the user in the text edit field
% Initliazing strings to hold invalid attempted words
InvalidString1 = "";
InvalidString2 = "";
InvalidString3 = "";
% Cell arrays to store the strings and drop down selections for each row
Strings = {split(lower(app.Turn1EditField.Value)), split(lower(app.Turn2EditField.Value)), split(lower(app.Turn3EditField.Value)), split(lower(app.Turn4EditField.Value)), split(lower(app.Turn5EditField.Value)), split(lower(app.Turn6EditField.Value)), split(lower(app.Turn7EditField.Value)), split(lower(app.Turn8EditField.Value))};
Drop_Downs = {app.DropDown.Value, app.DropDown_2.Value, app.DropDown_3.Value, app.DropDown_4.Value, app.DropDown_5.Value, app.DropDown_6.Value, app.DropDown_7.Value, app.DropDown_8.Value};
% This section read out the words in the order it was typed, row by row according to the speaker specified in the drop down menu
for ii = 1:8 % There is 8 edit fields for sentences to be entered
if Strings{ii} ~=""
for jj = 1:height(Strings{ii})
if Drop_Downs{ii} == "Speaker 1"
cd("") % To Directory containing speaker 1's words
if contains(Speaker1WordList, Strings{ii}{jj}) == 1
sound(audioread(strcat(Strings{ii}{jj}, ".wav")));
pause(.6)
else
InvalidString1 = append(InvalidString1, " ", Strings{ii}{jj});
end
elseif Drop_Downs{ii} == "Speaker 2"
cd("") %Speaker 2's words
if contains(Speaker2WordList, Strings{ii}{jj}) == 1
sound(audioread(strcat(Strings{ii}{jj}, ".wav")));
pause(.6)
else
InvalidString2 = append(InvalidString2, " ", Strings{ii}{jj});
end
elseif Drop_Downs{ii} == "Speaker 3"
cd("") %Speaker 3's words
if contains(Speaker3WordList, Strings{ii}{jj}) == 1
sound(audioread(strcat(Strings{ii}{jj}, ".wav")));
pause(.6)
else
InvalidString3 = append(InvalidString3, " ", Strings{ii}{jj});
end
end
end
end
pause(.3)
end
% Highlighting invalid words whose use was attempted
app.Speaker1Invalid.Value = InvalidString1;
app.Speaker2Invalid.Value = InvalidString2;
app.Speaker3Invalid.Value = InvalidString3;
% Checking to see which of the words we have in our file was not used
cd("") % Back to main directory
I know how to do the comparisson just not how to get the filesnames from the directory
Below is an image of the app tab that is relevant to the question for some context. The words that are present in the directory is "een twee drie vier vyf"
0 Comments
Accepted Answer
Stephen23
on 25 Aug 2023
Edited: Stephen23
on 26 Aug 2023
"I know how to do the comparisson just not how to get the filesnames from the directory"
Use DIR:
For example:
P = 'absolute or relative path to where the files are saved';
S = dir(fullfile('*.wav'));
C = {S.name} % cell array of matching filenames
You can also use wildcards in the path itself, i.e. to search multiple subfolders for particular files using just one DIR call (note one wildcard * matches any characters, two wildcards ** match folders recursively: read the DIR documentation to know more).
By the way, it is recommended to avoid CD in code (it is slow, forces MATLAB to rescan the folders for MATLAB code files, and makes debugging harder) and to avoid changing the current directory or search path just to access data files. The recommended approach is to use absolute/relative filenames, for which FULLFILE will be very useful:
3 Comments
More Answers (0)
See Also
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!