Matrix addition from multiple files

I have 240 matrices stored in 240 files with the name DOS1.dat DOS2.dat ......DOS240.dat.
The matrix size is 300x19. How to do a matrix addition.

 Accepted Answer

You haven't provided enough detail to answer your question too specifically but in general the steps would be to read the individual data files into MATLAB and assign the data in each file to a matlab array. You could then perform the desired matrix addition on the elements of this matlab array. So for example:
% assign your dimensions
numRows = 300 % number of rows in your individual matrices
numCols = 19 % number of columns in your individual matrices
numMats = 240 % number of individual matrices
% preallocate array to hold the data
A = zeros(numRows,numCols,numMats);
% loop to read individual matrices from files
for k = 1:numMats
% build the name of the data file as a character vector'
filename = ['DOS',num2str(k)];
A(:,:,k) = readmatrix(filename);
end
% now for example add the first and third matrix
B = A(:,:,1) + A(:,:,3)
You may have to modify the above code to fit the specifics of your situation, but this should give you an idea of how it could be done.

7 Comments

Hi Jonathan,
Thank you for your great help, but after running the script this is the error
Screenshot 2019-04-12 at 10.23.39 AM.png
Let me explain my question in detail so that you can help me better.
I have DOSx.dat files from DOS1.dat to DOS240.dat
Below is the screenshot (Shown partially )
Screenshot 2019-04-12 at 10.26.13 AM.png
DOS1.dat to DOS240.dat have same number of rows and columns.
I'm attaching DOS1.dat for overall idea. I cannot upload .dat file, hence I've changed the extension to .txt. Thank you. Please help me in matrix addition. DOS1.dat is the first matrix, DOS2.dat is second matrix and so on...
Jon
Jon on 12 Apr 2019
Edited: Jon on 12 Apr 2019
Hi Gollapalli,
The error you are getting is due to the fact that the MATLAB readmatrix function was just recently introduced, (I think in 2019A) so if you are running an older version of MATLAB you will not have this function available.
So either you can update your MATLAB, or use the older function csvread. In the latter case please use the following approach, and in particular note that on line 13 the filename now explicitly assigns the file extension .dat, and on line 15 the offending call to readmatrix is replaced by a call to csvread :
% assign your dimensions
numRows = 2; % number of rows in your individual matrices
numCols = 3; % number of columns in your individual matrices
numMats = 3; % number of individual matrices
% preallocate array to hold the data
A = zeros(numRows,numCols,numMats);
% loop to read individual matrices from files
for k = 1:numMats
% build the name of the data file as a character vector'
filename = ['DOS',num2str(k),'.dat'];
A(:,:,k) = csvread(filename);
end
% now for example add the first and third matrix
B = A(:,:,1) + A(:,:,3)
I've used online version of matlab 2019a: This is the error
Screenshot 2019-04-15 at 2.54.53 PM.png
This is the workspace
Screenshot 2019-04-15 at 2.54.27 PM.png
Matlab 2018a: offline version with your modified code (latest)
Screenshot 2019-04-15 at 3.01.46 PM.png
My matrix size is 301x19
Please help me also in looping through the sum:
matrix1+matrix2+matrix3+......matrix240
B=A(:,:,1)+A(:,:,2)+.......+A(:,:,240))
Hi Gollipalli,
I think that in both cases there seems to be a problem recognizing the delimiters in your file, which I now see are spaces, not commas. As a result, the full 301 x 19 matrix seems to be read incorrectly and thus does not have the correct size. I think there may be some kind of hidden characters in that file, maybe line ending characters that confuse it. I couldn't get the new readmatrix function to work but I could get dlmread (delimited read to work).
I recommend replacing the line
A(:,:,k) = csvread(filename)
with
A(:,:,k) = dlmread(filename,'')
I couldn't actually test this on your file, because for some reason when I try to download your attached file it just opens as webpage in my browser. I tried copying and pasting from there into a text file and the above line then seemed to work OK.
Regarding adding up the matrices once you have them in the big three dimensional array, A, this can be done with just the one line
Atotal = sum(A,3)
which adds across the third dimension.
Jonathan,
Finally the one below worked for me. Thanks for your time and effort
File=dir('path/*.dat');
for i=1:size(File,1)
A(:,:,i)=load(File(i,1).name);
end
sum = 0
for i=1:size(File,1)
sum = sum + A (:,:,i);
end
Hi Gollapalli,
I'm glad you finally got it working. I have only used the "load" command for loading .mat files, but I see now now from the documentation that it can also be used for loading "ascii" text files, which I guess is what you must have had. That's good to know.
Regarding your final loop to add the elements, in general it is good to take advantage of MATLAB's ability to efficiently handle such operations without loops. So you could replace the loop with
B = sum(A,3) % sum across the third dimension of A
Avoiding loops should make the code run faster, and also makes the code simpler to read.
Also, although MATLAB doesn't complain when you use a variable name that is the same as a MATLAB function, it can make things a little confusing. So in your case, your local variable named "sum" is actually taking the place of the MATLAB sum command. If later in your code you tried to use the "sum" command it would not work, as it would think that you were referring to your variable named "sum".
I noticed that you unaccepted the answer to this. I'm curious, which one did you find that was better? If there is a better answer, it would be good to make a comment pointing others toward it, in case they are following this thread, and it just seems to be left unanswered.

Sign in to comment.

More Answers (0)

Asked:

on 11 Apr 2019

Commented:

Jon
on 22 Apr 2019

Community Treasure Hunt

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

Start Hunting!