Read .txt file into a matrix and remove unwanted text

조회 수: 8 (최근 30일)
Justin Rosenthal
Justin Rosenthal 2022년 1월 5일
댓글: Justin Rosenthal 2022년 1월 5일
Hello,
I have a .txt file that I want to simplify into a matrix. I want to remove the first 3 lines completely, keep the numbers in the first row without the decimal places or colon (i.e. 6248.2: should become 6248) and I want to keep the key words in the last column such as flush-evac, flush, etc. I would also like the remove the line in the middle of the file that starts with a #. I'm pretty sure that textscan can accomplish this task but I am struggling with acquiring the correct format. Any help would be greatly appreciated.

채택된 답변

Stephen23
Stephen23 2022년 1월 5일
편집: Stephen23 2022년 1월 5일
Simpler:
str = fileread('N2_trace.txt');
tkn = regexp(str,'^(\d+)\.?\d*:[^:]+:\s+([^\n]+)','tokens','lineanchors');
tkn = vertcat(tkn{:})
tkn = 39×2 cell array
{'0' } {'start' } {'0' } {'vent-check' } {'5' } {'flush-evac' } {'70' } {'flush' } {'84' } {'flush-vent' } {'147' } {'init-evac' } {'538' } {'pre-leak-test'} {'548' } {'leak-test' } {'5948' } {'leak-test-end'} {'5948' } {'evac' } {'6248' } {'pre-ramp' } {'6308' } {'ramp' } {'6344' } {'hold' } {'71144' } {'hold-end' } {'71144' } {'evac' } {'71444' } {'pre-ramp' } {'71504' } {'ramp' } {'71536' } {'hold' } {'136336'} {'hold-end' } {'136336'} {'evac' } {'136637'} {'pre-ramp' } {'136697'} {'ramp' } {'136729'} {'hold' } {'201529'} {'hold-end' } {'201529'} {'evac' } {'201829'} {'pre-ramp' } {'201889'} {'ramp' } {'201921'} {'hold' } {'266721'} {'hold-end' } {'266721'} {'evac' }
vec = str2double(tkn(:,1))
vec = 39×1
0 0 5 70 84 147 538 548 5948 5948
  댓글 수: 1
Justin Rosenthal
Justin Rosenthal 2022년 1월 5일
This is perfect and exactly what I was trying to accomplish. Thank you so much for your help Stephen!

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

추가 답변 (1개)

Voss
Voss 2022년 1월 5일
fid = fopen('N2_trace.txt');
data = fread(fid);
fclose(fid);
c_data = strsplit(char(data).',newline());
if isempty(c_data{end})
c_data(end) = [];
end
c_data(1:3) = [];
time = cellfun(@(x)str2double(x(1:find(x == '.',1)-1)),c_data);
status = cellfun(@(x)x(find(x == ':',1,'last')+2:end),c_data,'UniformOutput',false);
disp(c_data);
Columns 1 through 9 {'5.0: step #1.1:…'} {'70.0: step #1.1…'} {'84.6: step #1.1…'} {'147.2: step #1.…'} {'538.0: step #1.…'} {'548.0: step #1.…'} {'5948.0: step #1…'} {'5948.0: step #1…'} {'6248.2: step #1…'} Columns 10 through 18 {'6308.2: step #1…'} {'6344.0: step #1…'} {'# Edit Batch Pa…'} {'71144.0: step #…'} {'71144.0: step #…'} {'71444.2: step #…'} {'71504.2: step #…'} {'71536.8: step #…'} {'136336.8: step …'} Columns 19 through 27 {'136336.8: step …'} {'136637.0: step …'} {'136697.0: step …'} {'136729.2: step …'} {'201529.2: step …'} {'201529.2: step …'} {'201829.4: step …'} {'201889.4: step …'} {'201921.0: step …'} Columns 28 through 36 {'266721.0: step …'} {'266721.0: step …'} {'267021.2: step …'} {'267081.2: step …'} {'267112.6: step …'} {'331912.6: step …'} {'331912.6: post-…'} {'332001.0: post-…'} {'332098.8: post-…'} Columns 37 through 38 {'332113.4: post-…'} {'332176.0: post-…'}
disp(time);
Columns 1 through 18 5 70 84 147 538 548 5948 5948 6248 6308 6344 NaN 71144 71144 71444 71504 71536 136336 Columns 19 through 36 136336 136637 136697 136729 201529 201529 201829 201889 201921 266721 266721 267021 267081 267112 331912 331912 332001 332098 Columns 37 through 38 332113 332176
disp(status);
Columns 1 through 14 {'flush-evac'} {'flush'} {'flush-vent'} {'init-evac'} {'pre-leak-test'} {'leak-test'} {'leak-test-end'} {'evac'} {'pre-ramp'} {'ramp'} {'hold'} {'1.0}'} {'hold-end'} {'evac'} Columns 15 through 30 {'pre-ramp'} {'ramp'} {'hold'} {'hold-end'} {'evac'} {'pre-ramp'} {'ramp'} {'hold'} {'hold-end'} {'evac'} {'pre-ramp'} {'ramp'} {'hold'} {'hold-end'} {'evac'} {'pre-ramp'} Columns 31 through 38 {'ramp'} {'hold'} {'hold-end'} {'vent'} {'flush-evac'} {'flush'} {'flush-vent'} {'done'}

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by