Matlabでpythonのプログラムを実行し、さらにその結果をMatlabの関数として出力する方法
17 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
現在、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);
以上の質問、解答していただけると幸いです。
0 commentaires
Réponses (1)
Hiroshi Miyagawa
le 18 Déc 2020
実行したいPythonプログラムを関数化するのが分かり易いと思います。
その上で以下のページが参考になると思われます。
0 commentaires
Voir également
Catégories
En savoir plus sur Python からの MATLAB の呼び出し 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!