필터 지우기
필터 지우기

Removing certain lines in a text file by setting a restriction

조회 수: 1 (최근 30일)
jgillis16
jgillis16 2015년 6월 17일
댓글: Star Strider 2015년 6월 18일
I am trying to remove certain lines of text in a file by setting the restriction that the 15th column of the each line can only go up to the numerical value of '21'.
For example:
13|PGC000013|0.00370|33.13420|~|15.41|0.675|0.217|0.587|~|0.87|0.102|~|-18.94|72.722|10.908|0.40|0.41|
has a value of '72.722', which is more than the '21' cutoff threshold so it would be eliminated.
My file is attached.

채택된 답변

Star Strider
Star Strider 2015년 6월 17일
편집: Star Strider 2015년 6월 17일
This works, and it’s relatively fast:
fidi = fopen('jgillis16 copiedlines.txt','rt');
Glxc = textscan(fidi, '%s', 'HeaderLines',1, 'Delimiter','|');
frewind(fidi)
Glxcs = textscan(fidi, '%s', 'EndOfLine','\r\n');
fclose(fidi);
dlen = 18*fix(length(Glxc{:})/18); % Set Row Length
Glxcr = reshape(Glxc{:}(1:dlen), 18, [])'; % Reshape & Transpose
Idx = cellfun(@(x) str2num(x) <= 21, Glxcr(:,15), 'Uni',0); % Find Rows With Col18 <= 21 To Retain & Write To New File
LIdx = logical(cell2mat(Idx)); % Logical Array From Cell
NewGlxc = Glxcs{:}(LIdx,:); % Rows Of New Array
You would then write the ‘NewGlxc’ array to your file. (I would save it as a .mat file if it is only for MATLAB use.)
EDIT — To write it to a text file:
fido = fopen('NewGalaxy.txt','wt');
fprintf(fido, '%s\n', NewGlxc{:});
fclose(fido);
  댓글 수: 8
jgillis16
jgillis16 2015년 6월 18일
I had to reload my .txt file as for some reason it wasn't showing an updated version. But, yes it works. Thanks for your continuous help!
Star Strider
Star Strider 2015년 6월 18일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 17일
fid=fopen('fic.txt')
l=fgetl(fid);
k=1;
while ischar(l)
r{k}=l;
k=k+1
l=fgetl(fid);
end
kk=0
for k=1:numel(r)
a=str2double(regexp(r{k},'-?\d+(\.\d+)?','match'));
if a(5)<21
kk=kk+1;
out{kk}=r{k};
k=k+1;
end
end
  댓글 수: 9
jgillis16
jgillis16 2015년 6월 18일
Overall, the code does not achieve what I want to do. It doesn't transpose lines that have a value <21 in the 15th column in another file.
Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 18일
No, they are saved in the file 'fic1.txt'

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by