Clear Filters
Clear Filters

Calling MATLAB file from Linux using MCR

2 views (last 30 days)
Mohamed AKI Ahmed
Mohamed AKI Ahmed on 13 Apr 2023
Answered: Manoj Mirge on 20 Apr 2023
I have a MATLAB file that is called "run_mycode.m", This code is a function that takes 4 arguments(one boolean, two empty strings, and one mat file). I want to call this function from a Python script on Linux using MCR (I don't have MATLAB on that machine so I'm using MCR). I tried the following:
from subprocess import call
mypart = /mycomputer/part.mat
mcr_path = '/mycomputer/matlab/MATLAB_Compiler_Runtime/v91'
arg = '1 "" "" ' + mypart
cmd = 'run_mycode.sh ' + mcr_path + " " + arg
call(cmd, shell=True)
However, I get the following result:
/bin/sh: run_mycode.sh: command not found

Answers (1)

Manoj Mirge
Manoj Mirge on 20 Apr 2023
Hi ,
Although you are in the same directory as the “run_mycode.sh” file, Bash could not find this file. Because the current directory isn't in your $PATH variable of Linux bash.
In Linux, UNIX and related operating systems, . denotes the current directory. Since you want to run a file in your current directory and that directory is not in your $PATH, you need the ./ bit to tell the shell where the executable is. So, ./run_mycode.sh means run the executable called run_mycode.sh that is in this directory.
Therefore, you need to specify the relative or absolute path to the file so that shell knows where our executable file is.
Regarding your situation, code would look like this:
from subprocess import call
mypart = /mycomputer/part.mat
mcr_path = '/mycomputer/matlab/MATLAB_Compiler_Runtime/v91'
arg = '1 "" "" ' + mypart
cmd = './run_mycode.sh ' + mcr_path + " " + arg % Just add ./ before
% run_mycode.sh
call(cmd, shell=True)
Hope this helps.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!