I have a .m code what I want to run in python. Is it any easy way? 1. this code is not a function. 2. don't want to show the matlab window.
Best Regards
Xinfeng

 채택된 답변

Stephan
Stephan 2018년 8월 21일
편집: Stephan 2018년 8월 21일

4 개 추천

Hi,
this is not a big deal. The python code looks like:
import matlab.engine
eng = matlab.engine.start_matlab()
eng.simple_script(nargout=0)
eng.quit()
The Matlab script would be perhaps this one line saved as simple_script.m:
a = 'it works easily...'
Make sure that the script is saved in a folder matlab knows as a search folder.
Then run your python script and get the answer:
a =
'it works easily...'
It is not necessary that you have a function in your .m-file, but possible. See more information here.
The Matlab screen is not visible by default, so you should be happy if you follow this and the linked information...
Best regards
Stephan

댓글 수: 5

madhan ravi
madhan ravi 2018년 8월 29일
Vielen Dank @Stephen Jung was looking for this for a very long time :)
Saurabh Soman
Saurabh Soman 2019년 12월 6일
편집: Saurabh Soman 2019년 12월 6일
I tried this code snippet but it is giving me error
ModuleNotFoundError: No module named 'matlab'
I am using spyder(python 3.7). Are there any other pre-requisites for running this script?
I am trying to run an m script file from python script.
Please reply ASAP. Thanks!
Rishab khantwal
Rishab khantwal 2019년 12월 20일
did you got the solution
Ankit Jaiswal
Ankit Jaiswal 2020년 2월 8일
It doesnt work in Anaconda-spyder. Try in Pycharm, it will work.
swagat shovan
swagat shovan 2020년 3월 6일
it works in anaconda spyder but u gave to install anaconda 3.4 or below for matlab 2016b or according to the python package present in matlab.
you can check the python package available in matlab from matlabroot\extern\engines\python path
for safe side install anaconda 2.7 or 3.0 and
goto> anaconda prompt and type
cd "matlabroot\extern\engines\python"
python setup.py install

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Gonzalez
David Gonzalez 2019년 3월 28일

0 개 추천

How can I do this if the script is in another folder?
Thanks!

댓글 수: 3

Wasabi Lee
Wasabi Lee 2019년 4월 25일
You should just add the location of the file to path.
addpath('matlab/myfiles','-end')
Yihui Quek
Yihui Quek 2019년 7월 22일
I think the OP means, 'what should I add to my Python script if I want it to run a matlab script in a different folder?' Your command is a Matlab command, which doesn't help if the Matlab script can't even be found by Python.
Huang Chi-En
Huang Chi-En 2020년 3월 10일
편집: Huang Chi-En 2020년 3월 10일
The classical python function :
# python..
import sys
positionOfPath = 1
sys.path.insert( positionOfPath, 'your matlab path' )
# end of python
may help python find the path to access the matlab script ~~

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

질문:

2018년 8월 21일

편집:

2020년 3월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by