Removing specific lines from a .txt file.

조회 수: 2 (최근 30일)
Justin Rosenthal
Justin Rosenthal 2022년 5월 9일
답변: Mitch Lautigar 2022년 5월 9일
I have a .txt file that I have imported into MATLAB and I need to remove some specific lines. Currently, I have simplified it to what I have shown below:
Trace = 'D:\trace.txt';
%Simplify trace file into time and status matrix
str = fileread(Trace);
trace = regexp(str, '^(\d+\.?\d*):[^:]+:\s+([^\n\r,]+)','tokens','lineanchors');
trace = vertcat(trace{:});
%Simplify trace file to display only desired keywords
trace(strcmp(trace(:,2), 'init-hold'),:) = [];
trace(strcmp(trace(:,2), 'start'),:) = [];
trace(strcmp(trace(:,2), 'vent-check'),:) = [];
trace(strcmp(trace(:,2), 'flush'),:) = [];
trace(strcmp(trace(:,2), 'flush-evac'),:) = [];
trace(strcmp(trace(:,2), 'flush-vent'),:) = [];
trace(strcmp(trace(:,2), 'init-evac'),:) = [];
trace(strcmp(trace(:,2), 'leak-test-end'),:) = [];
trace(strcmp(trace(:,2), 'pre-leak-test'),:) = [];
trace(strcmp(trace(:,2), 'leak-test'),:) = [];
trace(strcmp(trace(:,2), 'pre-ramp'),:) = [];
trace(strcmp(trace(:,2), 'ramp'),:) = [];
trace(strcmp(trace(:,2), 'evac'),:) = [];
trace(strcmp(trace(:,2), 'vent'),:) = [];
trace(strcmp(trace(:,2), 'done'),:) = [];
trace(strcmp(trace(:,2), 'end-vent'),:) = []
Where I end up with the following result:
{'14667.4' } {'hold' }
{'18267.4' } {'hold-end' }
{'18456.4' } {'hold' }
{'22056.4' } {'hold-end' }
{'22245.6' } {'hold' }
{'25845.6000001'} {'max-pressure-evac 60.0'}
{'26034.6000001'} {'max-pressure-evac 60.0'}
{'29634.6000001'} {'hold-end' }
{'29823.6000001'} {'hold' }
{'31332.4000001'} {'hold-end' }
{'32785.4000001'} {'hold' }
{'33423.6000001'} {'hold-end' }
I want my code to be able to recognize when a 'max-pressure-evac 60.0' follows a 'hold' and then to remove only the lines between that initial 'hold' and the next 'hold-end'. For the above example I would want the following output:
{'14667.4' } {'hold' }
{'18267.4' } {'hold-end' }
{'18456.4' } {'hold' }
{'22056.4' } {'hold-end' }
{'29823.6000001'} {'hold' }
{'31332.4000001'} {'hold-end' }
{'32785.4000001'} {'hold' }
{'33423.6000001'} {'hold-end' }
Where the four lines corresponding to the 22245.6-29634.6000001 timestamps have been removed but everything else was kept. Any help would be greatly appreciated! I have attached an example .txt file for reference.

답변 (1개)

Mitch Lautigar
Mitch Lautigar 2022년 5월 9일
As you read this in, you really just need to add in a conditional flag. Specifically, if a "hold" is seen, set a hold_flag to be true (you can use setappdata/getappdata to move flags around like a global variable without it actually being a global value) you do not record any lines into your new array. If the hold_flag is false, then you record normally.

카테고리

Help CenterFile Exchange에서 Detection, Range and Doppler Estimation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by