Reading files from a directory

1 Ansicht (letzte 30 Tage)
Lara Lirnow
Lara Lirnow am 26 Mai 2017
Bearbeitet: Lara Lirnow am 28 Mai 2017
I have a code which cuts audio signal (.wav) into segments written in .txt files. My current code works only with one audio signal, and matching text file. But I need to adjust the code to work with more files. I have two directories; one for .wav files and one for text files which both include more than one file. I need to write the code which reads one by one audio and text file and cuts the audio segment. The code below works with one audio file and one text file.
%read audiofile
[data,Fs] = audioread('sz08010103404.wav');
%read text file with segments
list_zv = fopen('intervali_zvucni_sek.txt','r');
C_zv=cell(size(list_zv)) %put every interval int it's own cell
%signal
N = length(data);
totaldur = N/Fs; % total duration
t = linspace(0,totaldur,N); % create a time vector
for k=1:length(list_zv)
content_zv = fgets(list_zv(k)) %gets every part of the list
d_zv= strsplit(content_zv,',')% splits str at the delimiters specified by delimiter
Z=[] %for results
for n=1:length(d_zv)
y=d_zv{n}
z= strsplit(y,' ') %split the content of current cell
start=z{1} %get the first part of the cell
stop=z{2} %get the second part of the cell
start1 = str2num(start) %turn into numeric
stop1 = str2num(stop)
B(n,1:2) = [start1,stop1];%test matrix
% cut the signal
seg1 = data(t>start1 & t<stop1)
sound(seg1)
ZV{n}=seg1 %put the signal into the matrix
end
end
I tried to open all the files from the directory by using dir function, what I managed to do, but I can't get the contents from text file to read it. Every text file looks like this; 0.205000 0.220000 ,0.23500 0.265000 ,...
Code:
FileList = dir('ODOGS/segments'); %folder with text files for segments
N = size(FileList,1);
FileList_wav = dir('ODOGS/WAV'); %folder with audio files
N_w = size(FileList_wav,1);
for k = 1:N
filename = FileList(k).name % get the file name
for x = 1:N_w
filename_w = FileList_wav(x).name
disp(filename_w)
if filename(k) == filename_w(x) %audio file and text file need to have the same name
%do the rest of the code
end
end
end

Akzeptierte Antwort

Stephen23
Stephen23 am 27 Mai 2017
How to read multiple files is explained extensively in the documentation, on this forum, and in the wiki:
etc
The first thing to decide is if you want to generate the file names, or if you want to read the names of existing files:
  • generate names: use sprintf and fullfile.
  • read names: use dir and fullfile.
You can also find some examples with my FEX submission natsortfiles:
  1 Kommentar
Lara Lirnow
Lara Lirnow am 27 Mai 2017
I used dir for reading first and then fullfile. Thank you, your links with examples helped a lot.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by