0
votes

I have about 200 .dat files each of which has 8 rows and 20 columns. All files are exactly the same size and their names follow the order of: Gizmo002, Gizmo004, Gizmo006 etc.

I'd like to extract the value for row2 and column4 from every single file and put the extracted value in a new array/file/matrix (the SAME one). I.e. I'd like to have just 1 file that contains all the row2 and column4 values.

What is the simplest way of doing it?

Please post the code - my Matlab skills are quite limited.

Thank you.

1

1 Answers

0
votes

You can use the below script. At first, it finds all existing '.dat' files, then retrieves the desired element from each file.

close all
clear
clc

data_path = 'Data\';
files = dir( strcat(data_path,'*.dat') );

data = zeros(length(files),1);
for i = 1:length(files)
    content = load(strcat(data_path,files(i).name));
    data(i) = content(2,4);
end

save 'output.dat' data -ascii