フィルターのクリア

Executing unix commands set in PATH in matlab does not work with unix command

26 ビュー (過去 30 日間)
Matthew
Matthew 2012 年 2 月 1 日
コメント済み: Makis 2021 年 9 月 22 日
Greetings,
When running the "unix" command in matlab, i.e.: unix('<unix_command>') I get an error, the unix command is not found in /bin/bash. Interestingly, other commands work with the unix command, e.g.: unix('who'). Also of note, running "echo $SHELL" in the terminal window returns "/bin/bash". The command I am trying to run is part of a package that is set in my PATH.
Any help you might offer would be greatly appreciated-
  1 件のコメント
Walter Roberson
Walter Roberson 2012 年 2 月 1 日
What is shown if you
unix('echo $PATH')
And does it match the PATH that was set before you started MATLAB? (except that starting MATLAB probably added MATLAB directories to the PATH) ?

サインインしてコメントする。

回答 (5 件)

Walter Roberson
Walter Roberson 2012 年 3 月 12 日
PATH = getenv('PATH');
setenv('PATH', [PATH ':/usr/local/desiredpath']);
unix('commandofinterest')
  3 件のコメント
Monique
Monique 2017 年 5 月 23 日
編集済み: Monique 2017 年 5 月 23 日
Thank you Walter! 5 years later and this answer is still helpful
Makis
Makis 2021 年 9 月 22 日
Great this worked.

サインインしてコメントする。


Miriam
Miriam 2019 年 5 月 30 日
編集済み: Walter Roberson 2019 年 5 月 30 日
Hi,
Following up on this, I encounter a similar problem for unix commands (Ubuntu, Win 10).
I can call basic unix functions from Matlab directly (for example !ubuntu -c ls) but I can’t access a specific program (which lives in /home/myname/abin) or any of its functions which I would like to run from Matlab.
When I set the path to access the directory of this program in Matlab using setenv, the path is indeed set correctly. But whatever function I am trying to run, with using !myfunction or unix(myfunction) or !ubuntu myfunction, I get the error “myfunction is not recognized as an internal or external command, operable program or batch file.“ (or similar for the other calls).
I think this might be related with Windows being blind for Ubuntu directories or not having permission to access them but even setting the path directly to Ubuntu files stored in Windows (as decribed here: https://www.howtogeek.com/261383/how-to-access-your-ubuntu-bash-files-in-windows-and-your-windows-system-drive-in-bash/ ) does not do the job.
Before I reinstall Matlab in Ubuntu only to be able to access this program, does anyone know how to solve this problem?

Ken Atwell
Ken Atwell 2012 年 2 月 2 日
MATLAB does not source your BASH shell resource file, so anything established in that file may not be available to the unix command.
First check your PATH as Walter recommend, and if it is not to your liking, you can use setenv to include the directories you need.
  5 件のコメント
Matthias Fripp
Matthias Fripp 2017 年 10 月 8 日
@Walter Roberson, the setenv command is a Matlab command; it should be run directly on the Matlab command line, not via system() or unix(). When run from within Matlab, it modifies the environment within which Matlab is running, and that affects all subsequent unix() or system() calls.
Walter Roberson
Walter Roberson 2017 年 10 月 8 日
Matthew had used
unix('setenv PATH "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/desiredpath"')
which is only valid syntax for csh derivatives. Setting the environment from within the command to be unix()'d is a valid approach, but these days typically involves an "export" command.

サインインしてコメントする。


Carmine
Carmine 2014 年 2 月 3 日
Hello, I'm still getting the same error:
unix('ls')
gives
ans=127
even though I've added /bin to the path. Calling unix('/bin/ls') also gives 127. Any possible other solutions?
  1 件のコメント
Walter Roberson
Walter Roberson 2014 年 2 月 3 日
You have likely defined a variable named "unix", or else have defined your own function named "unix". Check with
which -all unix

サインインしてコメントする。


Carmine
Carmine 2014 年 2 月 4 日
Thanks for you comment, Walter. No, I get the following:
>> which -all unix
built-in (/usr/local/common/matlab_2013b/toolbox/matlab/general/unix)
Also, if I try system('pwd'), I get 127.
If I run Matlab on another machine, the commands work fine. Somewhere, there is a difference between the two machines, or my environment setup. I am wondering where I should look to find that difference. getenv('LD_LIBRARY_PATH') give the same paths on both machines.
  2 件のコメント
Walter Roberson
Walter Roberson 2014 年 2 月 4 日
How about
!pwd
?
My guess: you have configured your shell to source a file each time a shell is started, but the commands inside it rely upon being sourced from a shell that has terminal access. Shells started with unix() or system() do not have an associated terminal.
The file being sourced that I am referring to is probably not /etc/profile or ~/.bash_profile or ~/.bash_login or ~/.profile as those are the ones checked for interactive login, which would not be the case in this situation. However, ~/.bashrc would be used for interactive non-login shells, and for any shell the environment variable BASE_ENV is checked and if it refers to a file then the file is sourced. But if the shell was invoked as "sh" instead of as "bash" then some of the above files are skipped and the environment variable examined is ENV instead of BASE_ENV
The most common startup command that causes problems is "tset" to try to query the user to set the terminal type, especially in the form
eval `tset -s -m ... `
Such a command needs to be protected by testing to see if a terminal is defined, such as by using
[[ -t 0 ]] && eval `tset -s -m ... `
which checks that file descriptor 0 (stdin) is associated with a terminal before it goes ahead with the tset.
Carmine
Carmine 2014 年 2 月 5 日
No, that doesn't work either (it gives no output at all).
>>!pwd
>>
And I am not using tset anywhere.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by