Problem in running python function from MATLAB console

74 Ansichten (letzte 30 Tage)
Poulomi
Poulomi am 23 Aug. 2025 um 15:16
Kommentiert: Anmol am 1 Sep. 2025 um 12:27
Hello
I am new to python but extensive users of MATLAB. I wish to run Python function from MATLAB command prompt, so that the derived output can be utilized in further processing in MATLAB. I am trying to run this function available in this link. I have installed numba and numpy via cmd. As given in different tutorials, I have added my python path to MATLAB, using pyenv command. Then, I tried to call the function using below script for desired values as below:
[Twb] = py.wetbulb_dj08_spedup.WetBulb(25.,100000.,0.015,0),[0]
However, I am getting the following error:
Python Error: IndexError: list index out of range
Kindly help me in this regard. Also, if I wish to run for the time series array, how to define them from the MATLAB console. Do I need to define these vectors using py.numpy.array(A), where A is my time series array? Kindly help.
I am using MATLAB2024b.

Akzeptierte Antwort

Dev
Dev am 29 Aug. 2025 um 8:48
In the code provided in the question, when you call the function "WetBulb", the [0] added at the end results in indexing the Python return value as if it were a list/tuple. That’s why we get the 'IndexError' exception. I have provided a code snippet below which represents a better way of calling the same-
% Ensure MATLAB is using the correct Python
pyenv
% Import your module
mod = py.importlib.import_module('wetbulb_dj08_spedup');
% Call the function
Twb = mod.WetBulb(25.0, 100000.0, 0.015, int32(0));
Here, int32(0) ensures MATLAB passes a proper integer to Python. Sometimes MATLAB defaults to double, which may confuse Python if it expects int.
Furthermore, if we wish to run for the time series array, the best practice is to use py.numpy.array(A) to convert MATLAB arrays into numPy arrays before passing to Python.
I hope the above explanation resolves your query.
  2 Kommentare
Poulomi
Poulomi am 31 Aug. 2025 um 6:52
For me, the foll. statement, itself is not working.
mod = py.importlib.import_module('wetbulb_dj08_spedup')
I am landing to a range of error messages:
Error using wetbulb_dj08_spedup><module> (line 67)
Python Error: IndexError: list index out of range
Error in <frozen importlib>_call_with_frames_removed (line 488)
Error in <frozen importlib>exec_module (line 999)
Error in <frozen importlib>_load_unlocked (line 935)
Error in <frozen importlib>_find_and_load_unlocked (line 1331)
Error in <frozen importlib>_find_and_load (line 1360)
Error in <frozen importlib>_gcd_import (line 1387)
Error in __init__>import_module (line 90)
I am unable to call any of the Python function from my MATLAB console. I am using pyversion 3.12, which is compatible with MATLAB2024b. I am not sure if these integration really works.
Anmol
Anmol am 1 Sep. 2025 um 12:27
Hi Poulomi,
I understand that you are facing the issue while running the command:
mod = py.importlib.import_module('wetbulb_dj08_spedup')
on MATLAB 2024b and pyversion 3.12 when trying to import the file wetbulb_dj08_spedup.py.
The error which is occurring is :
Error using wetbulb_dj08_spedup><module> (line 67)
Python Error: IndexError: list index out of range
Error in <frozen importlib>_call_with_frames_removed (line 488)
Error in <frozen importlib>exec_module (line 999)
Error in <frozen importlib>_load_unlocked (line 935)
Error in <frozen importlib>_find_and_load_unlocked (line 1331)
Error in <frozen importlib>_find_and_load (line 1360)
Error in <frozen importlib>_gcd_import (line 1387)
Error in __init__>import_module (line 90)
The error is faced because of the attempt to access the first index of sys.argv which is empty.
  1. Try declaring the variables inside a function and not globally inside the python file. Then you can pass arguments to the function for initialization.
  2. Also try keeping a check for NaN (Not a Number) values before indexing any variable by adding print statements inside the python code.
Hope this helps.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by