Error/fault in code, 'Unrecognized function or variable'

I have the following loop within my script. The intention is to process thousands of files within a folder through the function PG_DFT. This works fine, but at a certain point I get an error:
Unrecognized function or variable 'PG_DFT'.
This is strange because when I run the code again from where the error occurred, everything works fine. The loop will process up to thousands of files before stopping and reporting the error. And when I run it again from where it left off, everything is fine. This is very confusing.
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
SN=strsplit(d(i).name,'.'); %split filename by '.'
SN1=char(SN(1)); %get serial no.
serialNo=str2num(SN1);
date=char(SN(2)); %get date from filename
date=datenum(date, 'yymmddHHMMSS'); %convert date to datenum format
outputdate(:,row+1)=date; %create vector with dates
switch serialNo %get calibration correction
case 5099 %if serial no. is...
S=-176.4; %S = -...
case 5100 %**all of these values apply to
S=-176; %HIGH GAIN setting only**
case 5101
S=-176.2;
case 5102
S=-176.6;
case 5103
S=-176.5;
case 5277
S=-176.3;
case 5278
S=-175.9;
case 5279
S=-176.4;
case 5280
S=-176.4;
case 5281
S=-176.6;
otherwise error('Unknown ST serial');
end
%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
xbit=detrend(xbit);
%removes DC offset
[A]= PG_DFT(xbit,fs,S,N,r,winname,envi,lcut,hcut,atype,tstamp,disppar);
%calculate TOLs (calibrated)
out_f=A(1,:); %get TOL freq bins
A=A(2:end,2:end); %remove freq and time interval data
A=mean(A,1); %take mean across file (get one val per TOL)
output(row,:) = A;
clearvars A
%fill successive rows with output from PG_DFT
row = row + 1;
%move onto next row of output matrix
%If file is too short (rare):
else
%fill data row with zeros
output(row,2:35)=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
end
end

4 commentaires

I recommend using
dbstop if error
and running the code. When it stops, ask
pwd
which -all PG_DFT
ls PG_DFT.m
fopen('all')
Thanks Walter! I will try that if/when it happens again. I am working with thousands of files so it can take several hours to crash. I think it may be an issue with having the .m file located in Google drive though.
Yes, that could do it. If you lose contact with Google Drive for a moment, you can get exactly that kind of problem.
You should make sure that your code is local to the system running MATLAB. You are already using fullfile() so it should not be any problem to split your code from your data.
Yep I can easily move things around it to run it more robustly. I hadn't even realised I was doing that until now, it wasn't my intention, but a result of having things backed up in multiple places! Thank you.

Connectez-vous pour commenter.

Réponses (1)

Image Analyst
Image Analyst le 14 Juil 2021

0 votes

Well evidently it doesn't know that function. Why do you think it should? Do you have an m-file named that in the current folder or on the search path? Is it defined elsewhere in your script?
What does this say
>> which -all PG_DFT
It will probably say there is no such function.

4 commentaires

Louise Wilson
Louise Wilson le 14 Juil 2021
Modifié(e) : Louise Wilson le 14 Juil 2021
It does know the function because it has successfully applied the function in previous iterations of the loop. The function is in an m file in the current folder as you say it should be.
Could it be something to do with running the code via Google Drive, maybe it is a poor connection at a certain point?
Try getting rid of clearvars in the loop. Just set any variables you want reset to null instead. See if that helps. Is the H drive a local drive that you always have access to, or is it a network/cloud drive that might possibly be unseeable at some point in time? See if the error happens if you have everything on a local drive.
Thanks! Answered in replies to Walter's post above.

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Produits

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by