How to extract specific data from a txt file
92 views (last 30 days)
Show older comments
Hi there!
I have a txt file that contains lines starting in either U4 or U6. I want to filter out all the lines with U4 so I'm left with only the ones that contain U6. How would I go about that?
Thanks in advance!
0 Comments
Answers (2)
Star Strider
on 23 Jun 2023
% type('Niskin-Logger-Sample.txt')
opts = detectImportOptions('Niskin-Logger-Sample.txt', 'Delimiter',{' '});
opts = setvaropts(opts, 'Var3', 'InputFormat','MM/dd/uuuu'); % Guessing The Date Format
T1 = readtable('Niskin-Logger-Sample.txt', 'Delimiter',{' '});
T2 = readtable('Niskin-Logger-Sample.txt', 'HeaderLines',50)
[UVar1,~,ix] = unique(T2.Var1);
Um = accumarray(ix, (1:numel(ix)).', [], @(x){T2(x,:)})
U6 = Um{2}
U6Var2NotNaN = U6(~isnan(U6.Var2),:)
NrVar2NaN = nnz(isnan(U6.Var2))
So ‘Var2’ (whatever it is) has only NaN values for the entire ‘U6’ table.
.
0 Comments
Mayur
on 23 Jun 2023
Hi Sydney!
I understand that you want to extract specific lines from a .txt file, here particularly lines starting with 'U6'. You can use the following code snippet:
fid = fopen('Niskin-Logger-Sample.txt', 'r');
data = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);
u6_lines = data{1}(startsWith(data{1}, 'U6'));
disp(u6_lines);
0 Comments
See Also
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!