I have 200 files with 20 columns each. The 1st column remains the same for all files. I want to sum up all 2nd columns from 200 files, .....20th columns from 200 files. I want the first column to remain the same.

1 view (last 30 days)
Gollapalli Prince
Gollapalli Prince on 15 Feb 2019
Answered: StefBu on 15 Feb 2019
file 1:
1 2 3 4
2 2 3 2
file 2:
1 3 4 1
2 1 3 2
output should be:
1 5 7 5
2 3 6 4

Answers (1)

StefBu
StefBu on 15 Feb 2019
Hi you have to use a for-loop to access your data and then add up the colums.
First use dir to get your files
files = dir('C:\users\...');
then create your output
Output = [];
then loop through your data, import them (that of course depends on the type of data):
Be sure to use double as datatype or it wont run.
for iFile = 1:size(files,1)
tempFile = importdata(fullfile(files(iFile).folder, files(iFile).name)); % get data
if iFile = 1
Output = tempFile; % write data for first run through the loop
else
tempFile(:,1) = 0; % make first column zeros so it doesnt change on adding up
Output = Output + tempFile; % add up data
end
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by