Ok, managed to loop through all files by changing the start bit to
all_files=dir('*.txt');
for k=1:1:length(all_files);
filename=all_files(k).name;
fid=fopen(filename,'r');
C=textscan(fid,'%f %f %f','Headerlines',1);
fclose(fid);
I=C{1}; % Index
l=C{2}; % Length(mm)
s=C{3}; % Stress(MPa)
end
rest of code same as before
However one little problem remains which is for some reason it reuses some variables from previous loops because if I do the operation on a single file the results is different than when that same file is done after others in a loop. I tried putting clear before the final end command but seems to break the loop giving error ??? Undefined variable "all_files" or class "all_files" after the first file.
I know it is reusing data from previous files also because the plot of the graphs look odd and the same as when I manually ran some files without clearing variables so that is my problem for now.