1

I'm trying to access a .mat file which contains various fields (constants, vectors, variables) where the variables are 10000x100 matrices and vectors are of size 1x100.

I tries to access the same using various packages like loadmat from scipy, loadmat from mat4py, tried by importing the package h5py and read_mat from pymatreader, but when I try to run the code, neither it shows any error not it prints the print statements.

Also I tried to install the matlabengine using pip but it doesn't get installed, maybe because my matlab version is 2024a and matlabengine support is in 2026a.

Kindly recommend me the correct way to access the same. The snippet of my code is here as attached.

#Python code for solving nonlinear equations numerically 
from scipy.optimize import fsolve
from scipy.optimize import root
import numpy as np
import numpy.lib.scimath as math
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd
#from scipy.io import loadmat
#from mat4py import loadmat
from pymatreader import read_mat

# Define constants (symbols or assign numeric values later)
chi_= 20
beta_= 4
chi0= 1
chi1= 10
alpha_chi= 1
D0= 0.25
D1= 2.5
alpha_D= 0.1
mu_c= 1.2
lam= 0.5
den = 10

#--------------------------------------------
# Define the system of nonlinear equations
#--------------------------------------------
def F(X):
    Q, Gam, g1, g2, g3 = X
    # Load .mat file correctly
    data = read_mat(r'E:\Rahul\Data_tokamak\data&plot\data3-field_ITB\data_3f_itb\Fisher_ITB_data\set5\data1_x0_03.mat')
    print("Type of data:", type(data))
    print("Data content preview:")
    # Remove MATLAB meta keys
    data_keys = [k for k in data.keys() if not k.startswith('__')]
    print("Keys found in file:", data_keys)

    if len(data_keys) == 0:
        raise KeyError("No user-defined variables found in MAT file.")

    # Get the first variable automatically
    var_name = data_keys[0]
    data_1 = np.array(data[var_name])
    print(f"Loaded variable '{var_name}' with shape {data_1.shape}")
#Define Q Gam g1 g2 g3
    

rgds

3
  • How large is the .mat file? What is everything contained in the file, their shape, and their data type? Are you running out of memory and the python process is being killed? Add print statements before and after trying to read in the file to see where it's failing. I've used scipy's load_mat before and it has worked fine for me. Commented Nov 8 at 13:57
  • With a notebook you should be able to run the loadmat (or other) without all the extra code you show. Focus on the load issue for now, and explicitly show the errors. Commented Nov 8 at 16:33
  • Yes, it is working. Thanks a lot. Commented Nov 8 at 17:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.