Running external programs from MATLAB - matlab

Here is what I am trying to achieve:
run a matlab command/script that starts a unix terminal and from within that terminal starts external software. Matlab itself should be decoupled from that shell immediately.
On a Unix system, I am currently trying to start an external program from within matlab. I know that I can basically use the matlab command prompt as a terminal by adding an ! in front of every command. However then, the program's output is also displayed within on the matlab command prompt and the program is killed as soon as matlab is closed.

To start an external terminal, call that terminal emulator using the matlab system command. If gnome-terminal is your terminal:
system('gnome-terminal');
To pass parameters to the terminal use -e
system('gnome-terminal -e echo hello World');
This terminal will close immediately after it's finished running. So too keep it open:
system('gnome-terminal -e "bash -c \"echo Hello World; exec bash\""');
Hope this helps. I similar command should work for other terminal emulators beside gnome-terminal.

Related

Can I execute an Octave script from the shell in macOS?

I'm attempting to execute a script from the shell (Mac OS) using Octave, and this is exactly what I type:
$ open -a octave "my_script.m"
The result, is that Octave (the full application in the GUI) opens and doesn't run the script.
Ideally, I'd like Octave to actually run in Terminal (without the GUI), and execute whatever commands and scripts I type there, including, as noted above, an entire script stored in a .m file.
I also tried the following:
$ octave-cli my_script.m
Which produces the following error:
-bash: octave-cli: command not found
I installed Octave through a DMG file linked to on the official GNU website.
The command you're looking for is called octave-cli, and there's no need to open, just octave-cli my_script.m.

Run multiple external commands at once in Octave

I am trying to open multiple cygwin terminals and run a .exe file in each of them through octave GUI. I was able to do this in MATLAB, but the exact same code in octave does not work.
Code used :
dos(['C:\cygwin64\bin\mintty.exe /bin/bash -login ./testtorun_ig1_1.sh']);
dos(['C:\cygwin64\bin\mintty.exe /bin/bash -login ./testtorun_ig1_2.sh']);
dos(['C:\cygwin64\bin\mintty.exe /bin/bash -login ./testtorun_ig1_3.sh']);
dos(['C:\cygwin64\bin\mintty.exe /bin/bash -login ./testtorun_ig1_4.sh']);
testtorun_ig1_1 has the command to open a .exe file.
What happens in octave is, initially one cygwin terminal open and runs the .exe file. After the application completes and exits, cygwin terminal closes and opens the next cygwin terminal opens and runs the second .exe files. I want to be able to run 4 cygwin terminals at a time, which is what happens in MATLAB but not in octave
In Octave, dos waits until the external command completes before executing any more commands.
Octave waits for the external command to finish before returning the exit status of the program in status and any output in text.
If you want to evaluate the external commands asyncronously, you should use the system command with the 'async' input argument
id = system('C:\cygwin64\bin\mintty.exe /bin/bash -login ./testtorun_ig1_1.sh', 0, 'async')

Running MATLAB from Unix shell script

I am having some trouble running a MATLAB script from a Unix shell script on my Mac OS (Mountain Lion).
I know how to call the MATLAB script from the shell, and that works fine. However, the problem is that I want my shell script to then call another program after MATLAB finishes running. But when the MATLAB program finishes, the shell is "stuck." That is, it doesn't continue executing the other functions in the shell, and the Terminal window is stuck in the MATLAB environment instead of returning control to sh.
What do I do here? Here is a copy of my shell:
#! /bin/sh
echo "Please make sure the network cable is plugged in"
echo "(then press return to continue)"
sudo mv /System/Library/CoreServices/Dock.app /System/Library/CoreServices/Dock.app.bak
killall Dock
/Applications/MATLAB_R2008a/bin/matlab -nodesktop -r "run Documents/MATLAB/BLS/BLS"
sudo mv /System/Library/CoreServices/Dock.app.bak /System/Library/CoreServices/Dock.app
# OTHER COMMANDS AFTER HERE CALLING OTHER PROGRAMS ...
The two "sudo mv" commands are just making the Dock invisible during the execution of MATLAB, and bringing it back after MATLAB finishes.
The first command works perfectly, and makes the Dock invisible, then runs MATLAB. However, when MATLAB finishes running, it doesn't continue with the rest of the shell. The terminal is "stuck" in MATLAB. I have tried fiddling around with named pipes and such, but I couldn't get any of them to work.
How do I get it to continue executing the shell script after MATLAB is finished?
Have you tried adding exit to Matlab command?
/Applications/MATLAB_R2008a/bin/matlab -nodesktop -r "run Documents/MATLAB/BLS/BLS; exit;"

How to launch new terminal for each spawn command of Expect in perl

using Expect perl module, i have written script that uses ssh command on local host machine to emulate a terminal. so consider the below scenario, i have a perl script (say script.pl) which uses expect module, and have two commands of this type
$ssh = Expect->spawn("ssh -Y $loginname\#$systemname")
so the problem here is...i am using a terminal to execute script.pl and my code has two spawn commands. I noticed that two spawn commands are started within the terminal used to execute script.pl. But i want two new terminals for two spawn commands. So here there should be three terminals one for script.pl, two for each spawn commands in the script.pl.
Is there any way of achieving two new terminals from single perl script using Expect??
"Is there any way of achieving two new terminals from single perl script using Expect??"
Sure. But a terminal is an application like anything. You have to invoke it explicitly; all you are spawning is ssh. Terminal applications are heterogeneous, so you need to figure out what terminal application is running on your system and how to get it to launch an application at start-up. Eg, for the Xfce terminal something like:
$ssh = Expect->spawn("Terminal -e \"ssh -Y $loginname\#$systemname\"");
should work. Or perhaps better:
$ssh = Expect->spawn("Terminal", ("-e", "ssh -Y $loginname\#$systemname"));
However, your terminal is probably not the Xfce terminal. If you are on Windows, I believe it is cmd. On Linux it depends upon what is installed; if you are using a distro default it will be the terminal that came with the DE -- gnome-terminal for GNOME, konsole for KDE, etc.
It might make more sense, however, if you automate launching the terminals some other way and have them execute a script using expect/ssh at start-up.

Cygwin - run script silenty from "run command"

I have script lets say:
C:\foo.bsh
I want to be able to run this command via the windows run command:
Start -> Run
Windows Key + R
and type something small like 'foo' and hitting return.
However, I do not want a cmd prompt to be visible. This script does some preprocessing for an IDE. I do not want the cmd prompt to be open for the lifetime of the IDE process.
I have tried:
1) Creating a bat file with the following contents:
c:\cygwin\bin\bash --login "C:\foo.bsh" (this fails because it keeps a cmd open)
2) Converting the above bat file to an exe using bat_2_exe_converter (does not make the cmd silent)
thoughts?
EDIT: The solution so far suggests something to type from an actual cygwin shell. I am trying to get a faster solution by having something short I can type in the Windows run command. Also, the nohup command; exit doesn't automatically kill the box - however I can manually kill it without killing the IDE process. The run command accepts shortcuts (.lnk's), bat's, exe's.
Try the run.exe command of cygwin. It is a big install, a complete unix environment for your Windows machine. Suppose you installed it at c:\cygwin\.
No mystery, just run c:\cygwin\bin\run.exe <your command here> and you will have your no dos window execution.
You can run it from any DOS window (run cmd.exe from the start menu). You don't need to run it from cygwin.
To make it easier, append C:\cygwin\bin to your %PATH% env var (My Computer → Properties → Advanced → Environment Variables) (Kudos to Felipe Alvarez comment).
Now you can just type
c:\cygwin\bin\run.exe "C:\foo.bsh"
You must create a link in your Start Menu with this command so will be able to run it with Win-R.
Here is the man page of the runcommand:
$ man run
RUN(1) run 1.3.0 RUN(1)
NAME
run - start programs with hidden console window
SYNOPSIS
run [ -p path ] command [ -wait ] arguments
runcommand [ -p path ] [ -wait ] arguments
DESCRIPTION
Windows programs are either GUI programs or console programs. When
started console programs will either attach to an existing console
or create a new one. GUI programs can never attach to an exiting con‐
sole. There is no way to attach to an existing console but hide it if
started as GUI program.
run will do this for you. It works as intermediate and starts a pro‐
gram but makes the console window hidden.
With -p path you can add path to the PATH environment variable.
Issuing -wait as first program argument will make run wait for program
completition, otherwise it returns immediately.
The second variant is for creating wrappers. If the executable is
named runcommand (eg runemacs), run will try to start the program (eg
emacs).
EXAMPLES
run -p /usr/X11R6/bin xterm
run emacs -wait
runemacs -wait
run make -wait
AUTHORS
Charles S. Wilson
Harold L Hunt II
Jehan Bing
Alexander Gottwald
Version 1.3.0 November 2005 RUN(1)
You can use either...
c:\cygwin\bin\bash -l /path/to/script_to_interpret.sh
...or...
c:\cygwin\bin\bash -l -c /path/to/executable_script.sh
Note: the -l flag tell bash to "act as if it had been directly invoked by login" and use Bash Startup Files. This is important in that it sets your $PATH and other things you rely on when you launch a cygwin terminal. If you don't include -l or --login you will get "command not found" when you try to call anything except of a bash builtin.
The difference between the 2 is like the difference between doing...
bash script_to_interpret.sh
...and...
./executable_script.sh
...in *nix. The former interprets the script using bash. The latter executes the script (only if it has chmod +x executable_script.sh) and interprets it according to its "shebang" line. The latter method is also what you want to do if your executable is not a script at all, like a *nix binary compiled from source.)
It has been bugging me for a while I couldn't find the solution for this, but I finally got the right mix together.
You can simply do the following if you have cygwin on your PATH:
run bash test.js
If cygwin is not on your path, you can do this:
c:\cygwin\bin\run.exe -p /bin bash test.js
If you are looking for more control over the created window (maximize, etc) it looks like you can use cygstart also.
Sources:
- neves answer above (though that wasn't enough by itself for me personally to figure it out)
- http://cygwin.com/ml/cygwin/2008-09/msg00156.html
As the terminal can't close while your script is still running, try the command:
"nohup C:\foo.bsh; exit"
This way your script will be backgrounded and detached from the terminal, and it should exit quickly so the terminal goes away. I think that the window may still 'flash' with this approach, but the results should be better than what you're getting.
I'm running Cygwin64 and the xwin server link points to:
C:\cygwin64\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe
This creates an icon AND a notification on the taskbar. I don't like that. The icon is rather useless, the notification has all your menu options from .XWinrc.
So... I wrote a .vbs script to silently run this command and make the icon go away:
Set objShell = CreateObject("WScript.Shell")
objShell.Run("C:\cygwin64\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe"), 0
Another imperfect possibility is to run the script via a shortcut and set the shortcut's Run option to "minimized".
Go to the directory where you have installed cygwin(on my machine it is c:/cygwin64/bin)
Once there simply type "bash.exe"