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 Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by