rnt_42 - 5 months ago 9x

Python Question

So, I have to load many .mat files with some features to plot it.

Each array to be plotted is loaded into a dictionary:

`import numpy as np`

import scipy.io as io

dict1 = io.loadmat('file1.MAT')

dict2 = io.loadmat('file2.MAT') # type = dict

dict3 = io.loadmat('file3.MAT')

...

so I have to take the dictionarie's element I need, to plot after:

`array1 = dict1['data']`

array2 = dict2['data']

array3 = dict3['data']

...

After this, I can plot the data. It works, but looks dumb to me (If I have 100 vectors, it will take some time...). Is there a better way to make this task?

Answer

Given that you are talking about dealing with many matrices, you should manage them as a collection. First, let's define your set of files. It could be a tuple, or a list:

```
Matrix_files = [ 'fileA.MAT', 'file1.MAT', 'no pattern to these names.MAT' ]
```

If they happen to have a pattern, you might try generating the names:

```
Matrix_files = [ 'file{}.MAT'.format(num) for num in range(1,4) ]
```

If they share a common location, you might consider using one of the various directory scanning approaches (`opendir`

or `glob`

, to name two).

Once you have a list of filenames, you can read the dictionaries in:

```
def read_matrix(filespec):
from scipy.io import loadmat
md = loadmat(filespec)
# process md
return md
```

With that, you can either get all the data, or get some of the data:

```
All_data = [read_matrix(f) for f in Matrix_files]
Some_data = [read_matrix(f)['data'] for f in Matrix_files]
```

If you only care about the data, you could skip the function definition:

```
from scipy.io import loadmat
Just_data = [loadmat(f)['data'] for f in Matrix_files]
```

Source (Stackoverflow)

Comments