Include several folders into path using Matlab Engine for Python
44 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Davide Mori
le 21 Oct 2020
Modifié(e) : Stefanie Schwarz
le 19 Juil 2023
Hi everyone,
I've just found out the Matlab engine for Python and I've managed to install it and the simple commands work well as well. Now I have a problem:
I have a huge library with a lot of subfolders. Inside them there are a lot of functions and there are functions that call other functions through these folders. As you may know, in Matlab is not a problem until I specify the path and include all these folders in the path.
How can I do that in my python code? This is what I've written in python:
import matlab.engine
eng = matlab.engine.start_matlab()
path = r'C:\Users\*name*\Desktop\pythonProject\sfs-matlab-master'
eng.cd(path, nargout=0)
eng.example(nargout=0)
What I've done was:
1) put the library "sfs-matlab-master" into my python_project folder
2) Inside this library I have a script "example.m" which in turn call other functions inside the library
When I run the python code I got this error
Undefined function 'sound_field_mono_nfchoa' for input arguments of type 'struct'.
where "sound_field_mono_nfchoa" is a script which is inside other subfolders.
Hope I've made myself clear and that you could help me :)
0 commentaires
Réponse acceptée
Athul Prakash
le 30 Oct 2020
Hi Davide,
If you're in MATLAB at the command line, you may use genpath and addpath as follows to add a folder 'myOuterFolder' and all its subfolders to the MATLAB path:
addpath(genpath('myOuterFolder'));
You may call the same function(s) in the MATLAB Engine as well:
eng = matlab.engine.start_matlab()
s = eng.genpath('myOuterFodler')
eng.addpath(s, nargout=0)
Hope it helps!
1 commentaire
Stefanie Schwarz
le 19 Juil 2023
Modifié(e) : Stefanie Schwarz
le 19 Juil 2023
To add the paths to the MATLAB path permanently (so it is loaded automatically for every MATLAB session), make sure to run this in MATLAB:
addpath(genpath('myOuterFolder'));
savepath
In case this throws an error, refer to: https://www.mathworks.com/matlabcentral/answers/95811-why-can-t-i-save-my-matlab-path-on-windows-with-user-account-control-uac-enabled
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Call MATLAB from Python dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!