How can I automatically let Matlab input the file in sequence?

How can I let Matlab automatically input the file itself rather than one by one myself?
I mean, I want to put `Sample 1.wav` and then output `Sample 1.png` and then
put `Sample 2.wav` and then output `Sample 2.png` and then put `Sample 3.wav` and then output `Sample 3.png`
I do not want to type myself 1, 2, 3 and rather let the matlab run itself from `1 to 1,000`
[y,Fs] = audioread('sample1.wav');
spectrogram(y,'yaxis')
saveas(gcf,'sample1.png')
Then
[y,Fs] = audioread('sample2.wav');
spectrogram(y,'yaxis')
saveas(gcf,'sample2.png')
Then
[y,Fs] = audioread('sample3.wav');
spectrogram(y,'yaxis')
saveas(gcf,'sample3.png')

8 commentaires

You can use a loop.
Inside of the loop body, build a string with the filename to read/write. Then use those variables when you call audioread() and saveas().
Can you try this:
>> for i=1:1000
fn_read ="sample"+i+".wav"; % this is the file name to read from
fn_write="sample"+i+".png"; % this is the file name to write to
[y,Fs] = audioread(fn_read);
spectrogram(y,'yaxis')
saveas(gcf,fn_write)
end
if it works for you, let me know.
Thanks. Sorry for late reply. It take me time to debug.
It have error:
Error using which
Arguments must contain a character vector.
Error in multimedia.internal.io.absolutePathForReading (line 10)
whichFileName = which(filename);
Error in audioread (line 74)
filename = multimedia.internal.io.absolutePathForReading(...
Error in Spectrogram (line 35)
[y,Fs] = audioread("sample"+i+".wav");
I had tried to :
%%
fn_read ="sample1.wav";
[y,Fs] = audioread(fn_read);
sound(y,Fs);
It is still the same.
filename = 'sample1.wav';
[y,Fs] = audioread('sample1.wav');
sound(y,Fs);
Is working. Does audioread cannot take filename?
what version of matlab are you using?
Matlab R2017b version.
That's older than mine. Older versions worked a little different with strings.
Can you try the following:
>> for i=1:1000
fn_read ="sample"+i+".wav"; % this is the file name to read from
fn_read = char(fn_read); % convert the string to a char array
fn_write ="sample"+i+".png"; % this is the file name to write to
fn_write = char(fn_write); % convert the string to a char array
[y,Fs] = audioread(fn_read);
spectrogram(y,'yaxis')
saveas(gcf,fn_write)
end
let me know if it works.
The simplest is to follow one of the approaches shown in the MATLAB documentation:
Thanks. it works well!
great news Alan.
What did you go with, my suggestion or did you use Stephen's solution?
If you used the solution, please mark it as the answer.

Connectez-vous pour commenter.

 Réponse acceptée

Can you try the following:
>> for i=1:1000
fn_read ="sample"+i+".wav"; % this is the file name to read from
fn_read = char(fn_read); % convert the string to a char array
fn_write ="sample"+i+".png"; % this is the file name to write to
fn_write = char(fn_write); % convert the string to a char array
[y,Fs] = audioread(fn_read);
spectrogram(y,'yaxis')
saveas(gcf,fn_write)
end
let me know if it works.

2 commentaires

For MATLAB 2017b
Defining strings and then converting to char is rather convoluted.
It is more efficient to use sprintf, as the documentation shows:

Connectez-vous pour commenter.

Plus de réponses (1)

for i=1:1000
[y,Fs] = audioread(['sample' num2str(i) '.wav']);
spectrogram(y,'yaxis')
saveas(gcf,['sample' num2str(i) '.png'])
end

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by