How to extract data selection from multiple .txt files and plot one graph?
6 次查看(过去 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')
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
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
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)
plot(data.Zreal,-data.Zimag)
xlabel('Z_{real}(\Omega.cm^2)', 'FontSize', 12)
ylabel('-Z_{imag}(\Omega.cm^2)','FontSize', 12)
4 个评论
Cris LaPierre
2024-4-17
I mean do this
fullfile(fname(a).folder, fname(a).name)
instead of this
strcat(dir_files,'/', fname{a})
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!