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

Click For Summary
The discussion revolves around loading multiple .mat files containing large 2x2 matrices and calculating their average values efficiently. The user successfully loads the files named data1.mat through data60.mat and seeks to create a 1x60 array of average values from these matrices. While individual averaging works, the user aims to streamline the process with a single code snippet. They initially attempt to use the eval function to reference the matrix names stored in a string array but encounter an error indicating that eval does not recognize the input as a valid reference. The solution provided suggests using curly brackets with the name array (e.g., eval(name{1})) to correctly access the matrix data, resolving the issue and allowing for the intended averaging process.
Hoplite
Messages
50
Reaction score
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
Try using eval(name{1})

note the curly brackets after name.
 
That really works!

Thanks, Matonski.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
4K
Replies
1
Views
4K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
5K
  • · Replies 7 ·
Replies
7
Views
9K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
8K