AnnaSchumann
AnnaSchumann

Reputation: 1271

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.

Upvotes: 0

Views: 261

Answers (1)

AnnaSchumann
AnnaSchumann

Reputation: 1271

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

Upvotes: 1

Related Questions