# MATLAB: Indexing a loop to run through multiple files

for loopindexingMATLAB

I have experimental data in multiple csv files.
Ive written the code I want and checked this works by only inputting 1 specific file, but now I am trying to write in a loop to do the same for all the files. I am struggling with the indexing to get this to work, could anybody help?
%% input csv filesfile = dir('*.csv'); %read the files into matlabnum_files = length(file); %record how many files have been found[~, index] = natsort({file.name}); %sort the files into orderfilelist = file(index);%% Calculate granular temperature for a = 1:length(num_files)%read the table into matlabtable = table2array(readtable(filelist(a).name));%save the particle ID's and velocity componentsT = table(:, [1:4]);%calculate resultant velocity for each particle (sqrt of the velcoities squared)ResV = sqrt((T(:,2).^2) + (T(:,3).^2) + (T(:,4).^2));%calculate mean resultant velocity of all the particlesResVav = mean(ResV)%calculate the average granular temperature GTav = mean(ResV-ResVav)end 

%% input csv filesfile = dir('*.csv'); %read the files into matlabnum_files = length(file); %record how many files have been foundsorted = sort({file.name});for a = 1:length(num_files)%read the table into matlabtable = table2array(readtable(sorted{a}));%save the particle ID's and velocity componentsT = table(:, [1:4]);%calculate resultant velocity for each particle (sqrt of the velcoities squared)ResV = sqrt((T(:,2).^2) + (T(:,3).^2) + (T(:,4).^2));%calculate mean resultant velocity of all the particlesResVav = mean(ResV)%calculate the average granular temperature GTav = mean(ResV-ResVav)end