Change Environment Variable for Shell Command
This example shows how to substitute a user-specified value for an environment variable value set by MATLAB®when you call a function using thesystem
command.
When you use thesystem
command to call a function, the function inherits the MATLAB environment. To change environment variable values, use a shell wrapper. Use the environment variableMATLAB_SHELL
to specify a shell containing your updated variable. This example uses a custom value for the environment variableLD_LIBRARY_PATH
.
Create a wrapper filematlab_shell.sh
in the folder
with the following contents, where
is your custom value.
#!/bin/sh LD_LIBRARY_PATH=export LD_LIBRARY_PATH exec ${SHELL:-/bin/sh} $*
If you have a user-defined value forSHELL
, the expression${SHELL:-/bin/sh}
uses yourSHELL
value. Otherwise, MATLAB uses the Bourne shell.
From the operating system prompt, call MATLAB settingMATLAB_SHELL
to:
/matlab_shell.sh
Display your value ofLD_LIBRARY_PATH
from the MATLAB command prompt.
!echo $LD_LIBRARY_PATH
Now when you call a function using thesystem
command, the function uses theLD_LIBRARY_PATH
value specified by
.