How to extract data selection from multiple .txt files and plot one graph?

9 次查看(过去 30 天)
Hello everyone,
I have multiple .txt files like the one (please, check the .txt file enclosed). The data of interest is these two specific columns "Zreal" and "Zimag" (it can be seen in the image below):
I've created to scripts. One named 'read_gamry_data' and the other 'nyquist_gamry'.
Here is the code 'read_gamry_data'
% EDIT: adding function call so errors are displayed here (CL)
data = read_gamry_data('Sample Potentiostatic EIS.txt')
data = 0x1 empty cell array
function [data] = read_gamry_data(fname)
%READ_GAMRY_DATA Gets the data matrices in the file "fname" and
%stores them into the cell "data"
%get all lines of the file
stra = {};
fid=fopen(fname,'r');
sl = fgetl(fid);
while ischar(sl)
stra{end+1,1}=strtrim(sl);
sl = fgetl(fid);
end
fclose(fid);
%get the EIS data from the gamry .txt files
strid_dataset = 'ZCURVE';
id_start = [];
data = cell(length(id_start),1);
for y = 1:length(id_start)
for z = id_start(y):id_end(y)
if strcmp(strid_dataset,stra{z}(1:min(length(stra{z}),length(strid_dataset))))
%read data matrix
rstart = z+3;
rend = rstart;
while rend<=id_end(y) && length(split(stra{rend}))>1
rend = rend+1;
end
rend = rend-1;
break;
end
end
data{y}=zeros(rend-rstart+1,length(split(stra{rstart})));
for z = rstart:rend
data{y}(z-rstart+1,:)=(sscanf(stra{z},'%f'))';
end
end
end % added this CL
and here is the code of 'nyquist_gamry' script. Before just plotting, I need to correct the data by the area, so I tried:
clear all;close all;
dir_files = 'EIS'
area = (pi/4)*1.5^2
fname = dir(fullfile(dir_files,'*.txt'));
for a = 1:length(fname)
data{a} = read_gamry_data(strcat(dir_files,'/', fname{a}));
data{a}{1}(:,1:2) = area.*data{a}{1}(:,1:2);
plotgraph(1,data{a}{1}(:,1),-data{a}{1}(:,2))
hold on
end
xlabel('Z_{real}(\Omega.cm^2)', 'FontSize', 12)
ylabel('-Z_{imag}(\Omega.cm^2)','FontSize', 12)
set(gca,"dataaspectratio",[1,1,1])
I constantly having errors, especially saying that my index is not supported. Can anyone try to run it and see if you can help me with? I would like to hear any suggestion from you all.
Kindly
Leandro
  1 个评论
Cris LaPierre
Cris LaPierre 2024-4-16
编辑:Cris LaPierre 2024-4-16
I edited your code so that it could be run here. The sample file does not generate any errors, but the final result is also empty, so yout code is not properly loading the data..
Please share the full error message (all the red text).

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2024-4-16
The indexing error suggests something is wrong with how your code handles multiple files. As for loading the data, if your files all share the exact same formatting, you can just use readtable.
data = readtable('Sample Potentiostatic EIS.txt','numHeaderLines',96)
data = 81x11 table
Pt Time Freq Zreal Zimag Zsig Zmod Zphz Idc Vdc IERange __ ____ __________ ______ ________ ____ ______ _________ ___________ _________ _______ 0 7 1.0001e+06 198.38 -0.30052 1 198.38 -0.086796 9.714e-07 -0.000675 9 1 8 7.9439e+05 199.27 0.44131 1 199.27 0.12689 -5.0535e-07 -0.000702 9 2 10 6.3098e+05 199.49 0.85197 1 199.5 0.24469 1.8085e-06 -0.000656 9 3 11 5.0123e+05 199.52 0.8388 1 199.52 0.24088 7.9994e-07 -0.000629 9 4 12 3.9811e+05 199.5 0.64289 1 199.5 0.18463 -3.8204e-07 -0.00063 9 5 14 3.1627e+05 199.5 0.34671 1 199.5 0.099577 2.9806e-07 -0.000603 9 6 15 2.512e+05 199.7 0.097624 1 199.7 0.028009 4.0072e-06 -0.000576 9 7 16 1.9955e+05 199.63 -0.17299 1 199.63 -0.049651 1.8076e-07 -0.000565 9 8 17 1.5842e+05 199.52 -0.57837 1 199.52 -0.16609 5.2998e-07 -0.00053 9 9 19 1.2595e+05 199.43 -0.93185 1 199.43 -0.26771 9.6905e-07 -0.000514 9 10 20 99978 199.52 -1.0675 1 199.53 -0.30655 4.8914e-07 -0.00048 9 11 21 79453 199.44 -1.707 1 199.44 -0.49038 1.132e-06 -0.000485 9 12 23 63078 199.49 -2.2827 1 199.51 -0.65557 4.9528e-07 -0.000492 9 13 24 50103 199.51 -3.1223 1 199.53 -0.89659 4.9509e-07 -0.000495 9 14 25 39811 199.48 -3.9843 1 199.52 -1.1442 5.1864e-07 -0.000489 9 15 27 31609 199.51 -5.0752 1 199.57 -1.4572 5.1111e-07 -0.000495 9
plot(data.Zreal,-data.Zimag)
xlabel('Z_{real}(\Omega.cm^2)', 'FontSize', 12)
ylabel('-Z_{imag}(\Omega.cm^2)','FontSize', 12)
  4 个评论
Leandro
Leandro 2024-4-17
Now it is working! Thanks for your time.
Do you mind if I ask how would you update this specifically? I couldnt get what you said here: "update the input to the read function to use fullfile based on the results of dir"
It would be helpful if I can separate it in folders per batch of samples.
Kindly

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by