How can I find the line after the line containing the text I am looking for?

6 visualizaciones (últimos 30 días)
Hello! The text I have looks something like what is shown below
So far I am able to find the number on every line with "1 x" but I also want to be able to find what "y" is and "z" is on the two lines below every instance of "1 x". I tried to just shift the position more as shown in the code I have (which is linked here as well) but that didn't work. Would really appreciate any help!
0 x: 0.41513773798942566
y: 0.11939018964767456
z: -0.9948439598083496
1 x: 0.4298868179321289
y: 0.0953717827796936
z: -0.9504631757736206
2 x: 0.439603328704834
y: 0.0969197154045105
z: -0.9506538510322571
0 x: 0.4152446985244751
y: 0.11244936287403107
z: -0.8208140730857849
1 x: 0.4303297996520996
y: 0.09078289568424225
z: -0.7857186794281006
2 x: 0.44101160764694214
y: 0.09236695617437363
z: -0.7859166264533997
etc
Would like my result to be:
0.4298868179321289
0.0953717827796936
-0.9504631757736206
0.4303297996520996
0.09078289568424225
-0.7857186794281006

Respuesta aceptada

Rik
Rik el 24 de Feb. de 2022
Editada: Rik el 24 de Feb. de 2022
There are two general ways to this:
  1. Read the data all at once, then do your analysis on the full file, then write the complete result.
  2. Read the data line by line, analyzing as you go, writing as needed.
Option 1 is generally recommended, as it will prevent errors during processing to lock down files. It is generally easier to keep everything ordered and structured.
Option 2 will work for any size file, even if it doesn't fit in memory.
% store the link to the data you posted:
URL='https://www.mathworks.com/matlabcentral/answers/uploaded_files/906200/matlabdata.txt';
Option 1:
%If you don't have R2020b or newer, use readfile:
%https://www.mathworks.com/matlabcentral/fileexchange/68780
data=cellstr(readlines(URL));
%remove empty lines
data(cellfun('prodofsize',data)<3)=[]; % the legacy syntax for cellfun is actually faster than a loop
L=false(size(data));
for n=1:numel(data)
%find '1 x'
L(n)=strcmp(data{n}(1:3),'1 x');
%keep only the number
data{n}(1:max(strfind(data{n},' ')))='';
end
ind=find(L);
%confirm the file is complete
if (max(ind)+2)>numel(data),error('file incomplete'),end
%write the output file
fid=fopen('output.txt','w');
for n=1:numel(ind)
fprintf(fid,'%s %s %s\n',data{ind(n)+(0:2)});
end
fclose(fid);
Option 2:
fid_data=fopen(websave('metlabdata.txt',URL),'r');
fid_output=fopen('output_2.txt','w');
data=cell(1,3);
while ~feof(fid_data) %there are still lines to be read
line=fgetl(fid_data);
if strcmp(line(1:(min(end,3))),'1 x')
% found a line to be written
data{1}=line;
data{2}=fgetl(fid_data);
data{3}=fgetl(fid_data);
if any(cellfun('isclass',data,'double'))
%fgetl returns -1 on error
fclose(fid_data);fclose(fid_output);
error('file incomplete')
end
for n=1:3
%keep only the number
data{n}(1:max(strfind(data{n},' ')))='';
end
%write line
fprintf(fid_output,'%s %s %s\n',data{:});
end
end
fclose(fid_data);fclose(fid_output);
Now we confirm the two results are the same:
type('output.txt')
0.4298868179321289 0.0953717827796936 -0.9504631757736206 0.4303297996520996 0.09078289568424225 -0.7857186794281006
type('output_2.txt')
0.4298868179321289 0.0953717827796936 -0.9504631757736206 0.4303297996520996 0.09078289568424225 -0.7857186794281006

Más respuestas (1)

Arif Hoq
Arif Hoq el 24 de Feb. de 2022
try this. not a function rather script
A=readtable('Book5.xlsx', 'ReadVariableNames', false);
B=table2cell(A);
r = strtok(B, ':')
R=[r B];
C=cell(18,1);
for i=1:18
if isequal(R(i,1),{'1 x'})
C{i}=[R(i+1,2) R(i+2,2)];
if i==18
break
end
end
end
matrix=[C{:}]';
  9 comentarios
Arif Hoq
Arif Hoq el 25 de Feb. de 2022
as the file length is limited to 18, hence i choosed 18. yes it would have been
N=size(A,1);
C=cell(N,1);
for i=1:N
Allen, you said it does not work for ~33,000 lines, but your attched file consists of 18 lines only.. you can try written code above as a alternative way.
Arif Hoq
Arif Hoq el 25 de Feb. de 2022
A=readtable('Book5.xlsx', 'ReadVariableNames', false);
B=table2cell(A)
% names = split(B,'delimiter',':')
r = strtok(B, ':')
R=[r B];
N=size(A,1);
C=cell(N,1);
for i=1:N
if isequal(R(i,1),{'1 x'})
C{i}=[R(i,2);R(i+1,2);R(i+2,2)];
if i==18
break
end
end
end
matrix=[C{:}]' % if you want with x, y , z value
[token remain]=strtok(matrix,':')
output=strrep(remain,': ','') % if you want without x, y , z value

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by