Clear Filters
Clear Filters

How to save regular audio files as variables with for loop?

5 views (last 30 days)
I want to save regular audio files as aa1.wav, aa2.wav ... aai.wav in a variable via audioread syntax.
For example,
load handel.mat
for i=1:2
Y{i} = audioread('aa1.wav');
How can I do?

Accepted Answer

Pooja Kumari
Pooja Kumari on 28 Sep 2023
Dear SeokWon,
It is my understanding that you are facing issues with saving regular audio files such as “aa1.wav”, “aa2.wav”, “aa3.wav,., “aa1.wav” as variables using “for” loop via audioread syntax.
There is an error in your code in “for” loop in which you have specified “aa1.wav” file, every time the loops run it will store file1, you can read different audio files such as “aa1.wav”, “aa2.wav”…”aai.wav” using “sprintf” function in MATLAB. You can refer to the below documentation for more information on “sprintf” function:
Below is the code for your reference:
% Load handel.mat if it is not already in your workspace
load handel.mat
% Define the number of audio files you want to read
numFiles = 3;
% Create a cell array to store the audio data
Y = cell(numFiles, 1);
% Loop through the desired number of files
for i = 1:numFiles
% Construct the file name using the desired format
fileName = sprintf('aa%d.wav', i);
% Read the audio file using audioread and store it in the cell array
Y{i} = audioread(fileName);
I hope this helps!
Pooja Kumari

More Answers (0)

Community Treasure Hunt

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

Start Hunting!