How to extract specific data from a txt file
92 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Star Strider
el 23 de Jun. de 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 comentarios
Mayur
el 23 de Jun. de 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 comentarios
Ver también
Categorías
Más información sobre Large Files and Big Data en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!