how to create wave files with different names in loop
Show older comments
I have to create a database of sounds for different speakers with different words. I wanted to know if it is possible to do it using loop. I have to use the audiowrite function like this:
audiowrite('file.wav', variable, samplingfrequency)
Is it possible to alter the filename to accomodate names a1-a11.wav and then s1-s11.wav in loop?
Answers (2)
Image Analyst
on 19 Feb 2017
Create the filenames with sprintf:
for loopIndex = 1 : whatever
filename = sprintf('a%d.wav', loopIndex);
% code to create audio....
end
Similar code for s:
filename = sprintf('s%d.wav', loopIndex);
3 Comments
Amritha V
on 19 Feb 2017
Image Analyst
on 19 Feb 2017
You'll need to explain better why sprintf() doesn't work. Because when I run the code below, it seems to make up the filenames exactly as you asked:
for loopIndex = 1 : 11
% Insert code here to create audio....
% Then save it to disk.
filename = sprintf('a%d.wav', loopIndex);
fprintf('Now saving to file %s\n', filename);
% audiiowrite(.......
end
And when you run it, you'll see the filenames you want:
Now saving to file a1.wav
Now saving to file a2.wav
Now saving to file a3.wav
Now saving to file a4.wav
Now saving to file a5.wav
Now saving to file a6.wav
Now saving to file a7.wav
Now saving to file a8.wav
Now saving to file a9.wav
Now saving to file a10.wav
Now saving to file a11.wav
So exactly what does "Actually it isn't working" mean?
I can guarantee you that the fact that you're creating and saving audio files has no effect whatsoever on how sprintf() creates the filenames.
SARVESH AGRAWAL
on 29 Jan 2018
This works! Thank you so much!
John D'Errico
on 19 Feb 2017
Edited: John D'Errico
on 19 Feb 2017
0 votes
Why? Effectively this same question gets asked all the time.
Surely you can create a cell array, or perhaps even better, a struct array that contains all of your sounds. The virtue of that is not you can simply use indexing, rather than needing to create the name of a variable on the fly. Using such variables is difficult, it is slow, it tends to create bugs. In general, just poor programming style. So just learn to use indexing, with cell arrays or struct arrays, and you get all of the benefits from doing so, like speed, efficiency, etc.
Your database can remain entirely in MATLAB, as one variable. Or, you can save that variable to a .mat file.
A crutch is a nice thing if it helps you to walk when you cannot walk otherwise. But if you really don't need that crutch at all, why would you want it?
1 Comment
Amritha V
on 19 Feb 2017
Categories
Find more on Audio I/O and Waveform Generation 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!