Using Python to run a MATLAB function using MCR without importing the package
23 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello all. I'm aware of the two different ways to comunicate between MATLAB and Python (i.e., using the API and SDK). My question is about neither of them I have a MATLAB function that needs to 3 arguments to run. This function when given the arguements, does some calculations and saves a text file with the results. Since the computers that will run my program don't have MATLAB installed, the use of MCR will be done.
My question is, how can I use this function from Python without importing the package (i.e., compiling the code for python and importing it). I only want to run the function with the arguments specified using the MCR.
I know there could be a way using shell but I'm not familiar with how to do so
0 comentarios
Respuestas (1)
Ashutosh Bajpai
el 17 de Feb. de 2023
To run a MATLAB function from Python without importing the package, you can use the subprocess module in Python. The subprocess module allows you to run shell commands, including commands to run a MATLAB function using the MATLAB Compiler Runtime (MCR).
Here's an example of how you can use the subprocess module to run a MATLAB function that takes three arguments and saves a text file:
import subprocess
def run_matlab_function(arg1, arg2, arg3):
command = "path/to/MCR/v90/bin/glnxa64/MATLAB -r 'run_my_function(" + str(arg1) + "," + str(arg2) + "," + str(arg3) + ");exit'"
subprocess.call(command, shell=True)
run_matlab_function(1, 2, 3)
In this example, the function run_matlab_function takes three arguments and creates a shell command to run the MATLAB function run_my_function with the specified arguments. The command uses the MCR binary and runs the function in the MATLAB environment. The subprocess.call function is used to run the shell command. The shell=True argument specifies that the command is a shell command.
0 comentarios
Ver también
Categorías
Más información sobre Python Package Integration en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!