Extract data between specific indicators from txt file

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 件のコメント

Mathieu NOE
Mathieu NOE 2021 年 4 月 30 日
soryy
I'm tired
this is now the good one
data_str = 'R1';
%% main loop
out_total = [];
nn = (strfind(ll,data_str));
out = str2double(split(ll(nn(1):nn(2))));
out(isnan(out)) = [];
% write to excel a table
t = array2table(out,'VariableNames',{data_str});
writetable(t,'output3.xlsx');
chlor thanks
chlor thanks 2021 年 4 月 30 日
My bad! This code only works on my previous txt file example.
But my actual txt file is more complicated like shown in the updated txt file.
And I only needed
R1
0.222
0.444
0.555
0.777
0.999
chlor thanks
chlor thanks 2021 年 5 月 2 日
Never mind I figured it out by myself, thanks for the inspiration and stepping in to help anyways!
clc;
clear;
Str = fileread('data.txt');
TStr = split(Str)
TStr_R1 = find(strcmp(TStr, 'R1'))
R1_end = str2double(TStr(TStr_R1:end))
R1_nan = find(isnan(R1_end));
R1 = R1_end((R1_nan(1)+1):(R1_nan(2)-1))

サインインしてコメントする。

回答 (1 件)

Mathieu NOE
Mathieu NOE 2021 年 4 月 30 日

0 投票

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 件のコメント

chlor thanks
chlor thanks 2021 年 4 月 30 日
Thank you!
But this did not work even after I try modifying it. All it was able to write is R1 itself:
clc;
clear;
content = fileread( 'data.txt' ) ;
R1 = 'R1';
%% main loop
out_total = [];
mm = (strfind(content,R1));
for ci = 1:numel(mm)
if mm(ci) > 0
out = str2double(split(content(ci)));
out(isnan(out)) = [];
out_total = [out_total; out(:)];
end
end
% write to excel a table
t = array2table(out_total,'VariableNames',{R1});
writetable(t,'output.xlsx');
Mathieu NOE
Mathieu NOE 2021 年 4 月 30 日
maybe I didn't paste correctly my code
see attached
chlor thanks
chlor thanks 2021 年 4 月 30 日
For example, readlines doesnt exist in my matlab
Mathieu NOE
Mathieu NOE 2021 年 4 月 30 日
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');
chlor thanks
chlor thanks 2021 年 4 月 30 日
Thank you very much for the updated code! So far looks like it extracted
R1
0.222
0.444
0.555
0.004
0.009
0.008
but I need
R1
0.222
0.444
0.555
0.777
0.999

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Import from MATLAB についてさらに検索

質問済み:

2021 年 4 月 29 日

コメント済み:

2021 年 5 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by