MATLAB Answers

setenv fails to change environment variables on Linux

95 views (last 30 days)
Arabarra
Arabarra on 13 Mar 2019
Answered: Jesse Hopkins on 15 Oct 2019
Hi,
I'm using Matlab on Ubuntu 16.04, and it seems that Matlab cannot run system calls:, because of an apparently failure of the Matlab command setenv to change the environment variables in the shell running below my Matlab session. To clarify what I mean, I copy and paste from my current session in Matlab:
>> ls /usr/local/bin/chimera
/usr/local/bin/chimera
This file /usr/local/bin/chimera is an executable that I would like to run from matlab without needing to pass the full path (i.e., as !chimera or system('chimera');). To make it visible to my Matlab session, I prepend it to the environment variable PATH of the linux shell through the matlab commands getenv and setenv
>> setenv('PATH',['/usr/local/bin/:',getenv('PATH')]);
However, the Linux shell cannot find the executable "chimera" when asked by Matlab:
>> !which chimera
>>
In fact, looks like the PATH variable was never changed from the point of view of the Linux Shell
>> !echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:.
although Matlab finds the correct value through getenv:
>> getenv('PATH')
ans =
:/usr/local/bin/chimera:/usr/local/bsse/MATLAB_R2018a/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/opt/puppetlabs/bin:'
In any case, this does not help me, as my Matlab session is unable to run system calls properly:
>> unix('chimera');
/bin/bash: chimera: command not found
>> system('chimera');
/bin/bash: chimera: command not found
So, what can be the reason that setenv is not working properly?
thanks in advance,
Daniel
matlab version: (R2018a)

  0 Comments

Sign in to comment.

Answers (1)

Jesse Hopkins
Jesse Hopkins on 15 Oct 2019
Hello, I recently came across this same issue. While I wasn't able to directly solve it, I was able to workaround by setting up the PATH variable appropriately in my account's .cshrc file located in the account home directory, adding a line like
setenv PATH /my/new/path:${PATH}
That worked for future logins to my account. To refresh your current session, execute from your home directory:
source .cshrc

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!