run Matlab commands one by one on windows using .bat file - windows

I am trying to run a function on different files; I would like to use Bash-Like script to do that. When I looked on the web; I found that I can use .bat file.
My .bat file contains this
matlab -bodesktop -nosplash -r myFunction('input_1.txt')
matlab -bodesktop -nosplash -r myFunction('input_2.txt')
matlab -bodesktop -nosplash -r myFunction('input_3.txt')
matlab -bodesktop -nosplash -r myFunction('input_4.txt')
matlab -bodesktop -nosplash -r myFunction('input_5.txt')
When I double click the file, it seems that these commands are running on parallel, which, makes the PC to crash.
I looked on Matlab Forum for alternative solutions, but couldn't work with me
Another option I found:
start -wait matlab -bodesktop -nosplash -r "myFunction('input_1.txt');exit"
..
Anyone used this before ?

There are two matlab binaries, one matlabroot/bin the other in matlabroot/bin/win64/. The first one is only a launcher application which typically terminates as soon as the main application is started successfully. To keep it open until the main application terminates you have to use the -wait option with your matlab.exe (not to be confused with the start -wait option, bot can be used together).
In your case try:
matlab -wait -nodesktop -nosplash -r myFunction('input_1.txt')
(I assume you intended to use "nodesktop").
All start parameters for windows are explained here in the documentation. (You have to click "option1...optionN" to expand the relevant section.)

First check that you have the option spelled correctly. Click here for the options.
Try this:
matlab -wait -nodesktop -nosplash -r "myFunction('input_1.txt')"
Edit:
By default, when you call the matlab command from a script, the command starts MATLAB and then immediately executes the next statements in the script. The -wait option pauses the script until MATLAB terminates.
Use the -wait option in a startup script to process the results from MATLAB. Calling MATLAB with this option blocks the script from continuing until the results are generated.

Related

Matlab run script on command line and block until finished

is there a way to run a matlab script on the command line and make matlab block until the script is done?
I can invoke a single execution of the script on the command line like so:
"C:\Program Files\MATLAB\R2017a\bin\matlab.exe" -nodisplay -nosplash -nodesktop -r "run('C:\Users\myuser\profile.m');exit;"
There have been several questions on running a script in matlab in command line mode but all of them are about just invoking a single execution via cmd.
They aren't tackling the issue of executing the script multiple times sequentially or executing several scripts in order where one has to finish before the other one is started.
Matlab: Running an m-file from command-line
Open a GUI directly from desktop (Shortcut) in MATLAB environment
If I created a batch file with N repetition of the above command, they will all be started at the same time. Since I'm profiling a set of commands, I'd like them to run on their own. I could also create a script which repeats the current script N times and call this from matlab but I'd rather have matlab process shutdown between invocations so that persistent variables are cleared.
You can use the -wait option above like so:
"C:\Program Files\MATLAB\R2017a\bin\matlab.exe" -wait -nodisplay -nosplash -nodesktop -r "run('C:\Users\myuser\profile.m');exit;"

Running MATLAB script in a bash loop

I need to run a MATLAB script inside a bash loop. I'm aware of the syntax which goes as follows:
for i in "${img[#]}"
do
echo $i
matlab -nosplash -nodesktop -nojvm -r "myfunction('$i','cropped_$i');quit;"
done
It works as it should, however, I find it extremely annoying that it has to quit and open MATLAB every iteration, greatly increasing computation time. Is there any way to run it more natively without having to close/open every iteration?
Writing the loop in Matlab is clearly the superior answer, although I don't know Matlab well enough to present that as an answer. Another option is to construct the Matlab code dynamically.
for i in "${img[#]}"; do
code+="myfunction('$i', 'cropped_$i');"
done
code+="quit;"
matlab -nosplash -nodesktop -nojvm -r "$code"

Running Matlab .m file with arguments in a bash script

I have a Matlab .m file that takes three input arguments:
calculate('input.txt','Alex','output.txt')
I would like to run this .m file in a shell script as follows:
matlab -nodisplay -nodesktop -r "run
calculate('input.txt','Alex','output.txt)"
Unfortunately, this did not work. I get the following error:
Error using run (line 70)
calculate('input.txt','Alex','output.txt') not
found.
Any pointer as to how I can give the input arguments/variables?
Thanks.
Note: The following did not work either - complaining too many arguments.
matlab -nodisplay -nodesktop -r "run calculate input.txt Alex output.txt"
I think you just need to remove run. run is for scripts, not for functions (and anyway it's not necessary unless you need to specify the script's path). So, try
matlab -nodisplay -nodesktop -r "calculate('input.txt','Alex','output.txt')"
If your function calculate is not in Matlab's path, change to its folder first. For example
matlab -nodisplay -nodesktop -r "cd 'c:\users\Alex\SomeFolder'; calculate('input.txt','Alex','output.txt')"

Calling Matlab/Psychtoolbox from the Shell or from a Makefile does not give the same behavior ! Why?

I thought a Makefile was just executing the stated shell commands, but things seem not that simple :
sample.m is a minimal matlab program that displays a word on screen.
When launched from a Makefile, it does not behave the same as when launched from the shell...
From the shell
alex:~$ matlab -nosplash -nodisplay -r "sample"
-> Displays the word correctly
From a Makefile
all :
matlab -nosplash -nodisplay -r "sample"
alex:~$ make
-> Displays the word with a blue bounding box
How can it be different ?
I'm using Matlab 2010a on a Ubuntu 10.04 machine.
No arguments are passed to the sample.m script.
Did you make sure you quit Matlab after running the script?
The Matlab console will linger in memory, invisible when run from make, and some resources will remain locked.
try this in your Makefile:
matlab -nosplash -nodisplay -r "sample; exit"
I tested sample.m and it works here.

How do I call Matlab in a script on Windows?

I'm working on a project that uses several languages:
SQL for querying a database
Perl/Ruby for quick-and-dirty processing of the data from the database and some other bookkeeping
Matlab for matrix-oriented computations
Various statistics languages (SAS/R/SPSS) for processing the Matlab output
Each language fits its niche well and we already have a fair amount of code in each. Right now, there's a lot of manual work to run all these steps that would be much better scripted. I've already done this on Linux, and it works relatively well.
On Linux:
matlab -nosplash -nodesktop -r "command"
or
echo "command" | matlab -nosplash -nodesktop
...opens Matlab in a "command line" mode. (That is, no windows are created -- it just reads from STDIN, executes, and outputs to STDOUT/STDERR.) My problem is that on Windows (XP and 7), this same code opens up a window and doesn't read from / write to the command line. It just stares me blankly in the face, totally ignoring STDIN and STDOUT.
How can I script running Matlab commands on Windows? I basically want something that will do:
ruby database_query.rb
perl legacy_code.pl
ruby other_stuff.rb
matlab processing_step_1.m
matlab processing_step_2.m
# etc, etc.
I've found out that Matlab has an -automation flag on Windows to start an "automation server". That sounds like overkill for my purposes, and I'd like something that works on both platforms.
What options do I have for automating Matlab in this workflow?
matlab -nosplash -nodesktop -r "command"
works on Windows as well. Yes, it opens another window, but it's not a problem. I run it in a batch mode from Java wrapper on Tomcat server and there were no issues. Put all commands into a script file, don't forget to close the session with exit command, and use -r flag. Also you can find -noFigureWindows and -wait parameters useful. And it works on both Windows and Unix. You can use platform-specific flags, if some are not supported they will be ignored.
Start MATLAB program (Windows platforms)
There is also a way to hide matlab window with C library. See engSetVisible.

Resources