How do you play a sound using sound("changing string variable", fs)?
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
[oddball2, fs]=audioread('AtlCanSC_2_7.35779_0.960_M.wav'); % other audio files are named oddball3, oddball4, etc
....
s1 = string('oddball');
s2 = num2str(n); % n is within a loop and increasing by 1
s3 = strcat(s1,s2);
sound(s3, fs) % s3 thus correlates to "oddball2"
Even though I can see the variable s3 correlates to the string "oddball2", it gives the error: "Audio data must be real and floating point."
Is there something I am missing? Is there any way I can use a string variable for the sound call or will I have to manually input sound(oddball2, fs) ; sound(oddball3, fs) ; sound(oddball4, fs), etc
1 commentaire
Walter Roberson
le 15 Sep 2022
Please read http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval for information about why we strongly recommend against creating variable names dynamically.
Réponses (1)
Moksh
le 5 Sep 2023
Modifié(e) : Moksh
le 6 Sep 2023
Hi Luke,
As per my understanding, you are trying to play multiple audio files through a loop.
The error arises as the ‘sound’ function in MATLAB does not take a string input. This function takes a double input.
In the above example, the concatenated string ‘s3’ does have a similar name as the audio file, but its datatype is string rather than a double array. You can simply check this by printing the ‘class’ values for ‘s3’ and ‘oddball2’.
% Creating an example audio file
load chirp.mat
filename = 'chirp.wav';
audiowrite(filename,y,Fs);
clear y Fs
% Reading the audiofile
[oddball2, fs] = audioread("chirp.wav");
% Same string concatenation technique
s1 = string('oddball');
s2 = num2str(2);
s3 = strcat(s1, s2);
% sound(s3, fs)
c1 = class(s3)
c2 = class(oddball2)
Assuming that all the audio files are in the same folder, you can use the ‘dir’ function in MATLAB to get a list of names of all the files in that folder. Then you can simply loop over this name array and read the corresponding audio file. You can refer to the example code below:
% Specify the path to your desginated folder in the dir function
allFiles = dir("Audio_Directory");
allNames = {allFiles.name};
sz = size(allNames);
n = sz(2);
% Looping over all audio files
for i = 1 : n
[oddball, fs] = audioread(allNames{i});
sound(oddball, fs);
end
For further understanding of the ‘dir’ and ‘audioread’ functions please refer to the following documentation.
Hope this helps!
1 commentaire
Stephen23
le 5 Sep 2023
Modifié(e) : Stephen23
le 6 Sep 2023
The code has a few bugs that need to be fixed, e.g.:
- use curly-braces to access the content of ALLNAMES.
- either filter/remove foldernames (including dot-directory names) from ALLNAMES, or ensure that they are not returned by DIR by specifying a suitable filename pattern (with wildcards as required).
Fixed and improved code:
P = "absolute or relative path to where the files are saved";
S = dir(fullfile(P,"*.wav"));
for k = 1:numel(S)
F = fullfile(P,S(k).name);
[y,fs] = audioread(F);
sound(y,fs)
end
Voir également
Catégories
En savoir plus sur Audio and Video Data dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!