I have been trying to call matlab from Terminal (I have a Mac) and have it simply run a program for me, without display or anything, just the program. This program both displays graphs and writes a text file for me to use. I have found several of the previous answers on this question, and so I have tried:
matlab -nodisplay -r foo matlab -nodisplay -r foo.m matlab -nojvm -nosplash -nodisplay -r foo;quit; matlab -nojvm -nosplash -nodisplay -r "foo;quit;" matlab -nojvm -nodesktop -r "foo;quit;" matlab -nojvm -nodesktop -r "foo" matlab -nojvm -nodesktop -nosplash -r foo Pretty much every time I try it, I keep getting the same response (when the line just doesn't cause the prompt to say "screw you" and give up): the actual MATLAB program window will open, and it will stay open. It will interact normally. And command line won't do anything until I close out the window. The program I want doesn't run. The window just... sits there.
If it helps, this is how I have matlab coded in my bin:
#!/bin/bash /Applications/MATLAB_R2015b.app/bin/matlab I have no clue what is going on. Any help would be greatly appreciated.
Your bash script for calling Matlab will not pass any arguments to the Matlab executable. When you type
$ matlab -nodesktop -nosplash -r "foo" what is actually called is
$ /Applications/MATLAB_R2015b.app/bin/matlab without the arguments. There are several ways you can fix this whilst retaining the ease of just calling matlab. Alternatively you could call the full path to matlab like
$ /Applications/MATLAB_R2015b.app/bin/matlab -nodesktop -nosplash -r "foo" matlab ExecutableGiven that you have already written a bash script to call matlab the easiest solution is to alter it to include the $@ bash wildcard like
#!/bin/bash /Applications/MATLAB_R2015b.app/bin/matlab "$@" The $@ wildcard passes all of the parameters you use, like -nodesktop -nosplash -r "foo" to the matlab executable so what is actually called now is
$ /Applications/MATLAB_R2015b.app/bin/matlab -nodesktop -nosplash -r "foo" I recommend you place your matlab bash script in /usr/local/bin and ensure that /usr/local/bin is in your PATH. The /usr/local/ directory is for user installed scripts as opposed to system installed scripts. You can check what directories are in your PATH with
$ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin and you should see an output similar to the above with /usr/local/bin present. The bash script should also be executable. You can set this with
$ sudo chmod +x /usr/local/bin/matlab Note: OS X El Capitan places strong restrictions on where scripts can be installed via its new System Integrity Protection feature.
matlab Another method similar to the creation of the bash script is to create a symbolic link to the matlab executable. This again should be placed in /usr/local/bin
$ cd /usr/local/bin/ $ ln -s /Applications/MATLAB_R2015b.app/bin/matlab matlab Also for this method you need to make sure that /usr/local/bin is in your PATH.
matlab to the PATH An alternative method is to simply add the directory where the matlab executable resides to your PATH. You can do this by modifying your .bash_profile (or .bashrc) file. Your .bash_profile file resides in your home directory at ~/.bash_profile. It is executed every time your user opens the Terminal. To add matlab to the PATH simply append
export PATH=$PATH:/Applications/MATLAB_R2015b.app/bin/ to it. Now you can call matlab with
$ matlab -nodesktop -nosplash -r "foo" and this will locate the matlab executable in /Applications/MATLAB_R2015b.app/bin/ and call it with
$ /Applications/MATLAB_R2015b.app/bin/matlab -nodesktop -nosplash -r "foo" After you modify your .bash_profile file you need to reload it with
$ source ~/.bash_profile or restart the Terminal for the changes to take affect.
Note: I prefer to modify the .bashrc file instead of .bash_profile because I use .bashrc on Linux too. I have set my .bash_profile file up to load my .bashrc file
$ cat .bash_profile # Load .bashrc if it exists test -f ~/.bashrc && source ~/.bashrc Note: If you want matlab to be available for every user and not just your user you need to add
export PATH=$PATH:/Applications/MATLAB_R2015b.app/bin/ to the system-wide /etc/profile file.
matlab The last method I'm going to mention is creating an alias for matlab. We do this by again modifying our .bash_profile (or .bashrc) file and appending
alias matlab="/Applications/MATLAB_R2015b.app/bin/matlab" to it. Again, after making changes we need to reload it with
$ source ~/.bash_profile or restart the Terminal for the changes to take affect. And, if you want matlab to be available for every user and not just your user you need to modify the system-wide /etc/profile file.
matlab from the TerminalNow that we've set up matlab to conveniently execute from the Terminal with the simple command
$ matlab we can look at actually executing scripts. To execute a Matlab script we first need to be in the directory where the script resides or it could be in our Matlab PATH. I'll assume it is not in your path and so we'll cd to the correct directory
$ cd /path/to/foo.m Now to execute matlab without the desktop MathWorks tells us to use -nojvm -nodisplay -nosplash but if we use -nojvm and/or -nodisplay we won't be able to display graphs. So we drop -nojvm and replace -nodisplay with -nodesktop and use -nodesktop -nosplash. This will launch Matlab without a display and allow us to display graphs. The correct command then to execute matlab without the full desktop GUI whilst also allowing us to display graphs is
$ matlab -nodesktop -nosplash Now you can use the Terminal (command prompt) as the Matlab command window and execute commands as normal. For instance we could call foo
>> foo Alternatively, we can use the -r option for the matlab executable to pass in commands for Matlab to execute. These must be quoted correctly and valid Matlab syntax. So our command to start Matlab with our previous options and to execute the script foo.m becomes
$ matlab -nodesktop -nosplash -r "foo" Aside: If, for example, we were to use
$ matlab -nodesktop -nosplash -r "foo; exit;" (note the use of exit;) this would start Matlab, execute foo.m, display the graphs and then exit Matlab closing the graphs too.
I think the bash script ignores the command line arguments. Could you try the following?
/Applications/MATLAB_R2015b.app/bin/matlab -nosplash -nodisplay -r "run foo.m;quit;"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With