When the variable name is changed, it gives error

조회 수: 1 (최근 30일)
Shaya s
Shaya s 2017년 8월 7일
댓글: Shaya s 2017년 8월 7일
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
Stephen23 2017년 8월 7일
Question: what function does the ellipsis have?:
'C:\datasets\dataset_Experiment1\segments_all\...'
Shaya s
Shaya s 2017년 8월 7일
Sorry, I don't understand your question. If you mean about the address, I had deleted the ending part of it because it was long.

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

채택된 답변

Stephen23
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".
  댓글 수: 1
Shaya s
Shaya s 2017년 8월 7일
Yes exactly I understood. Actually there's not EXPN. Great thanks for the comment. I'll use it.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by