How to have Matlab recognize my $PATH? - macos

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

Related

/bin/bash: No such file or directory when running a shell script from Matlab

I'm creating a Matlab code in windows 10, which in turns uses the Ubuntu environment (inside windows 10) to perform some calculations mixing Matlab and OpenFOAM.
I summary, what I do is that I have a Matlab code, which at certain moment calls a shell script function inside my ubuntu environment and this function perform everything that I need on ubuntu. If I test that .sh file inside the ubuntu environment it works flawless, but when I call it from Matlab (on windows 10) it doesn't work.
In Matlab my call is done like this:
system('bash -c "******.sh"');
and I receive the following error
/bin/bash: ******.sh: No such file or directory
even if the file exists.
Any clues?
Sorry, I was out of the country, but yesterday I was able to solve the problem.
The reason to use both software that way is that I need OpenFOAM to work in its native configuration.
That said, it seems that my problem was due to the fact that windows allows you to install bash and Ubuntu at the same time, so when I asked to Matlab to perform a system('bash -c "******.sh"') it tried to use bash whilst I had everything installed on Ubuntu. So the solution was simply to delete bash of my Windows10 installation.

Unix commands through Matlab - PATH not right?

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.

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

Too many bash initialization files?

I just installed GMT4 (Generic Mapping Tools) from MacPorts and have run into some serious issues with terminal...
When using the ls command in terminal I get the following:
-bash: ls: command not found
To fix this I've tried many things that I read many different places online. I've changed the following files to include /usr and /bin:
~/.bashrc
~/.bash_profile
~/.profile
Doing so gave me back the sudo command which I had previously lost.
However, I still am not able to use ls in a new terminal window without manually exporting the path("new session"). This has been causing problems when I'm running shell scripts that use system functions. So I'm starting to wonder what exactly those three files do and if maybe having three files doing the same thing is causing them to interfere with one another. If anyone can tell me where I need to add these PATHS that would be great; I dunno, maybe I can delete one of these files or something ELSE is conflicting. Right now I just run " export PATH=$PATH:/usr" in each new terminal window and it works for that session.
I'm running Mac OS X 10.6 on a early 2011 MacBook Pro with 4GB of RAM.
Background for those who are interested:
My end goal is to use Relax to model viscoelastic relaxation in a mid-oceanic ridge system to come up with some sort of constitutive law or power-relation between slip magnitude and the location of greatest tensional stress in the lithosphere due to normal faulting.
The Bash manual actually explains these files fairly thoroughly. However, the manual does not describe how these mechanisms are usually used in practice. What follows is a brief, abridged version of what is common best practice.
.profile is read at login by both sh and bash.
.bashrc is read by non-login Bash shells. By convention, .bash_profile sources .bashrc in turn.
.bash_profile is read at login by Bash. If this file exists, .profile is not read. By convention, .bash_profile should source .profile if it exists.

(Mac,Matlab,bash) Changing the PATH of bash in Matlab for system commands

Hello Fellow Matlab Users,
I was wondering if anyone could provide me with some insight on how to change the bash path environment in Matlab.
Basically, if I start a given application in the terminal outside of Matlab everything works fine. (e.g.: 3dMean)
However, if I try it in Matlab with the system command (system('~/abin/3dMean') (as you can see I have to tell Matlab where it is located) I get the following error:
dyld: Library not loaded: /usr/X11/lib/libpng12.0.dylib
Referenced from: /Users/kjio/abin/3dMean
Reason: Incompatible library version: 3dMean requires version 42.0.0 or later, but libpng12.0.dylib provides version 40.0.0
~/abin/3dMean: Trace/breakpoint trap
So I checked the bash PATH in Matlab, this is the output:
system('echo $PATH')
/usr/bin:/bin:/usr/sbin:/sbin
I tried to call the application with the following command:
system('env PATH=~/abin LD_LIBRARY_PATH=/usr/X11/lib 3dMean')
but that also doesn't work. I get the same error message.
Next what I tried was to change the PATH:
setenv('PATH', [TEMP; getenv('PATH') ]);
In the variable TEMP were all the paths of interest out of the Terminal $PATH.
Now I could do that
system('3dMean')
But I get still the same error message.
Is there any way to update the maci64 env or how can I change the bash PATH in Matlab to overcome this problem ??
Thanks
It looks like Matlab is linking against a different PNG library than when you run 3dMean from the command line. You probably want to change the DYLD_LIBRARY_PATH within matlab (not LD_LIBRARY_PATH) to point to a newer version of libpng
I would like to present step to step as to setting the PATH, DYLD_LIBRARY_PATH and other system variables in Matlab for CUDA on Mac (since this is what I am working on)
PATH = getenv('PATH');
setenv('PATH',[PATH ':/Developer/NVIDIA/CUDA-6.0/bin']);
libpath = getenv('DYLD_LIBRARY_PATH');
setenv('DYLD_LIBRARY_PATH',[libpath ':/Developer/NVIDIA/CUDA-6.0/lib']);
besides, the ':' before '/Developer' is very important
its function is to add a PATH instead of appending a string to the last path in the variable

Resources