Reading multiple .txt files and plotting a graph
조회 수: 18 (최근 30일)
이전 댓글 표시
Hello all,
First of all, I am a beginner.
My files are all in one folder and they look like this (screen1). I just need to plot the columns T (Time (s) ) vs Vf (Potential (V vs Vref)) and just one chart for all files. I would appreciate if I could be able to label my samples (S1, S2 and etc.) to differentiate each curve.
Could someone help me with?
Thank you all in advance.
댓글 수: 1
Rik
2024년 4월 15일
Have a read here and here. It will greatly improve your chances of getting an answer. If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks).
What did you try? What went wrong? Try to make a MWE so we can run your code without any other dependencies and can reproduce your issue. The best way to do this is to use the code section in the editor and use the run button. That way you can make sure we will see the same output as you do.
채택된 답변
Voss
2024년 4월 15일
% folder where your txt files are
your_folder = '.';
% get info about all .txt files in your_folder (use 'S*.txt' instead
% of '*.txt' if you want info about .txt files whose name starts with 'S')
F = dir(fullfile(your_folder,'*.txt'));
% read each file
for ii = 1:numel(F)
F(ii).Data = readtable(fullfile(F(ii).folder,F(ii).name),'DecimalSeparator',',');
end
% plot each file
figure
hold on
for ii = 1:numel(F)
plot(F(ii).Data.T,F(ii).Data.Vf)
end
% label each curve
legend({F.name},'Location','best')
댓글 수: 2
Voss
2024년 4월 16일
You're welcome!
Yes, you can use the filename without extension in the legend:
% label each curve
[~,C] = fileparts({F.name});
legend(C,'Location','best')
추가 답변 (1개)
Alexander
2024년 4월 15일
My ancient approach:
clear;
fid = fopen('S1.txt','r');
fgetl(fid);fgetl(fid);
Titel = fgetl(fid)
for(ii=1:44)
dyTxt = fgetl(fid);
end
ColumnHeader = fgetl(fid);
fgetl(fid);
ii=0;jj=1;
while(~feof(fid))
ii = ii + 1;
dyTxt = fgetl(fid);
dyTxt = strrep(dyTxt,'.','');
dyTxt = strrep(dyTxt,',','.');
data = str2num(dyTxt);
T(ii,jj) = data(2);
Vf(ii,jj) = data(3);
end
fclose(fid);
figure(1);plot(T(:,jj),Vf(:,jj));grid minor;
xlabel('Time [s]'); ylabel('Vf [V]'); title(Titel);
% The second curve
fid = fopen('S2.txt','r');
fgetl(fid);fgetl(fid);
Titel = fgetl(fid)
for(ii=1:44)
dyTxt = fgetl(fid);
end
ColumnHeader = fgetl(fid);
fgetl(fid);
ii=0;jj=2;
while(~feof(fid))
ii = ii + 1;
dyTxt = fgetl(fid);
dyTxt = strrep(dyTxt,'.','');
dyTxt = strrep(dyTxt,',','.');
data = str2num(dyTxt);
T(ii,jj) = data(2);
Vf(ii,jj) = data(3);
end
fclose(fid);
figure(2);plot(T(:,jj),Vf(:,jj));grid minor;
xlabel('Time [s]'); ylabel('Vf [v]'); title(Titel);
figure(3);plot(T(:,1),Vf(:,1),T(:,2),Vf(:,2));grid minor;
xlabel('Time [s]'); ylabel('Vf [V]'); title('V1, V2');
legend('V1','V2')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!