0

I have written a function to convert a NumPy array into a mat file using scipy.io.savemat() but it produces a generic type of file: File with the same name but not of type .mat as expected. The array I want to save is of type <class 'numpy.ndarray'> as verified by the print statement. I don't know what may be the issue.

 
import os
import scipy.io

def save_electrode_measurement_to_matfile( measurement: np.ndarray, file_name: str = 'latest_electrode_measurement'):
    print(type(measurement))
    mdict = { 'electrode_measurement' : measurement}
    dir = r"...\Simulation_results"
    file_path = os.path.join(dir, file_name)
    scipy.io.savemat(file_path, mdict, appendmat = True)
1
  • Have you tried having the filename include the extension .mat? Commented Mar 12, 2022 at 15:17

1 Answer 1

0

Refering to the documentation, I think that you should add '.mat' to the filename (which is the file_path in your case). By replacing

file_path = os.path.join(dir, file_name)

with

file_path = os.path.join(dir, file_name) + ".mat"

Your problem should be solved.

Sign up to request clarification or add additional context in comments.

3 Comments

It worked, thank you! Apologies, I was under the impression that was what appendmat optional argument did.
appendmat works as you expected - at least in my tests. Actually adding the mat is the default behavior. Does you filename already have something that looks like an extension?
I don't fully follow the code that appends the '.mat' if not 'already present' (it's buried several function calls down), but it looks like it's easily fooled. So it's probably safer to include your own.

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.