I have a minor problem -- the workaround is trivial, but I wanted to learn why the problem exists at all. I have some environment variables set in .bashrc:
export FREESURFER_HOME=/usr/local/freesurfer
In my startup.m file, I tried to set the variable:
freesurfer_home=getenv('FREESURFER_HOME');
However, this call to getenv returns an empty character array. Calls to getenv within MATLAB return some environment variables (e.g., getenv('HOME')) but not others. I can see which environment variables are visible to MATLAB from the following line:
map = java.lang.System.getenv();
I can also confirm the missing values are visible, at least within the shell, using any of
export
env
printenv
Is there an explanation for this discrepancy?
If you check process tree with system and ps f command in Matlab, you will see that it actually launches sh and executes there your command. I don't have Matlab, the same thing with Octave.
octave:1> system("ps f")
PID TTY STAT TIME COMMAND
2520 pts/0 Ss 0:00 bash
2580 pts/0 Sl+ 0:02 \_ octave
2885 pts/0 S+ 0:00 \_ sh -c ps f
2886 pts/0 R+ 0:00 \_ ps f
ans = 0
In this case variables set in .bashrc are available through getenv as I launch octave from bash. But when launching Matlab from menu launcher, the situation is different:
octave:1> system("ps f")
PID TTY STAT TIME COMMAND
3400 pts/2 Ssl+ 0:01 /usr/bin/octave
3444 pts/2 S+ 0:00 \_ sh -c ps f
3445 pts/2 R+ 0:00 \_ ps f
bash has not been executed and exports from .bashrc are not set.