Unix commands through Matlab - PATH not right? - macos

I've installed a series of binaries which appear in usr/local/bin on my MACOSX (They're called DCMTK). The usr/local/bin folder appears on the path as expected:
PATH=/Users/jim/Library/Enthought/Canopy_64bit/User/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/git/bin:niftyreg_install/bin
I can run these commands as expected from any folder in a Terminal window, however I can't seem to run any of them from within Matlab (2014b) using the command:
cmd=['dcmdump -h'];
system(cmd)
/bin/bash: dcmdump: command not found
Running 'env' in a Terminal shows that I am using the same SHELL as Matlab:
SHELL=/bin/bash
My question is why is it when Matlab invokes bin/bash it is not finding the binaries in usr/local/bin? Is there something in .bash_profile I need to update?
Thanks,
Jim

As Mark Setchell already pointed out in his comments, you can echo the PATH variable from within MATLAB using
cmd=['echo $PATH'];
system(cmd);
You can also get environment variables, such as $PATH using the MATLAB getenv function:
getenv('PATH');
As you also posted in comments, in your case /usr/bin/local is missing in the $PATH variable. MATLAB has an option to set environment variables via the setenv function. But watch out: This sets the variable to only the specified value. To append a folder, you have to query the existing variable and append a folder to that:
setenv('PATH', [getenv('PATH'),':','/usr/bin/local']);
As separator, either ; (for Windows systems) or : (for Unix based systems) is used. In the example above I added : as you are working with Mac OS X.

Related

How do programs add to $PATH variable with using .bash_profile?

How do some programs seem to add to the $PATH variable without using a .bash_profile file? What are the advantages to these approaches of adding to $PATH variable, but not using a .bash_profile file?
For my mac
echo $PATH returns
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin
which java returns
/usr/bin/java
which tex returns
/Library/TeX/texbin/tex
How is Java available in the terminal without being in the $PATH variable? (This is an example of something running in the terminal without being in the $PATH variable.)
How did TeX add to the PATH variable without using a .bash_profile file?
What added Library/Apple/usr/bin to the PATH variable? Wikipedia says that only the first three are defaults: /bin, /usr/bin, and /usr/local/bin.
Note: Other StackOverflow posts helped users create a .bash_profile file (a file that doesn't exist by default on a Mac) to run programs such as android and adb when they received -bash: android: command not found
My question is about how the terminal works (like how java is running without being on $PATH) and how other programs (like TeX) added to $PATH without using a file like .bash_profile.
I am running macOS 10.15.5, confirmed that I do not have a .bash_profile file, do have homebrew installed (not sure if that affects anything) and when I open a terminal, it says "The default interactive shell is now zsh." (I think I installed zsh after reading a different StackOverflow a few weeks back.

Command not found and ImportError in Python for OSX

I have OSX El Captain with installed Python3.5 and Python2.7.
I have a folder with my python script and functions which I already added to the environment variable PATH and somehow[*] also to sys.path:
Project_folder/
- Python/
-- __init__.py
-- batchProc.py (written in Python 3.4)
-- (other python files)
I set my PATH with a .bash_profile file:
PATH=“/Users/Robyc/Project_folder/Python:${PATH}”
export PATH
And the folder it's there:
echo $PATH
“/Users/Robyc/Project_Folder/Python:/Library/Frameworks/Python.framework/Versions/3.5/bin:/opt/local/bin:/opt/local/sbin:/usr/local/mysql/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/TeX/texbin”
My sys.path variable
['/Applications/PyCharm CE.app/Contents/helpers/pydev', '/Applications/PyCharm CE.app/Contents/helpers/pydev', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python35.zip', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages', '/Users/Robyc/Project_Folder/Python', '/Users/Robyc/Project_Folder/']
I have two problems which I have the feeling that are linked to each other.
if I type bacthProc.py in the terminal I get Command not found. I need to go to ~/Project_folder/Python
if I run ~/Project_folder/Python/batchProc.py the code starts but I get ImportError: No module named Python.
I really don't understand what's wrong with my configuration.
[*] my ~/Project_folder was already there before, but I don't remember how I added it...
My PYTHONPATH is empty. I now used a pth file as suggested in the
documentation to add my ~/Project_folder/Python folder to the
sys.path, i.e. I created the following file:
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/svn_code.pth
which contains:
/Users/Robyc/Project_folder/Python/
For the sake of completeness I share the solution to the problem.
The problem was, as expected, in the PATH variable:
the text editor (TextEdit.app) I used to modify the .bash_profile modified the quotation marks ("...") in a way not correctly recognized by the shell.
This can be actually seen in the path I posted in the question above: the quotation marks shouldn't appear when doing echo $PATH in the terminal.
As a result the PATH variable was not properly set. That's why it wasn't possible to call python anymore.
Solution: open the bash_profile with nano and modify the quotation marks...
and never use again TextEdit to modify the bash_profile

How to have Matlab recognize my $PATH?

I am using the GraphViz interface for Matlab (link), and noticed that Matlab's calls to the shell (e.g., via system or !) return with errors (command not found) when attempting to call graphviz or neato or other related names, which are perfectly valid when called from my own default shell (bash, using Terminal on OSX).
Well, I installed graphviz to my system using homebrew, so it should work fine -- I tested !brew -v from Matlab and it doesn't work either! So, I checked my path. In bash, echo $PATH returns
/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin
while in Matlab, !echo $PATH returns /usr/bin:/bin:/usr/sbin:/sbin
So, this is potentially causing my problems. That said,
1) How can I have Matlab either automatically sync the correct path, or do so once-per-startup? (i.e., so I could put the code into startup.m)
2) Can someone diagnose what causes Matlab to not find the correct environment variables, and whether this is a global problem or likely specific to my configuration?
Many thanks!
The most likely explanation for what you are seeing is that Matlab is being run on your OSX distribution under a different user than your own profile.
When you do an echo $PATH from Matlab, it is showing you the PATH for the user under which Matlab is running. However, when you do echo $PATH from the command line, it is showing you the PATH for your user profile.
To correct this, please investigate the way you are running Matlab, and make sure you are running it as yourself (or as a user who has the correct PATH settings).
Update:
I downloaded Matlab 8.5 on my Fedora box at home. I was unable to run it unless I did so using sudo su followed by ./matlab. What I observed is that Matlab caches the value of the PATH variable of the Linux user who launched it for the entire session. Changing the PATH during a Matlab session did not change the output of !echo $PATH in Matlab.
Please try to reproduce the following on your setup:
Type sudo su from a Terminal window
echo $PATH and record what you see
./matlab Start Matlab
!echo $PATH from within Matlab. What you see should match Step 2.
Kill Matlab, and do export PATH=$PATH:/newpath/ from the same Terminal
Repeat steps 3 and 4, and you should see the new PATH value in Matlab

Cant call PDFtk from Matlab, but can from terminal

For some reason, when I try to call PDFtk from Matlab (pdftk *.pdf cat output combFile.pdf), I get a /bin/bash: pdftk: command not found error, but I can run the same command in terminal in the same directory with no problem. I have restarted my system, but that did not seem to help. I am running Mac OSX 10.9.1 and Matlab 2013b. I do not want to use the absolute path to PDFtk, because it needs to be cross-platform compatible.
EDIT: This may help. When I echo $PATH in Matlab I get /usr/bin:/bin:/usr/sbin:/sbin. When I do it in terminal, I get /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin. Then I did which pdftkand it returned /usr/local/bin/pdftk Is there anyway to add the /usr/local/bin/ path to Matlab?
I believe that the export PATH idea would probably be better for a single system (note: I have not tested it), but I needed my script to be used on several Mac and Linux machines that are out of my control. This is what I ended up using (yes, I know that this will break on Windows, but that is ok)
if(ismac)
system('/usr/local/bin/pdftk myfig[0-9][0-9].pdf cat output myfigCombined.pdf');
else
system('/usr/bin/pdftk myfig[0-9][0-9].pdf cat output myfigCombined.pdf');
end
Originally, I was using if(isunix) for the second command, but presumably because of Mac's unix architecture both commands were being executed.
EDIT: I was able to test it on Linux and it worked perfectly. I suppose this would be the syntax for Windows, but I do not have access to a Windows machine with PDFtk and Matlab installed, so no guarantees (also, I am not sure that I did the path quotation marks right...):
elseif (ispc)
system('"C:\Program Files (x86)\PDFtk Server\bin\pdftk" myfig[0-9][0-9].pdf cat output myfigCombined.pdf');
It seems that your $PATH environment variable is not exported to Matlab. Reading
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255609
I'd suggest to add a
export PATH=$PATH:<Path-to-your-PDFtk-binary>
in your .bash_profile

Setting an environment variable in Cygwin

I have been trying to setup a environment variable in Cygwin using the command export PRIMOSBASE=/directory/for/primosfiles.
And when i check the variable using the command echo $PRIMOSBASE it shows the /directory/for/primosfiles. hopeful this means the environment variable is set.
But when i try to run a shell script(primos) for the /directory/for/primosfiles, it shows
./primos: line 8: /prilaunch.pl: No such file or directory
chmod: failed to get attributes of `step1.sh': No such file or directory
which means i have not set the PRIMOSBASE environment. could anyone please tell me where i am going wrong...
Thanks ...
Run
echo "export PRIMOSBASE=/directory/for/primosfiles" >> ~/.bashrc
to append the command to the end of your .bashrc file, so that the variable is set each time you use Cygwin. Then run
source ~/.bashrc
to make it take effect immediately.
NOTE: Make sure you use double brackets (>>) to append. It might be a good idea to make a backup of .bashrc just in case. If you're not comfortable with I/O redirection, an alternative is to edit .bashrc with an editor. I think vim is among the default tools in Cygwin.
I had a similar issue trying to get ANDROID_HOME to work in a Cygwin window. When I used the linux path separators, as follows
ANDROID_HOME=/cygdrive/c/Users/User/AppData/Local/Android/sdk my gradlew build script complained it couldn't find the sdk in ANDROID_HOME.
I eventually discovered that I had to set my environment variable in the Windows format, including Windows path separators '\', as follows
ANDROID_HOME=C:\Users\User\AppData\Local\Android\sdk
Note: the PATH and several other environment variables set in Windows are converted into Linux format. I hope this helps others who want/need to use Cygwin + Windows + essentially Windows programs that need environment variables.

Resources