How do I call Matlab in a script on Windows? - 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.

Related

How can I execute Windows commands from Perl without leaving command windows open?

I have a Perl script on Win10 that uses the system() command to run a couple of different command line processes, including:
Start Windows Media Player with a specified .mp3 file.
my $cmd = "start call wmplayer.exe myRadoShow.mp3";
system($cmd);
Start another Perl program that does something else at the same time (specifically reads and broadcasts a set of timecodes and titles).
my $cmd2 = "secondScript.pl some_params";
system($cmd2);
All of this works correctly; the minor problem is that #1 above starts up a new command line window each time it executes the system($cmd) command. I have to later go back and close those windows.
If I don't use "start call" the Perl script doesn't continue to #2.
Is there a preferred way to execute #1 that doesn't leave these windows open?
I realize this question may be more about Windows commands than Perl.
Firstly, If youre using active perl versions, there is a wperl.exe you can call your script with instead of the default perl.exe. Not to sure if this will hide sub processes created by your script but you can give it a go.
If that doesnt work, then maybe you can use Win32::GUI:
use Win32::GUI;
my $hw = Win32::GUI::GetPerlWindow();
Win32::GUI::Hide($hw);
there are other modules that provide this functionality as well. Good luck!

MATLAB scripts and fortran scripts are used in a for loop of a bash script. Can I avoid call MATLAB multiple times?

I have a bash shell script that runs multiple MATLAB and fortran codes within a for loop.
In the loop, the bash script calls MATLAB over and over again. This make the script inefficient.
I use MATLAB 2017b and Mac OS X Catalina(10.15.7).
Any suggestions?
#!/bin/bash
for i in {1..10}
do
cp initial_input_$i initial_input
matlab -nodesktop -nosplash -r "matlab1; exit;"
cp matlat1.output fortran1.input
gfortran fortran1.f
./a.out
cp fortran1.output matlab2.input
matlab -nodesktop -nosplash -r "matlab2; exit;"
cp matlab2.output fortran2.input
gfortran fortran2.f
./a.out
cp fortran2.output matlab3.input
matlab -nodesktop -nosplash -r "matlab3; exit;"
cp matlab3.output fortran3.input
gfortran fortran3.f
./a.out
cp fortran3.output final_output_$i
done
There's no built-in plain Matlab way to do this on Mac or Linux; it doesn't run in a "client/server" mode where there's a persistent Matlab process that takes multiple commands. (If you were on Windows, you could use Matlab's "COM Automation Server" mode to do this.)
The official MathWorks way to do this would probably be to use their Matlab Production Server product, which is a server that runs a pool of Matlab workers that you can dispatch calls to via a JSON API. But that's pretty heavyweight and enterprisey for your environment, doesn't run on Mac at all, and will cost you like $20,000.
If you really wanted to do this with Matlab in a client-server way, you could write a Matlab program that causes Matlab to operate as a server: Have it run a loop that waits for input by checking a directory for new files or listening on a socket, and then when new input arrives, use eval() to run it, and then returns to the loop to wait for more input. When you're all done with it, send it an exit command.
But that's kind of a lot of work, and debugging multi-process communication stuff like that is hard. So what I'd actually do in your case is just push the bash script stuff down in to Matlab M-code like Cris Luengo suggested, so that your Matlab script is in control of the whole process. The functionality of this bash script could be easily written in Matlab using the system() function and/or ! construct.

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.

Ignore user-input when running a unix command from within Matlab

I have a Matlab program that runs different unix commands fairly often. For this question let's assume that what I'm doing is:
unix('ls test')
It happens to me quite frequently that I accidentally press a key(like enter or the arrow keys) f.e. when I'm waking up my display from standby. In theory this shouldn't interfere with the unix command. Though unfortunately, Matlab will take this input and forward it right into the execution of the command. The above command then becomes something like this:
unix('ls te^[0Ast')
(Side note: ^[0A is the hex representation of the linefeed character)
Obviously, this will produce an error.
Does anyone have an idea how to work around this issue?
I was thinking that there might be a way to start Matlab with my script in a way that doesn't forward any user input from within the unix shell.
#!/bin/bash
matlab -nodisplay -nosplash -r "runMyScript();"
Can I somehow pipe the user-input somewhere else and isolate Matlab from any sort of input?
That is not very specific question, but let me try. I can see several options. I am assuming that matlab is text terminal application.
There is nohup(1) command. Since you use linux, chances are that there is non-posix version if it which says in it's man page: If standard input is a terminal, redirect it from /dev/null.
$ nohup matlab -nodisplay -nosplash -r "runMyScript();"
You can redirect /dev/null yourself
$ matlab -nodisplay -nosplash -r "runMyScript();" < /dev/null
But matlab can actually re-open it's stdin ignoring what you piped into it (for example ssh does that, you can't use echo password | ssh somewhere.
if you are running in graphics environment you may want to minimise the window, so that it does not receive any input. Probably not your case, you would figure out yourself :)
you may try to wake up by hitting "Ctrl", similar key or mouse
You may run matlab in screen(1) command disconnect from the screen or switch to different window. Screen is a program allowing you to create virtual terminals (similar to virtual desktops in GUI). If you haven't heard of screen, I suggest you to look at some tutorials. Googling for gnu screen tutorial seems to offer quite a few.

Starting a Background Instance of Matlab on Mac OS X

In Windows you can use the following command in Matlab to start a new instance of MATLAB which will run in the background (i.e. you can keep executing commands in your first version of MATLAB).
system('matlab &')
An analogous call in OSX,
system([matlabroot '/bin/matlab &'])
however results in the display of the splash image, then nothing. If I take out the ampersand, the new instance opens as expected. Unfortunately, this won't work for me, I really need to be able to control the first instance of MATLAB while the second is running.
Does anyone know why this discrepancy between the operating systems exists? By the way, I'm using OSX 10.7, Windows 7 64 bit, and MATLAB R2012a on Mac and R2012b on PC.
As some background, I'm trying to write a generic tester for an interactive command line interface that uses the input() function extensively.
Edit: I should have mentioned that the command
/Applications/MATLAB_R2012a.app/bin/matlab &
works as expected from the OSX terminal. In other words, a new instance of MATLAB opens and new commands can be entered into the terminal. So this problem seems to be specific to the system() function in OSX matlab.
Also, I tried adding that command to a bash script and calling the script from matlab, but had the same problem that I did with putting the command into the system() function.
Thanks
This is a long shot, but it might be happening because when you invoke the new instance of Matlab from Matlab with the system() command on Unix or OS X, the matlab_helper process forks and runs a shell process to run the new application. If you omit the ampersand, the shell blocks and waits for the program to finish, and system() waits for it, so the first Matlab locks up. And (here's the speculation part) if you add the ampersand, Matlab launches in the background, and then the forked shell exits, which then causes the new Matlab process to exit because its parent process (the shell) has exited. (Windows doesn't have the same parent/child process relationships, process launch mechanism, or shells, which would explain the different behavior.)
You could try prefixing the command with nohup, which protects processes from getting killed by SIGHUP, which might be what's happening here to your second Matlab process.
system(['nohup ' matlabroot '/bin/matlab &'])
You could also try using the OS X open command to launch a new independent instance. Something like this. You may need to fiddle with the options and path, but -n should be what gives you a new instance. It should be pointing at /Applications/MATLAB_R2012a.app; I'm assuming that's what matlabroot is returning on OS X.
system(['open -na ' matlabroot])
You could also try running it from the Java process-launching features from within Matlab instead of with system(). Runtime.exec() doesn't block like system() does, and there may be other quirks to system(), like the matlab_helper architecture. Try launching it with java.lang.Runtime from Matlab.
jrt = java.lang.Runtime.getRuntime();
newMatlabProcess = jrt.exec([matlabroot '/bin/matlab']);
You can try the other command line variants above using this mechanism too, and you may need to redirect stdout to /dev/null, since the new processes input and output are buffered in to that newMatlabProcess object.
You can use applescript to do this. I do something like this:
! osascript -e "tell application \"Terminal\" to do script \"cd `pwd`;matlab -nojvm -nosplash -r 'why'\""
This example opens a new Matlab instance, in the current directory, and runs the command "why". You can remove the "-nojvm" if you need java in your background Matlab process

Resources