MATLAB: Importing data from multiple text files and plot on one figure

MATLABtext file

Hello,
I want to import some 24 files of same X axis i.e time step or the variable of X axis is Same in all 24 files and Y axis is different in all 24 files.All of these are located in this directory D:\STUDY\IIST Summer Internship May July 2019\Week 3\8th June\Files I want to import all these files in a sequence and plot them in sequence on 1 figure.
Thankyou in advance,
Satish

Best Answer

Since you have not shared a sample text file, I assumed a few things. You can tweak this code a bit as per your files and that should work.
for ii=1:24 % Read one file at a time
myfilename= sprintf('%f.txt',ii);% Assuming you have file names as 1.txt,2.txt and so on.
filename = fullfile('D:\','STUDY','IIST Summer Internship May July 2019','Week 3','8th June','Files',myfilename);
fid = fopen(filename, 'r'); % Open the file
Mydata= fscanf(fid, '%f'); % Read the file assuming there are no text headers
fid = fclose(fid); % CLose the file
plot(Mydata(:,1),Mydata(:,2)) % Plot the data
hold on % Hold the plot
end % End and repeat for all files