How to extract specific data from a txt file
72 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Star Strider
am 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 Kommentare
Mayur
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu Text Data Preparation 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!