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

조회 수: 9 (최근 30일)
Satishyadav Busa
Satishyadav Busa 2019년 6월 12일
댓글: Raman Kaur 2023년 7월 25일
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

채택된 답변

Raj
Raj 2019년 6월 12일
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
Raj
Raj 2019년 6월 12일
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 2023년 7월 25일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by