How should I import dataset separately from a long text file?

Hi,
I have a dataset like this (attached). Please let me know if there is a way to import all the "tables" individually as a separate dataset. Basically, the data loops start after the summary with a string "table x", then some parameters. After parameters the data starts with the variable names.
I also want to ignore the summary of the dataset at the beginning.
delimeter: tab

4 commentaires

hello
you need a dedicated parser for that , but a "real" data file would be more efficient to work with
all the best
Md Redwanul Islam
Md Redwanul Islam le 30 Mar 2022
Modifié(e) : Md Redwanul Islam le 30 Mar 2022
Thank you for your comment.
Here is an example real dataset. The first Table 1 is the summary.
hello again
to be sure, what you need is those tables ?
like
Time [s] V+ [V] V- [V] I1 [A] P1 [uC/cm2] I2 [A] P2 [uC/cm2] I3 [A] P3 [uC/cm2] D1 [nm] D2 [nm] D3 [nm] CH3 [V]
0.000000e+000 7.820238e-001 0.000000e+000 5.293780e-008 -4.931280e+001 5.293780e-008 -4.931280e+001 0.000000e+000 -2.672872e+001 0.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000
2.500000e-004 2.778735e+000 0.000000e+000 5.542453e-008 -4.917735e+001 5.542453e-008 -4.917735e+001 0.000000e+000 -2.672872e+001 -2.207564e-003 -2.207564e-003 0.000000e+000 0.000000e+000
5.000000e-004 4.777116e+000 0.000000e+000 6.000530e-008 -4.903306e+001 6.000530e-008 -4.903306e+001 0.000000e+000 -2.672872e+001 -1.466427e-002 -1.466427e-002 0.000000e+000 0.000000e+000
7.500000e-004 6.777614e+000 0.000000e+000 6.009513e-008 -4.888294e+001 6.009513e-008 -4.888294e+001 0.000000e+000 -2.672872e+001 -1.587351e-002 -1.587351e-002 0.000000e+000 0.000000e+000
1.000000e-003 8.768961e+000 0.000000e+000 5.680956e-008 -4.873681e+001 5.680956e-008 -4.873681e+001 0.000000e+000 -2.672872e+001 -2.400754e-002 -2.400754e-002 0.000000e+000 0.000000e+000
1.250000e-003 1.077756e+001 0.000000e+000 5.808965e-008 -4.859318e+001 5.808965e-008 -4.859318e+001 0.000000e+000 -2.672872e+001 -3.077039e-002 -3.077039e-002 0.000000e+000 0.000000e+000
1.500000e-003 1.276941e+001 0.000000e+000 5.580522e-008 -4.845081e+001 5.580522e-008 -4.845081e+001 0.000000e+000 -2.672872e+001 -3.635528e-002 -3.635528e-002 0.000000e+000 0.000000e+000
1.750000e-003 1.476892e+001 0.000000e+000 6.166729e-008 -4.830397e+001 6.166729e-008 -4.830397e+001 0.000000e+000 -2.672872e+001 -3.822271e-002 -3.822271e-002 0.000000e+000 0.000000e+000
2.000000e-003 1.676584e+001 0.000000e+000 5.885770e-008 -4.815332e+001 5.885770e-008 -4.815332e+001 0.000000e+000 -2.672872e+001 -4.513578e-002 -4.513578e-002 0.000000e+000 0.000000e+000
2.250000e-003 1.876169e+001 0.000000e+000 5.659342e-008 -4.800900e+001 5.659342e-008 -4.800900e+001 0.000000e+000 -2.672872e+001 -4.782756e-002 -4.782756e-002 0.000000e+000 0.000000e+000
Hi,
Yes. so, I have these separate tables of dataset in one large text file. I wanted to load these table dataset separately e.g. table-1, table-2, ..., table-n. Then it would be easier for me to plot them, like i can say plot column2 and column4 of table-n.

Connectez-vous pour commenter.

 Réponse acceptée

hello
the demo txt files has two tables so this is how the code generates the labels and data (as cell arrays)
label =
1×13 cell array
Columns 1 through 7
{'Time [s]'} {'→V+ [V]'} {'→V- [V]'} {'→I1 [A]'} {'→P1 [uC/cm2]'} {'→I2 [A]'} {'→P2 [uC/cm2]'}
Columns 8 through 13
{'→I3 [A]'} {'→P3 [uC/cm2]'} {'→D1 [nm]'} {'→D2 [nm]'} {'→D3 [nm]'} {'→CH3 [V]'}
data =
1×2 cell array
{10×13 double} {10×13 double}
code
fid = fopen('data.txt');
tline = fgetl(fid);
% init variables
n = 0;
flag =0;
while ischar(tline)
if findstr(tline,'Time [s]') % extract data only for lines below 'Time [s]'
headerline = tline;
n = n+1;
% disp(n) % debug only
% disp(tline) % debug only
flag =1;
k = 0;
end
tline = fgetl(fid);
if flag == 1
out = tline;
if length(out) <2
flag =0;
else
k = k+1;
data{n}(k,:) = str2num(out);
end
end
end
fclose(fid);
% labels
in2 = strfind(headerline,']');
label{1} = headerline(1:in2(1));
for ci = 2:length(in2)
label{ci} = headerline(in2(ci-1)+1:in2(ci));
end
% display in command window for info
label
data

2 commentaires

Hi,
Works perfectly! Thank you so much for the help.
Glad it matches your expectations !
do you mind accepting my answer ?
tx !

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by