shawpnik
shawpnik

Reputation: 111

Get all the values from the "for loop" run

I have some *.dat files in a folder, I would like to extract a particular column (8th column) from all of the files and put into a excel file. I have run a for loop, but it only gives me the results of final run (i.e. if there are 10 number of files, it only returns me 8th column of the 10th files).

data = cell(numel(files),1);
for i = 1:numel(files)
    fid = fopen(fullfile(pathToFolder,files(i).name), 'rt');
    H = textscan(fid, '%s', 4, 'Delimiter','\n'); 
    C = textscan(fid, repmat('%f ',1,48), 'Delimiter',' ', ... 
    'MultipleDelimsAsOne',true, 'CollectOutput',true); 
     fclose(fid);
     H = H = H{1}; C = C{1};
     data{i} = C;
     B = C(:,8);
end

Looking for your help on this.

It would be greatly appreciated.

Upvotes: 0

Views: 36

Answers (1)

David
David

Reputation: 8459

You are overwriting B each iteration. B(:,i) will put each column 8 of C in a column of B.

Upvotes: 1

Related Questions