Extract data between specific indicators from txt file
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a demo1.txt file that looks something like this
R1 0.222 0.444 0.555
0.777 0.999
NR8 0.001 0.002 0.003
0.004 0.008
NR1 0.004 0.009 0.008
0.006 0.009
I would like to only extract the numbers in the R1 section (in this case, search for the exact match as the string "R1", grab all numbers after "R1" up until NaN), then create an excel spreadsheet named "demo1" and have the following entries in a column:
R1
0.222
0.444
0.555
0.777
0.999
Any hint will be appreciated! Thank you very much!
3 Kommentare
Antworten (1)
Mathieu NOE
am 30 Apr. 2021
hello
this is one solution - hope it helps !
tested with the attached txt file
= readlines('data.txt');
data_str = 'R1';
%% main loop
out_total = [];
mm = (strfind(ll,data_str));
for ci = 1:numel(mm)
if cell2mat(mm(ci)) > 0
out = str2double(split(ll(ci)));
out(isnan(out)) = [];
out_total = [out_total; out(:)];
end
end
% write to excel a table
t = array2table(out_total,'VariableNames',{data_str});
writetable(t,'output.xlsx');
5 Kommentare
Mathieu NOE
am 30 Apr. 2021
ok , ths should work for you
ll = fileread('data.txt');
data_str = 'R1';
%% main loop
out_total = [];
mm = (strfind(ll,data_str));
rr = strfind(ll,' ');
for ci = 1:numel(mm)
ind1 = mm(ci);
ind2 = rr(ci);
out = str2double(split(ll(ind1:ind2)));
out(isnan(out)) = [];
out_total = [out_total; out(:)];
end
% write to excel a table
t = array2table(out_total,'VariableNames',{data_str});
writetable(t,'output2.xlsx');
Siehe auch
Kategorien
Mehr zu Spreadsheets finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!