Reading multiple .txt files and plotting a graph

16 visualizaciones (últimos 30 días)
Leandro
Leandro el 15 de Abr. de 2024
Comentada: Voss el 16 de Abr. de 2024
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 comentario
Rik
Rik el 15 de Abr. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (1)

Alexander
Alexander el 15 de Abr. de 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')

Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by