Importing data from multiple text files and plot on one figure?

15 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Raj
Raj el 12 de Jun. de 2019
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
  3 comentarios
Raj
Raj el 12 de Jun. de 2019
Use this:
for ii=1:24 % Number of files
myfilename= sprintf('mass%i.txt',ii);%Assuming you have file names as mass1.txt,mass2.txt and so on.
filename = fullfile('D:\','STUDY','IIST Summer Internship May July 2019','Week 3','8th June','Files',myfilename);
fid = fopen(filename,'r');
Mydata = textscan(fid,'%f %f','HeaderLines',3); % Skip header lines
fid = fclose(fid);
Mydata=cell2mat(Mydata);
plot(Mydata(:,1),Mydata(:,2))
hold on
end
Raman Kaur
Raman Kaur el 25 de Jul. de 2023
Hi Raj
I am trying to do something similar !!
trying to load 'P11.txt', 'P21.txt', 'P31.txt', 'P41.txt' and 'P51.txt' in a loop and perform exactly same calculation on each file and finally plot the results on one graph (with same x axis). 5 files in total, x data is from column 2 and y data is from column 22.
I have modified your code , as shown below and get the error on highlighted line, could you please help?
for ii=1:5
myfilename=sprintf('P%i.txt',ii);
filename = fullfile('C:\','USER','Desktop','Results','monitor_points','Analysis','Probe_points_first_set',myfilename);
fid = fopen(filename, 'r');
Mydata = textscan(fid,'%f %f'); %there are no headers in my file,have been manually removed
fid = fclose(fid);
Mydata=cell2mat(Mydata);
plot(Mydata(:,2),Mydata(:,22))
hold on
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Text Data Preparation en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by