Error: Warning: The end of file was reached before the requested samples were read completely. Total number of samples read is 15138816.

4 views (last 30 days)
I am getting an error which says that the file I am reading is too short to read the requested number of samples. However, I know this cannot be the case as I can check the length of the file itself by looking at it before processing. Further, I have inserted lines into my code which means it should only attempt to read the file if it is a certain length. Why is this happening?
%Input variables:
directory={'H:\SoundTrap\wav files\GoatIsland\008_GoatIsland_5280';};
%% For loop to run through series of folders of .wav files:
for j=1:length(directory)
clearvars -except j directory tlo thi filelength; %clear all variables between folders
d=dir(fullfile(folder, '*.wav')); %list all .wav files in path folder
files=length(d); %number of files in folder
output=[]; %create empty output array
clear('A'); %clear output array between folders
row=1; %tool to store output data on new row of output matrix
%'A' at each iteration of loop
%get sampling frequency of first (i.e. all) files in folder
firstfile=fullfile(folder, char(d(1,1).name));
[xbit,fs]=audioread(firstfile, [1 2]);
%% Input variables (using fs from first file)
lcut=0; %low frequency cutoff (will adjust later)
hcut=72000; %high freq cutoff
%Select 2 mins or 5 mins of each .wav depending on data/purpose...
nlo=fs*tlo; %multiply fs by tlo to get starting point
nup=fs*thi; %do the same for end point
N=fs/1; %segment length
window=fs; %frequency bin size
overlap=window/2; %amount of overlap between segments (50%)
nfft=fs; %fft length
for i=1:files %for each file
disp(d(i).name); %display filename
filename=fullfile(folder, d(i).name); %get full filename
wavinfo=audioinfo(filename); %get file info
%Run PSD only if file is 2 mins long (1:59):
%i.e. 119seconds*144000samples
if (wavinfo.TotalSamples>=nup)
[xbit, fs]=audioread(filename, [nlo,nup]);
%read in file
%removes DC offset
[spectra, f]=pwelch(xbit,window,overlap,nfft,fs);
%perform pwelch
%convert output to dB by multiplying by 10*log10
%apply calibration (S)
output(row,:) = calcspectra;
%fill successive rows with output from %pwelch
row = row + 1;
%move onto next row of output matrix
%If file is too short (rare):
%fill data row with zeros
output(row,2:72001)=zeros; %fill row with zeroes
formatSpec=('Error: %s has an unexpected file length! Not processed\n');
fprintf(2, formatSpec, filename);
row=row + 1;
%list of filenames where rows were filled with zeroes is issued in
%a separate _shortfiles.csv
%code goes on to save output of each loop in .csv file...

Answers (0)


Find more on Scripts 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!