Finding area of a plot/graph in a for loop and save answer into array

1 次查看(过去 30 天)
Hello Community,
I have a few audio files.
When I open one of these files, I have a file named 'data' in my workspace which contains the data set to plot it's graph.
I am trying to get the area of every audio file in a loop, and save it in an array in a column.
Is anyone able to help with my code especially on : [data,fs] = audioread(Extracted_files(k).name);
I am running into errors for this particular line.
note that every graph is different..
A = zeros(210,1); %set array for allocating the area of 210 of these audio files
m=1;
cd 'my directory'
Extracted_files = dir;
nExtracted_files = length(Extracted_files);
for k = 1:nExtracted_files
[data,fs] = audioread(Extracted_files(k).name);
area = trapz(data)
A(m) = area;
m = m + 1;
end

采纳的回答

Geoff Hayes
Geoff Hayes 2020-4-16
whalelady - you mention that you are running into errors with
data,fs] = audioread(Extracted_files(k).name);
but you don't mention what the errors are. Is it
Error using audioread (line 74)
The filename specified was not found in the MATLAB path.
? When you call dir, some unexpected "files" may be added to your list (perhaps "." or ".."). I recommend that you add a filter like
Extracted_files = dir('*.wav');
or whatever extension makes sense for your files. Also, there is no need for the m variable as k can be used in its place. Your code could look like
cd 'my directory'
Extracted_files = dir('*.wav');
nExtracted_files = length(Extracted_files);
A = zeros(nExtracted_files,1);
for k = 1:nExtracted_files
[data,fs] = audioread(Extracted_files(k).name);
area = trapz(data)
A(k) = area;
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Using audio files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by