sprintf only get the first character in a string array
이전 댓글 표시
Hi, I have 25 audio files of 5 different words and I am trying to get their mfcc's using two for loops. My code looks:
filename = ['asr','cnn','dnn','hmm','tts'];
for i=1:5
for j=1:5
fname = sprintf('%s%d',filename(1,i),j);
disp(fname);
mfcc_i = mfcc(eval(fname), 44100);
end
end
I already have matrices like asr1, asr2...in the workspace. However I got the error like this:
a1
Error using eval
Undefined function or variable 'a1'.
So it looks like sprintf() only reads the first character in the array of strings instead of the first string('asr'). Why does it happen and how I can fix this?
채택된 답변
추가 답변 (1개)
Walter Roberson
2018년 10월 19일
filename = ['asr','cnn','dnn','hmm','tts'];
is not an array of strings. It is exactly the same thing as
filename = horzcat('asr','cnn','dnn','hmm','tts');
which is going to produce
filename = 'asrcnndnnhmmtts';
String arrays use " instead of '
filename = ["asr", "cnn", "dnn", "hmm", "tts"];
If you are using a string array then you can simplify
fname = sprintf('%s%d',filename(1,i),j);
into
fname = filename(1,i) + j;
We firmly recommend against using eval.
Instead of naming your variables asr1 asr2 and so on, use a cell array for them, asr{1}, asr{2} and so on. You can use things like
asr = 1; cnn = 2; dnn = 3; hmm = 4; tts = 5;
soundata = cell(5, 5);
soundata{asr,1} = ....
...
soundata{hmm,3} = ....
...
for J = 1 : 5
for K = 1 : 5
mfcc_results{J,K} = mfcc(soundata{J,K}, 44100);
end
end
카테고리
도움말 센터 및 File Exchange에서 Audio and Video Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!