필터 지우기
필터 지우기

Reading files from a directory

조회 수: 3 (최근 30일)
Lara Lirnow
Lara Lirnow 2017년 5월 26일
편집: Lara Lirnow 2017년 5월 28일
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

채택된 답변

Stephen23
Stephen23 2017년 5월 27일
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
Lara Lirnow
Lara Lirnow 2017년 5월 27일
I used dir for reading first and then fullfile. Thank you, your links with examples helped a lot.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by