run matlab script from command line or shell [duplicate] - matlab

This question already has answers here:
How do I hide "MATLAB Command Window" when I run an m-file from command line?
(4 answers)
Closed 7 years ago.
Is there a way to run matlab code from command line or linux shell? If yes, please also let me know what OS you are using. I searched online and found several 'answer' but does not work under my desktop (win 7).
I tried following code in cmd matlab -nodesktop -nosplash -nodisplay -nojvm -r "a=[3,4,5,6,6]; dlmwrite('test', a);". Although the code worked and write out the file under current folder, but there is this white backgrounded window open up. Can it just run quietly and when finished, do not show this window?

Yes, you can call matlab from command prompt. In a windows machine it will look like this depending on your matlab installation path:
"C:\Path\to\matlab\matlab.exe" -r matfile.m
But it does open up Matlab gui. I do not know how to run it silently, but including
exit
at the end of your routine will close it automatically.

Related

how do I mimic the terminal path when making a system call in MATLAB on a mac

I'm trying to automate mac terminal calls in MATLAB. In my specific use case I used brew to install cmake but in MATLAB cmake isn't recognized [~,result] = system('cmake ..'); returns zsh:1: command not found: cmake
Using the following I am pretty sure I could update the path so that cmake is recognized.
(Mac,Matlab,bash) Changing the PATH of bash in Matlab for system commands
However, I was wondering if there was a generic way of mimicking the path that the terminal is seeing.
In particular when I run env in the terminal and in MATLAB using [~,result] = system('env'); the path variables are different and I'm wondering why that is and how to ensure they align.
When you start MATLAB from its icon on macOS, you never ran the login shell startup files, so most of your configuration is not loaded. That is, anything configured in ~/.zprofile, ~/.zshrc, etc. is not seen by MATLAB. Unlike other Unixes, macOS doesn’t start a login shell when you log on. See here for the differences between a login shell and a non-login shell.
One way around this is to launch MATLAB from a terminal window. Another way is to manually load the zsh configuration before running your shell command.
A cleaner solution is to avoid ~/.zprofile (loaded for login shells) and ~/.zshrc (loaded for interactive shells), and instead put your configuration in ~/.zshenv (loaded for all shells, including the one started by MATLAB for system() or !).
In particular, Homebrew adds a line
eval "$(/opt/homebrew/bin/brew shellenv)"
to your ~/.zprofile file. Moving this line to ~/.zshenv and restarting MATLAB should add the Homebrew configuration to shells started from within MATLAB.

Run MATLAB in command line allowing display of plots

To run MATLAB on Linux in the command line I use the following:
matlab -nodisplay
But I guess this does not allow windows of plots to open either, because the following command has no output:
plot(X,Y)
How to allow only graphs and plots to display when running MATLAB from the command line?
Under Linux, to run MATLAB in a terminal window use the -nodesktop option. It causes MATLAB to start without the GUI, with all text input and output to happen through the terminal window. But it does not disable the JVM software altogether and so allows interactive figure windows to be displayed.
The -nojvm option disables the JVM software, disallowing all Java-based components of MATLAB, including figure windows. The -nodisplay option disables al graphical display output, but doesn't disable the JVM.
For more details, see the documentation.

How to run MATLAB file when it is already open using a batch file?

I'm trying to find a CMD code to run a MATLAB file but when MATLAB is already running (I don't want to open MATLAB again in different window).
I'm using this code:
matlab -nosplash -r run('C:\xampp\htdocs\RCE\FYP_expirement\SpeedControl\exp1_start.m');
But every time this code is executed (from a batch file), it opens MATLAB again in a separate window.

Execution of bash script differs when runned from Matlab and Terminal

I have a bash script and I want to execute it from Matlab 2014b in Ubuntu 14.04. When I launch it from terminal (that I start from os) everything is ok. I try to launch it from Matlab like this
!./script.sh
It executes but cannot open image files that have to be loaded.
More than that if I launch terminal window from Matlab
!./gnome-terminal
and use it to launch the script I got the same 'file not found' problem. Text files are accessed with no problem. Path to the images are global. To load images opencv library is used. My guess is that for some reason opencv works different from Matlab, but I don't know what to do.
I spend a lot of time to figure out the problem, but still cannot resolve it. I will appreciate any advise or help.
LD_LIBRARY_PATH of Matlab shell differs from one of teminal shell. Following command in Matlab:
setenv('LD_LIBRARY_PATH', <content of LD_LIBRARY_PATH from terminal>)
solves the problem.
Content of LD_LIBRARY_PATH from terminal is printed by
echo $LD_LIBRARY_PATH

how to open emacs via OSX terminal [duplicate]

This question already has answers here:
How to launch GUI Emacs from command line in OSX?
(15 answers)
How to start "emacsformacosx" in terminal
(5 answers)
Closed 9 years ago.
how do you open emacs for OSX?
I have it downloaded in my Applications, but I don't know how to open it via my terminal (calling it "emacs test.cpp")..
I checked out some sites, but their solution didn't work for me.
I have this in my .bashrc (or .zshrc) file.
alias emacs="open -a Emacs.app"
ysakamoto's answer is correct.
I've taken it a step further, I'm using a shellscript using emacsclient to open a file in a running instance of Emacs, or launch a new one if there's none running. Alternatively, it just focuses Emacs/opens an empty session when I don't give it a file. Take a look.