Clear Filters
Clear Filters

Reading multiple .txt files and plotting a graph

8 views (last 30 days)
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 Comment
Rik
Rik on 15 Apr 2024
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.

Sign in to comment.

Accepted Answer

Voss
Voss on 15 Apr 2024
% 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 Comments
Leandro
Leandro on 16 Apr 2024
Hello Voss,
Thank you very much for your answer.
Can we take off the .txt file extension from the legend?
Voss
Voss on 16 Apr 2024
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')

Sign in to comment.

More Answers (1)

Alexander
Alexander on 15 Apr 2024
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')

Categories

Find more on Labels and Annotations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!