When the variable name is changed, it gives error
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2017-8-7
Question: what function does the ellipsis have?:
'C:\datasets\dataset_Experiment1\segments_all\...'
采纳的回答
Stephen23
2017-8-7
编辑:Stephen23
2017-8-7
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".
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!