Seeking a little clarification on using sprintf with %d and formatSpec etc. to load similar *.MAT filenames that only differ in name by number.
Afficher commentaires plus anciens
Hi,
I have previously written code to successfully load multiple *.MAT files from the directory that were named "Position01.mat ... Position10.mat" using ..
NumFiles = 10;
AudioCell = cell(2, NumFiles);
for k = 1: NumFiles;
Position = sprintf('Position%02d.mat', k);
AudioCell{1,k} = importdata(Position);
AudioCell{2,k} = Position;
end
I want to do the same for a group of files named "Band200Hz.mat", "Band400Hz.mat" ... by octave band to ... "Band12000Hz.mat" and I've tried to specify the filename format in a few different ways, but not been able to find the symbol(s) that works. The documentation on format specifiers isn't necessarly too clear or easy to understand for me. I think the biggest complication I (think I) face is that there isn't a ocnsistent number of different digits in each file; i.e., some have 3, like 'Band400Hz.mat' up to five as with 'Band20000Hz', so can't specify in a similar way to the first example by simply adding 'Hz' to the end, after the number specifier. Thusly ...
NumBands = 8;
MasterCell = cell(2, NumBands);
for k = 1 : NumBands
Band = sprintf('Band%02dHz.mat', k);
MasterCell{1,k} = importdata(Band);
MasterCell{2,k} = Band;
end
A "brute force" workaround would be to put the octave band filtered signal *.MAT files into their own directory and use ...
dir('*.mat')
But I'd prefer to avoid that.
Any help or helpful links regarding this kind of format specification would be greatly appreciated. If there are any entirely different approaches, I'm interested to find out about those too. Thanks a lot!
Réponse acceptée
Plus de réponses (1)
Asad (Mehrzad) Khoddam
le 25 Oct 2020
you can use:
sprintf('Band%dHz.mat', k*200);
6 commentaires
Peter Beringer
le 25 Oct 2020
per isakson
le 25 Oct 2020
>> sprintf('Band%dHz.mat', 32*200)
ans =
'Band6400Hz.mat'
per isakson
le 25 Oct 2020
"I hope I'm being clear enough in how the files are named" I for one doesn't get it.
Peter Beringer
le 25 Oct 2020
per isakson
le 25 Oct 2020
Add the line
assert( isfile(Band), 'Cannot find: "%s"', Band )
before importdata().
Peter Beringer
le 25 Oct 2020
Catégories
En savoir plus sur Audio and Video Data dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
