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

Click For Summary
SUMMARY

The discussion focuses on averaging multiple 2x2 matrices stored in .mat files using MATLAB. The user successfully loads the .mat files named data1.mat through data60.mat but encounters issues when attempting to access them using the eval function. The solution provided involves using curly brackets to reference the cell array, specifically using eval(name{1}) instead of eval(name(1)). This method effectively resolves the issue and allows for the desired averaging of the matrices.

PREREQUISITES
  • Familiarity with MATLAB programming language
  • Understanding of .mat file structure and loading
  • Knowledge of matrix operations in MATLAB
  • Experience with cell arrays and indexing in MATLAB
NEXT STEPS
  • Explore MATLAB's mean function for multi-dimensional arrays
  • Learn about cell array manipulation in MATLAB
  • Investigate efficient file handling techniques in MATLAB
  • Study vectorization techniques to optimize MATLAB code performance
USEFUL FOR

MATLAB users, data analysts, and researchers who need to process and analyze large datasets stored in .mat files.

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 5 ·
Replies
5
Views
7K
  • · 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
3K