Matlabでpyt​honのプログラムを​実行し、さらにその結​果をMatlabの関​数として出力する方法

28 views (last 30 days)
Yiqi Qu
Yiqi Qu on 10 Dec 2020
Answered: Hiroshi Miyagawa on 18 Dec 2020
現在、Matlabでpythonのプログラムを実行することができますが、実行した結果は、Matlabのワークスペースで関数として出力したいです。
pythonのプログラムは下記となります。
こちらはSUMOという交通流シミュレーションを起動し、実行するためのプログラムです。さらに、下から三行目の「print(traci.vehicle.getSpeed('veh0'))」は、指定した車両の車速をログするためのコマンドです。pythonでは、Consoleウィンドウでログすることが出来ましたが、そのデータをMatlabの関数として出力する方法を知りたいです。
import os
import sys
import optparse
import traci
if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.path.append(tools)
else:
sys.exit("please declare environment variable 'SUMO_HOME'")
from sumolib import checkBinary
def get_options():
optParser = optparse.OptionParser()
optParser.add_option("--nogui", action="store_true",
default=False, help="run the commandline version of sumo")
options, args = optParser.parse_args()
return options
if __name__ == "__main__":
options = get_options()
if options.nogui:
sumoBinary = checkBinary('sumo')
else:
sumoBinary = checkBinary('sumo-gui')
traci.start([sumoBinary, "-c", "osm.sumocfg"])
for step in range(0,1500):
print(traci.vehicle.getSpeed('veh0'))
traci.simulationStep()
traci.close()
Matlabでは、下記のコマンドでpythonを実行することが出来ましたが、関数の出力はできませんでした。
cd C:/Users/cooki/sumo/tools/2020-11-11-11-46-21
command = 'python C:/Users/cooki/sumo/tools/2020-11-11-11-46-21/runner.py';
status = dos(command);
以上の質問、解答していただけると幸いです。

Answers (1)

Hiroshi Miyagawa
Hiroshi Miyagawa on 18 Dec 2020
実行したいPythonプログラムを関数化するのが分かり易いと思います。
その上で以下のページが参考になると思われます。

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!