Using Python to run a MATLAB function using MCR without importing the package
19 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Ashutosh Bajpai
2023-2-17
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!