'How can I make a saving code faster? -MatLab

I'm running a short code to open one by one a list of files and saving back only one of the variables contained in the files. The process seems to me much slower than I expected and getting slower with time, I don't fully understand why and how I could make it run faster. I always struggle with optimization. I'd appreciate if you have suggestions.

The code is the following (the ... substitute the actual path just for example):

main_dir=dir(strcat('\\storage2-...\Raw\DAQ5\'));

filename={};
for m=7:size(main_dir,1)
    m
    second_dir=dir([main_dir(m).folder '\' main_dir(m).name '\*.mat']);
    for mm=1:numel(second_dir)
        filename{end+1}=[second_dir(mm).folder '\' second_dir(mm).name];
        for mmm=1:numel(filename)
            namefile=sprintf(second_dir(mm,1).name);
            load(string(filename(1,mmm)));
            save(['\\storage2-...\DAQ5\Ch1_',namefile(end-18:end-4),'.mat'], 'Ch_1_y')
        end
    end
end 

The original file is about 17 MB and once the single variable is saved it is about 6 MB in size.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source