0

For files with some repeating numerical pattern like file0001, file0002...file0100, I've written the following script to operate on the input files and save the results with the same name.

% The folder has 100 files to be operated on
% "save_filename" is the name of data saved for the "inname" file
for k = 1:100
    inputfiledir = './Data/Category1/';
    if (k<10)
        save_filename = sprintf('score_sp1_a000%d',k);
        inname = sprintf('sp1_a000%d',k);
    elseif (k>=10)
        save_filename = sprintf('score_sp1_a00%d',k);
        inname = sprintf('sp1_a00%d',k);
    end
    if (k>=100)
        save_filename = sprintf('score_sp1_a0%d',k);
        inname = sprintf('sp1_a0%d',k);
    end
    [prediction,score] = predict_class(trainedNet,strcat(inputfiledir,inname,'.wav'));
    save(strcat('./VGGish/Data_PredScores/Categoty1/',save_filename),"prediction","score")
    
end

How do I do it for files with no pattern? I tried to use the ls command:

filename = ls()

But it returns the entire list of file names as a string, without separation.

Pranav
  • 3
  • 4
  • 2
    Have you read the documentation for [`dir`](https://uk.mathworks.com/help/matlab/ref/dir.html)? – Wolfie Feb 04 '22 at 14:24
  • See the second part of the answer on the dupe. That's exactly your case. Use `dir()` as @Wolfie mentioned. Also note that doing `%03d` as a format specifier will zero-pad on the front, eliminating the need for your `if` statements. And one `if` can have multiple `elseif`, so you can even latch your three statements together into a single `if;elseif;elseif;end` – Adriaan Feb 04 '22 at 14:37
  • Hi, thanks for that suggestion. Adding to the question, is there a way to remove the extension from the name as filenames=dir() returns the full name in the name field? I want to save the data computed with the input file and save it as the filename, but doing so directly saves it with the original file extension and not .mat format. – Pranav Feb 07 '22 at 14:50

0 Answers0