I know MATLAB doesn't have a good performance in for loop computation, and I know that if some variables' size increases at each iteration of the for loop, more and more memory space will be occupied. But in my case, my purpose is to do batch processing to several data files. At the begin of the each iteration, I will clear all the variables except the for loop index (see below), but I noticed that the program turned slower and slower. Can any explain what happened and help me fix it? Thanks!

The framework of the for loop is as follow:

clear;close all;clc;

% define data ID

WTNO = [15,3,1:2,4:14,16:24];

for i = length(WTNO)

    clearvars -except WTNO i

    load(num2str(WTNO(i)),'.mat');

    % data processing

    ...

    % data save

    ...

end

Similar questions and discussions