How to extract specific data from data files
조회 수: 10 (최근 30일)
이전 댓글 표시
There are multiple data files and I attached two as examples. For each of the data file, there are two data blocks. I want to extract the line corresponding to F=1.0000 in each data block, and combine the two lines into one. Then create a new file which includes the extracted data for all the data files.
For file "test1", the two lines corresponding to F=1.0000 are
- 1.0000 0.98581 -23.906 5.74804 164.322
- 1.0000 0.0674 0.84738 9.741 0.2434
The extracted data would be
- 1.0000 0.98581 -23.906 5.74804 164.322 0.0674 0.84738 9.741 0.2434
Similarly, the extracted data for file "test2" would be
- 1.0000 0.98373 -23.184 5.52288 164.574 0.0690 0.84402 12.860 0.2598
The new file I want to obtain will include the extracted data from "test1" and "test2"
- 1.0000 0.98581 -23.906 5.74804 164.322 0.0674 0.84738 9.741 0.2434
- 1.0000 0.98373 -23.184 5.52288 164.574 0.0690 0.84402 12.860 0.2598
What's a good way to realize this? Thank you.
댓글 수: 0
채택된 답변
Bob Thompson
2018년 4월 24일
I'm fairly certain there is a better way of doing this, but I personally would scan through each document with a for loop, and then use fgetl to extract the specific lines.
for nf = 1:length(files); % Loop through all files
count = 0; % Reset line counter
line = 1; % Reset line value
a = 1;
file = fopen(files(nf)); % Open new file
while line ~= -1; % Run to end of file
line = fgetl(file); % Read next line
if length(line)<5; % Ignore small lines
elseif strcmp(line(1:6)),'1.0000')==1; % Look for lines beginning with desire value
datahold(a,:) = num2str(line); % You may have issues with this line converting correctly, but this should give
% the concept of what you want to accomplish
a = a+1; % Add new line to array for new data to recover
end % Line value check
count = count+1; % Advance line count
end % while loop
extracted(nf,:) = [datahold(1,:) datahold(2,2:end)]; % Only accounts for two lines extracted, transforms to one line
end % File loop
It's far from perfect, but it should get you started.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!