Cant call PDFtk from Matlab, but can from terminal - macos

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

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.

Running Octave-cli on Iterm/Zsh/Omgzsh in Mac OSX

It feels impossible to get it to run on any custom terminal. I know that octave-cli.app is there but it always opens in the standard terminal. Any ways to execute octave scripts like a compiler (or) run it interactively like an interpreter from Iterm?
Using Mac OSX 10.9+
Edit:
I know how to export path variables. But having searched the web can't find a way to do it. Is it even possible? I even tried it using homebrew to no avail.
You can see the content of octave-cli.app, it's a script. Mine goes like this
open -a Terminal.app /usr/local/octave/3.8.0/bin/octave | logger 2>&1
It specify the terminal application used to open octave. This is the reason of your problem, as I think.
The solution is linking octave-cli in system path, better locates at "/usr/local/bin". like
ln -s /usr/local/octave/3.8.0/bin/octave-cli /usr/local/bin/octave-cli
Finally, octave can be accessed via any terminal(like iTerm) or shell(bash, zsh) by just type "octave-cli" command, which will be searched in system path and found to executed directly.

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

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.

Is there anything similar to binfmt-support on mac OS X?

What I wish to know is whether there is something similar to binfmt-support on mac.
I have a nacl binary executable and I wish to make mac aware of the executable format so thatI can run this executable from command prompt directly.
I have seen people using binfmt-support on linux to achieve this.
Just to explain what I want in more simple terms - Say I have a.nexe file. I can run this file on mac terminal with command "../tools/sel_ldr_x86_32 -B ../tools/irt_core_x86_32.nexe a.nexe"
Instead of this big command, I wish to simply run it like "./a.nexe" and my shell file should then get invoked which would eventually call the command like "../tools/sel_ldr_x86_32 -B ../tools/irt_core_x86_32.nexe a.nexe"
If the file itself is binary, so you can not add a #! to the start of the file, then so far as I know you only have one option; create an alias for the command line in your ~/.bashrc file:
alias nacl='/path/to/sel_ldr_x86_32 -B /path/to/irt_core_x86_32.nexe'
which would then let you type nacl a.nexe at the command line.
NOTE: You will want to use absolute paths in your alias for this to work correctly.

Resources