When the variable name is changed, it gives error

1 Ansicht (letzte 30 Tage)
Shaya s
Shaya s am 7 Aug. 2017
Kommentiert: Shaya s am 7 Aug. 2017
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 Kommentare
Stephen23
Stephen23 am 7 Aug. 2017
Question: what function does the ellipsis have?:
'C:\datasets\dataset_Experiment1\segments_all\...'
Shaya s
Shaya s am 7 Aug. 2017
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 7 Aug. 2017
Bearbeitet: Stephen23 am 7 Aug. 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".
  1 Kommentar
Shaya s
Shaya s am 7 Aug. 2017
Yes exactly I understood. Actually there's not EXPN. Great thanks for the comment. I'll use it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by