Inserting a variable into a string of text.
21 views (last 30 days)
Show older comments
Hello everyone,
I am attempting to make a program that can split 40 different sound files into 2 second segments. I can either "hard code" it (write a program that will split just one sound file into 2 second segments) or I can use just a simple function and use if/else statements and one or more loops to achieve the same goal but with many less lines of code (I could save hundreds of lines of code!).
At the moment I have something like this:
filename01='BASSOON\01_AchGottundHerr_bassoon.RESYN.wav';
[y, fs] = audioread(filename01);
samples = length(y);
duration = 2; % seconds
buffer = fix(duration*fs); % samples
nb_of_buffers = fix(samples/buffer); % NB : no overlap
for k = 1:nb_of_buffers
ind_start = 1+(k-1)*buffer;
ind_stop = ind_start+buffer-1;
filename01 = sprintf('Bassoon01-%d.wav', k);
audiowrite(filename01,y(ind_start:ind_stop,:),fs)
end
mkdir Bassoon_snippet01
Bassoon01 = dir('C:\Users\adilp\Desktop\Bach10-mf0-synth\audio_stems_sound\*.wav');
for k = 1:numel(Bassoon01)
Name = Bassoon01(k).name;
if contains(Name,'Bassoon01')
Instrument = 'Bassoon_snippet01';
end
movefile(Name,Instrument);
end
movefile('Bassoon_snippet01','C:\Users\adilp\Desktop\Bach10-mf0-synth\audio_stems_sound\BASSOON');
It works for one file! But I would need to do the same thing forty times! I would have several hundred lines of code and that is simply inefficient.
I am attempting to incorporate an if else statement and/or a loop if necessary, but it does not seem to accomplish much.
%for n = 1
if n == 1
filename0n='BASSOON\01_AchGottundHerr_bassoon.RESYN.wav';
elseif n == 2
filename0n='BASSOON\02_AchLiebenChristen_bassoon.RESYN.wav';
elseif n==3
filename0n='BASSOON\03_ChristederdubistTagundLicht_bassoon.RESYN.wav';
end
[y, fs] = audioread(filename0n); % I need a way to incorporate 'n'
samples = length(y);
duration = 2; % seconds
buffer = fix(duration*fs); % samples
nb_of_buffers = fix(samples/buffer); % NB : no overlap
for k = 1:nb_of_buffers
ind_start = 1+(k-1)*buffer;
ind_stop = ind_start+buffer-1;
filename0n = sprintf('Bassoon0n-%d.wav', k); % I need a way to incorporate 'n'
audiowrite(filename0n,y(ind_start:ind_stop,:),fs) % I need a way to incorporate 'n'
end
mkdir Bassoon_snippet0n % I need a way to incorporate 'n'
Bassoon0n = dir('C:\Users\adilp\Desktop\Bach10-mf0-synth\audio_stems_sound\*.wav');
for k = 1:numel(Bassoon0n) % I need a way to incorporate 'n'
Name = Bassoon0n(k).name; % I need a way to incorporate 'n'
if contains(Name,'Bassoon0n') % I need a way to incorporate 'n'
Instrument = 'Bassoon_snippet0n'; % I need a way to incorporate 'n'
end
movefile(Name,Instrument);
end
movefile('Bassoon_snippet0n','C:\Users\adilp\Desktop\Bach10-mf0-synth\audio_stems_sound\BASSOON'); % I need a way to incorporate 'n'
%end
I believe all I really need is a way to incorporate a variable 'n' into some character strings, where n is a positive integer with values 1 , 2 , ... , 8 , 9 in and is formatted in such a manner that 0n is 01, 02 , ... , 08 , 09. I hope this question is understandable.
Thank you very much!
4 Comments
Answers (0)
See Also
Categories
Find more on Data Type Identification 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!