MATLAB started as an app in OS X causes problems with "unix" function

4 次查看(过去 30 天)
I noticed that the function "unix" acts differently, mainly in regards to my PATH variable, depending on whether I start MATLAB from the application or from Terminal.
When I start from Terminal, I get the same $PATH that I would in that Terminal window:
>> unix('echo $PATH')
/usr/local/fsl/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/Applications/Inkscape.app/Contents/Resources/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
When I start from the Application, it gets a very basic PATH from... somewhere:
>> unix('echo $PATH')
/usr/bin:/bin:/usr/sbin:/sbin
I can change my path, but it reverts the next time I use the unix function:
>> unix('source /Users/pcrutchl/.bash_profile; echo $PATH')
/usr/local/fsl/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/Applications/Inkscape.app/Contents/Resources/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
ans =
0
>> unix('echo $PATH')
/usr/bin:/bin:/usr/sbin:/sbin
ans =
0
(To be clear: changing the path manually every time, or even initially, isn't really a solution, just an exploration of the behavior. If I could put something in startup.m to fix it, that would be fine.)
Is this known behavior? Is there a good way to get the application MATLAB to behave like the MATLAB launched from command line?

采纳的回答

Walter Roberson
Walter Roberson 2011-9-6
This is standard UNIX behavior, not the responsibility of MATLAB.
Each time you unix(), a new shell process is created. You do whatever to the environment variables of that shell process, but then that shell process ends before control returns to MATLAB.
You can setenv() from within MATLAB to affect the environment variables that are passed to all unix() shells open after the setenv (in that MATLAB session)
Or you can use the normal configuration files of your shell to tell it that it should execute appropriate commands each time the shell is started.
I suggest that in a terminal shell, you execute
man bash
skip the options section for now and pay serious attention starting near "The following paragraphs describe how bash executes its startup files."
  2 个评论
Karl
Karl 2011-9-6
Ah, setenv() was the function I was missing. I'll just put that in my startup, executing my .bash_profile, and it should all be solid.
Just to be clear-- when MATLAB is started from Terminal, the unix() function takes the environment variables, like PATH, from the shell in which it was launched?
Walter Roberson
Walter Roberson 2011-9-6
setenv() cannot execute a shell script: it can only set variables to exact strings.
Use the two-output version of unix() to capture the result of the echo $PATH, and setenv() your path to that string result.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by