简体   繁体   中英

Batch import of data from .txt files - MATLAB

I have multiple .txt files each containing the same format.

I've been reading these into MATLAB individually using:

fid1 = fopen('Test_1.txt','r'); 
data = textscan(fid1, '%f %*f %f %f %f %*[^\r\n]','HeaderLines',4);

And using cell2mat to extract the values from there.

How can I batch import all .txt files and store them all?

So far I detect the relevant files to important using:

files = dir('Test_*.txt');

But I am not sure how to then batch import each .txt file in the above manner using a loop.

clear
data = {};
files = dir('Test_*.txt');
for i=1:length(files)
    fn = files(i).name;
    fid1 = fopen(fn,'r');
    data{1,i} = textscan(fid1, '%*f %*f %f %f %f %*[^\r\n]','HeaderLines',4);
end

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