Reading file names from a certain directory into a cell array to compare it with a different cell array

12 vues (au cours des 30 derniers jours)
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"

Réponse acceptée

Stephen23
Stephen23 le 25 Août 2023
Modifié(e) : Stephen23 le 26 Août 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 commentaires
Wynand
Wynand le 25 Août 2023
Thank you for the well explained answer. It was majorly usefull to me
Stephen23
Stephen23 le 26 Août 2023
@Wynand: I hope that it helped. Please remember to click the accept button!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by