Matlab : 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')
    


  • Hey again @Alan-Daniel

    Don't forget to format your code using backticks ` 🙂

    With that said, I don't know Matlab but here is what I am thinking and found online:

    a='sample';
    b = 1;
    c='.wav';
    d='.png'
    while( b < 1000 )
         bstr = num2str(b);
         inputname = [a bstr c];
         outputname= [a bstr d]
        [y,Fs] = audioread(inputname);
        spectrogram(y,'yaxis')
        saveas(gcf,outputname)
         b = b + 1;
    end
    

    Again, I don't know Matlab and I don't have Matlab installed on my computer so the above code is untested and might need some adjusting! But the idea should be relative solid.

    Hope this works for you in some way. Let me know.



  • Hey again @Alan-Daniel

    Don't forget to format your code using backticks ` 🙂

    With that said, I don't know Matlab but here is what I am thinking and found online:

    a='sample';
    b = 1;
    c='.wav';
    d='.png'
    while( b < 1000 )
         bstr = num2str(b);
         inputname = [a bstr c];
         outputname= [a bstr d]
        [y,Fs] = audioread(inputname);
        spectrogram(y,'yaxis')
        saveas(gcf,outputname)
         b = b + 1;
    end
    

    Again, I don't know Matlab and I don't have Matlab installed on my computer so the above code is untested and might need some adjusting! But the idea should be relative solid.

    Hope this works for you in some way. Let me know.



  • it works, thanks!



  • Awesome. Glad I can be of help.



  • Thanks a lot. You are brillant!


Log in to reply