how to create wave files with different names in loop

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)

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

Thank you so much. Actually it isn't working. Probably with audio, the function works differently?
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.
This works! Thank you so much!

Sign in to comment.

John D'Errico
John D'Errico on 19 Feb 2017
Edited: John D'Errico on 19 Feb 2017
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

Thank you so much for your reply. I did consider doing that, but then it wouldn't be possible for me to play it as a sound file right? Because if I save it in .mat format, I would only be able to process it using MATLAB itself and not otherwise? I know it's asking too much, but I was wondering if it was possible to do it. Otherwise I will have to create a struct or a cell array.

Sign in to comment.

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Asked:

on 19 Feb 2017

Commented:

on 29 Jan 2018

Community Treasure Hunt

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

Start Hunting!