Matlab run script on command line and block until finished - windows

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;"

Related

Perl script is running slow when calling bash

I am running a perl script that calls another script. Via command line. But it runs extremely slow. The back ticks makes it run via command line.
for my $results (#$data){
`/home/data/push_to_web $results->{file}`;
}
If i run the same command via bash /home/data/push_to_web book.txt the same script runs extremely fast. If i build a bash file that contains
/home/data/push_to_web book_one.txt
/home/data/push_to_web book_two.txt
/home/data/push_to_web book_three.txt
The code executes extremely fast. Is there any secret to speeding perl up via another perl script.
Your perl script fires up a new bash shell for each element in the array, whereas running the commands in bash from a file doesn't have to create any new shells.
Depending on how many files you have, and what's in your bash startup files, this could add a significant overhead.
One option would be to build a list of semicolon-separated commands in the for loop, and then run one system command at the end to execute them all in one bash process.

Open running instance of matlab

I am running a MATLAB script at a cluster, having used the command
matlab -nodisplay <LocalVariation.m
I can now see that it is not going to finish within the wall time I have put. Is there any way that after I have ssh'ed to the node, that I can open up the running MATLAB instance, and stop it within the program, such that I get access to the variables it has produced so far?
It seems Matlab does not allow that unless you explicitly include it beforehand in your Matlab script. See this answer for instance.
Also it is always good to make your script 'checkpointable' so that you are able to restart easily from where the script was stopped.
Finally, a word about Octave, which has the option to create a dump of the variables automatically upon wrongful termination of the program.
Assuming you are using the SLURM workload manager you can try the following.
First open an interactive bash session using srun --pty bash -i. If you check your job queue using,squeue -u <username> you should find a bash session:
Now run your Matlab code with a breakpoint where you wish to stop execution. For example, a file runTestJob.m could look something like:
function runTestJob
A = rand(5);
B = randn(5);
keyboard; % add breakpoint
end
Now run code,
srun matlab -nodisplay -singleCompThread -r "runTestJob;quit"
This should give you a Matlab access directly in the terminal, so that you can use Matlab just like you would use it in the command window:
After you are done, make sure to close the interactive session and free up resources:
scancel <JOBID>
Hope that helps!

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

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.

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.

Resources