sprintf only get the first character in a string array
Show older comments
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?
Accepted Answer
More Answers (1)
Walter Roberson
on 19 Oct 2018
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
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!