Reading multiple .txt files and plotting a graph
이전 댓글 표시
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.
채택된 답변
추가 답변 (1개)
Alexander
2024년 4월 15일
1 개 추천
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')
카테고리
도움말 센터 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
