When the variable name is changed, it gives error
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I'm trying to calculate the duration of some certain episodes including "Exp" and "ExpN" in an audio signal. There's a weird problem that when I change the variable name it doesn't result. Will you take a look at below codes (the error is for the last line for "ExpN_sum_duration"):
dataset_root='C:\datasets\dataset_Experiment1\segments_all\...';
Exp_root=dir(fullfile(dataset_root,'EXP*.wav'));
Exp_no=numel(Exp_root);
for i=1:Exp_no
info=audioinfo(fullfile(dataset_root,Exp_root(i).name));
duration_Exp(i)=info.Duration;
end
Exp_sum_duration= sum(duration_Exp)
%------------------------------------------------------------
ExpN_root=dir(fullfile(dataset_root,'EXPN*.wav'));
ExpN_no=numel(ExpN_root);
for ii=1:ExpN_no
info=audioinfo(fullfile(dataset_root,ExpN_root(ii).name));
duration_ExpN(ii)=info.Duration;
end
ExpN_sum_duration= sum(duration_ExpN)
So as you see there are two parts. In the first part I get the result of the "Exp_sum_duration", whereas for the next part I receive error for "ExpN_sum_duration", which is:
Undefined function or variable 'duration_ExpN'.
Error in Exp_percent_test (line 28)
ExpN_sum_duration= sum(duration_ExpN)
Thanks for reading.
2 commentaires
Stephen23
le 7 Août 2017
Question: what function does the ellipsis have?:
'C:\datasets\dataset_Experiment1\segments_all\...'
Réponse acceptée
Stephen23
le 7 Août 2017
Modifié(e) : Stephen23
le 7 Août 2017
I bet if you looked at the value of ExpN_no it is zero.
The cause will be that there are no files matching that pattern in that directory:
ExpN_root=dir(fullfile(dataset_root,'EXPN*.wav'));
therefore numel returns zero:
ExpN_no=numel(ExpN_root);
and so the loop iterates exactly zero times
for ii=1:ExpN_no
and so the variable duration_ExpN, which is only defined inside that loop, is never defined. Your code is very fragile because it cannot handle highly predictable situations, such as files not being found. To write robust code you have to think not just about "what should my code do in nominal mode", but also "what should my code do when something does not exist / is not loaded / has an invalid value / is corrupted / etc etc".
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!