MATLAB: Average a large number of matricies from .mat files

In summary, the conversation discusses the process of creating a 1x60 array from a series of large 2x2 matrices stored in .mat files. The speaker has successfully loaded each .mat file and wants to use a shorter code to average them all. They attempt to create a string array of the file names using 'strseq', but encounter an error when trying to use 'eval' to call the files. The solution is to use 'eval' with curly brackets instead of parentheses.
  • #1
Hoplite
51
0
I have a series of large 2x2 matricies, each of which is stored inside a .mat file. These files have the names data1.mat, data2.mat, data3.mat,..., data60.mat. I have sucessfully loaded each of these .mat files.

I want to create a 1x60 array whose entires are the average values of the matrices. i.e:

average(1) = mean(mean(data1));
average(2) = mean(mean(data2));
etc.

If I average the .mat files individually as above, it works, but I want to write a shot code which averages them all. I can make a string containing the names of the .mat files via

names = strseq('data',1:60)

This gives me an array with entries

name(1)=data1
name(2)=data2
etc.

However, when I try to use eval(name(1)) to call the .mat file, it doesn't work. It gives me the message

"? Undefined function or method 'eval' for input arguments of type 'cell'."

I take it that it is not recognising "name(1)" as a reference to the matrix data1. I'm not even sure that it's actually recognising name(1) as the string data1.


It would be great if anyone could explain what I'm doing wrong, or suggest another way to call these .mat files.
 
Physics news on Phys.org
  • #2
Try using eval(name{1})

note the curly brackets after name.
 
  • #3
That really works!

Thanks, Matonski.
 

FAQ: MATLAB: Average a large number of matricies from .mat files

How do I import a large number of .mat files into MATLAB?

To import multiple .mat files into MATLAB, you can use the "load" function with a wildcard (*) to specify the file names. For example, if your files are named "data1.mat", "data2.mat", and so on, you can use the command "load data*.mat" to import all of them into MATLAB.

How can I calculate the average of multiple matrices in MATLAB?

To calculate the average of multiple matrices, you can use the "mean" function. This will return the mean value for each element in the matrices. If you want to calculate the overall mean of the matrices, you can use the "mean2" function, which takes the average of all the elements in the matrices.

What is the best way to store the average of the matrices in MATLAB?

The best way to store the average of the matrices in MATLAB is to create a new variable and assign the result of the "mean" or "mean2" function to it. This will allow you to use the average value in further calculations or analysis.

Can I automate the process of averaging a large number of matrices in MATLAB?

Yes, you can use a for loop to automate the process of averaging a large number of matrices in MATLAB. This will allow you to iterate through each matrix and calculate the average, storing the result in a new variable.

How can I improve the efficiency of averaging a large number of matrices in MATLAB?

One way to improve efficiency is to pre-allocate memory for the new variable that will store the average. This will reduce the time it takes for MATLAB to create and resize the variable as it iterates through the matrices. Additionally, you can use parallel computing techniques to distribute the calculations across multiple processors, if available.

Similar threads

Replies
1
Views
4K
Replies
11
Views
4K
Replies
5
Views
9K
Replies
4
Views
4K
Replies
1
Views
2K
Replies
1
Views
3K
Back
Top