# Import numpy and scipy (maybe through Anaconda) in Matlab

8 Ansichten (letzte 30 Tage)
Sim am 7 Aug. 2024
Kommentiert: Sim am 8 Aug. 2024
(1) Introduction.
This is a follow up question of Call a Python function inside a MATLAB loop.
I am trying to run the following Python function (which is saved as anderson_darling.py) on Matlab:
rng = np.random.default_rng()
method = stats.PermutationMethod(n_resamples=9999, random_state=rng)
res = stats.anderson_ksamp([a,b], method=method)
print(a)
print(b)
print(res.statistic)
print(res.critical_values)
print(res.pvalue)
if __name__ == "__main__":
import sys
import numpy as np
from scipy import stats
input_list1 = list(map(int, sys.argv[1].strip('[]').split(',')))
input_list2 = list(map(int, sys.argv[2].strip('[]').split(',')))
a = np.array(input_list1)
b = np.array(input_list2)
To run this file, I open a Terminal (throgh Visual Studio Code, but this is not relevant), and I give the following command:
(base) xyz@xyz myfolder % python3 anderson_darling.py "[1,3,5,6,4,6,7,1,2,7]" "[1,3,5,6,4,6,7,1,2,7]"
The result, that appears on the Terminal is the following:
[1 3 5 6 4 6 7 1 2 7] % <-- array a
[1 3 5 6 4 6 7 1 2 7] % <-- array b
-1.4363560826434034 % <-- res.statistic
[0.325 1.226 1.961 2.718 3.752 4.592 6.546] % <-- res.critical_values
1.0 % <-- res.pvalue
(2) Problem.
Now, when I go to the editor of Matlab, I try to follow some indications of @Pavan Sahith, from Call a Python function inside a MATLAB loop:
a = [1,3,5,6,4,6,7,1,2,7];
b = [1,3,5,6,4,6,7,1,2,7];
% Convert MATLAB arrays to Python lists
py_a = py.list(a);
py_b = py.list(b);
However, I get the following error on the Matlab Command Window:
Python Error: NameError: name 'np' is not defined
(3) Potential source/reason of the problem & Question
I guess the problem is related to the way of importing "numpy" and "scipy" to Matlab. I found the following discussions on the Matlab forum, but it looks like they are related to Windows:
I instead use Mac and I do not know what to do.
Therefore, how can I successfully import and use "numpy" and "scipy" to Matlab?
(4) Additional information on my system.
If it can help, my Python, Anaconda (the "numpy" and "scipy" packages are contained in (Ana)conda) and Matlab versions are the following ones:
From Terminal:
(base) xyz@xyz ~ % python3 --version
Python 3.12.4
(base) xyz@xyz ~ % conda --version
conda 24.7.1
(base) xyz@xyz ~ % conda info --envs
# conda environments:
#
base * /opt/anaconda3
/usr/local/anaconda3
From the Matlab's Command Window:
% Python Version for your system (inside Matlab)
% https://ch.mathworks.com/help/matlab/ref/pyenv.html
>> pyenv
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "/Library/Developer/CommandLineTools/usr/bin/python3"
Library: "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/libpython3.9.dylib"
Home: "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9"
ExecutionMode: InProcess
ProcessID: "4128"
ProcessName: "MATLAB"
% Matlab version
>> version
ans =
'23.2.0.2515942 (R2023b) Update 7'
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

dpb am 7 Aug. 2024
No joy regarding Python, but to get to the desired end result, see the <FEX Submittal> that returned the identical test statistic your example does...much simpler/quicker route it would seem (although the function needs to be edited to return the results in a struct rather than or in lieu of the present long narrative dumped to the command window, that would be a pretty trivial exercise).
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Sim am 8 Aug. 2024
thanks a lot @dpb :-)

Melden Sie sich an, um zu kommentieren.

### 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