0

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?

4
  • Use a loop, maybe? Commented May 4, 2016 at 23:36
  • Yes, thanks Rad. but how can I store this arrays? Into another array with bigger shape? Just beggining, sorry for noob question. \ Commented May 4, 2016 at 23:41
  • If you need an array of arrays, then yes, Python allows that. Just keep on appending them I think (check the documentation). Commented May 4, 2016 at 23:44
  • Have you looked at dict.update(otherdict)? Commented May 4, 2016 at 23:53

1 Answer 1

4

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, @Austin Hastings. It is exactly what I´m looking for. It was very helpful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.