How to extract specific data from data files

9 Ansichten (letzte 30 Tage)
Dq Hu
Dq Hu am 24 Apr. 2018
Kommentiert: Dq Hu am 25 Apr. 2018
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.

Akzeptierte Antwort

Bob Thompson
Bob Thompson am 24 Apr. 2018
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.
  1 Kommentar
Dq Hu
Dq Hu am 25 Apr. 2018
Thank you very much Bob. It's good enough for me to start with.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Large Files and Big Data finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by