Finding area of a plot/graph in a for loop and save answer into array
1 view (last 30 days)
Show older comments
whalelady
on 16 Apr 2020
Commented: Walter Roberson
on 16 Apr 2020
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
2 Comments
Walter Roberson
on 16 Apr 2020
plot (data)
Before you do that, you have to read from the file indicated by Extracted_files(k).name
area = trapz(x,y) %or should I use area(x,y)?
Your code does not define x or y, so ... ?
Accepted Answer
Geoff Hayes
on 16 Apr 2020
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
More Answers (0)
See Also
Categories
Find more on Environment and Settings 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!