简体   繁体   中英

Matlab data import from .dat files

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). Ie 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM