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!
