Importing data from multiple text files and plot on one figure?
16 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
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
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 Center 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!