Importing text file and reading certain values with key terms
이전 댓글 표시
Hello, I am trying to import this text file into matlab but only reading certain values. Ignoring the uncollided data, I want to be able to retrieve the location of each detector (x,y and z) , (z = 0 for all detectors). Also, I want to be able to retrieve the "total" value which can be found at the end of each collided data. I know this data that is being imported is very hard to convert to the values I need but I would appreciate any help. Thank you in advance.
댓글 수: 8
John Vargas
2018년 9월 18일
John Vargas
2018년 9월 18일
Do you want the coordinates x,y,z in three matrices? Easy for total, maybe not very pretty:
raw = fileread('D1output1.txt');
[~,tok] = regexp(raw,'detector[^\n]*\n\s(?!\buncollided\b)[\s\S]*?total\s+(\d+\.\d+E[-+]\d+)','match','tokens');
data=str2double([tok{:}])
or easier:
raw = fileread('D1output1.txt');
[~,tok] = regexp(raw,'total\s+(\d+\.\d+E[-+]\d+)','match','tokens');
data=str2double([tok{:}]);
as, from looking at the text file, there is no "total" section for uncollided data.
John Vargas
2018년 9월 18일
Paolo
2018년 9월 18일
The coordinates being for example
x,y,z = 3.97500E+03 3.97500E+03-9.95000E+01
right?
Paolo
2018년 9월 18일
If that is the case, try:
raw = fileread('D1output1.txt');
[~,tok] = regexp(raw,'x,y,z = (\d+\.\d+E\+\d+) (\d+\.\d+E\+\d+)(-\d+\.\d+E\+\d+)\s*\n\s(?!uncollided)','match','tokens');
data=[tok{:}];
x=str2double(data(1:3:end));
y=str2double(data(2:3:end));
z=str2double(data(3:3:end));
John Vargas
2018년 9월 21일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!