How to send commands to command window programmatically in MATLAB? - matlab

In matlab I can change to another shell by the bang (!) Notation.
Example:
I enter a conda Environment in MATLAB by the following command:
!cmd '"%windir%\System32\cmd.exe" /K ""C:\Program Files\Anaconda3\Scripts\activate_<conda-env-name>.bat" "C:\Program Files\Anaconda3""'
My MATLAB Command window then Displays following:
(<conda-env-name>) U:\some_starting_path>
Now, is there a way to send commands to this newly entered shell in a programmatic way, so that that command is evaluated in that very shell's syntax and not as a MATLAB-command?
For example, how can I write code that will execute a Python command without needing to enter it manually into the command line?

Not using the ! command or system(). Those are "one and done" functions.
But you can use Java's java.lang.Process API from within Matlab to control and interact with an ongoing process.
function control_another_process
pb = java.lang.ProcessBuilder(["myCommand", "myArg1", "myArg2"]);
proc = pb.start; % now proc is a java.lang.Process object
stdin = proc.getOutputStream; % Here's how you send commands to the process
stdout = proc.getInputStream; % And here's how you get its output
stderr = proc.getErrorStream;
% ... now do stuff with the process ...
end
You can use this with a shell, with python, or any other command.
Here's a Matlab class that wraps up the Java code to make it convenient to work with in Matlab: https://github.com/apjanke/janklab/blob/master/Mcode/classes/%2Bjl/%2Butil/Process.m

Related

Is there an IPython equivalent to Bash's semicolon?

I use IPython as my interactive shell and I almost always run two commands at the start - something like:
%run helper_functions.py
data = import_data('file_with_data')
I would like a notation to run these two at the same time (so I could recall them with an "up" arrow from history) akin to what echo 1 ; echo 2 does in Bash.
I am aware that I can start IPython running with a script as per How can I start IPython running a script?.
Semicolon is a valid statement separator in Python, the same as the newline, so you can use it with regular commands:
a=5; a=a+1; print "This is a: %s" % a
Anyway, as pointed out by tobias_k, this is not working with "run" which is a special IPython macro (see the IPython built-in magic commands) and changes the command line interpretation. Instead of %run helper_functions.py; data = import_data('file_with_data'), you could use something almost equivalent:
__builtin__.execfile('helper_functions.py') ; data = import_data('file_with_data')
You don't have the trace in IPython, but still your script is executed and the variables are in your interactive namespace.

Running a MATLAB script from Notepad++

Is there a way of running a MATLAB script from Notepad++?
Obviously I have MATLAB installed on my computer. I know you can set a path for Notepad++to run when you hit F5, but when I set this path to my MATLAB.exe file, it simply opens another instance of MATLAB.
This is not what I want, I want the actual script in Notepad++ to be executed in the already open and running instance of MATLAB.
I'm afraid I'm not on my home computer at the moment to test this out, so the following is just a suggestion for you to try.
If you take a look at the NppExec plugin for Notepad++, you'll see that with it you can specify a command to be run when you hit F6 (like an enhanced version of hitting F5 in the regular Notepad++). You can also give it variables such as the path to the current file, and the name of the current file.
MATLAB (on Windows at least - I assume you're on Windows) makes available an API over ActiveX/COM. If you search in the MATLAB documentation for details, it's under External Interfaces -> MATLAB COM Automation Server. By running (in MATLAB) the command enableservice('AutomationServer') you will set up your running instance of MATLAB to receive instructions over this API.
You should be able to write a small script (perhaps in VBScript or something similar) that will take as input arguments the path and filename of the current file in Notepad++, and will then connect to a running instance of MATLAB over the COM API and execute the file's contents.
Set this script to be executed in NppExec when you hit F6, and it should then run the current file in the open instance of MATLAB.
As I say, the above is just speculation as I can't test it out right now, but I think it should work. Good luck!
Use NppExec add-on and press F6, copy paste the following and save the script:
NPP_SAVE
set local MATPATH=C:\Program Files\MATLAB\R2015a\bin\matlab.exe
cd "$(CURRENT_DIRECTORY)"
"$(MATPATH)" -nodisplay -nosplash -nodesktop -r "try, run('$(FILE_NAME)'),
catch me, fprintf('%s / %s\n',me.identifier,me.message), end"
then run (press F6; enter). Matlab Console and Plot windows still open and stay open. Error messages will be displayed in opening Matlab command window. Adding
, exit"
to the last command will make it quit and close again. If you want to run an automated application with crontabs or the like, check Matlab external interface reference for automation.
matlab.exe -automation ...
Also works in cmd terminal, but you have to fill in the paths yourself.
This is a usable implementation upon Sam's idea. First, execute MATLAB in automation mode like this.
matlab.exe -automation
Next, compile and execute this following VB in NppExec plugin. (which is to use MATLAB automation API)
'open_matlab.vb
Imports System
Module open_matlab
' connect to a opened matlab session
Sub Main()
Dim h As Object
Dim res As String
Dim matcmd As String
h = GetObject(, "Matlab.Application")
Console.WriteLine("MATLAB & Notepad++")
Console.WriteLine(" ")
'mainLoop
while True
Console.Write(">> ")
matcmd = Console.ReadLine()
' How you exit this app
if matcmd.Equals("!!") then
Exit while
End if
res=h.Execute(matcmd)
Console.WriteLine(res)
End while
End Sub
End Module
Then you'll get a matlab-like terminal below your editor. You can then code above and execute below. type !! to exit the terminal.
What it looks like
Tips: don't use ctrl+c to interrupt the MATLAB command, because it will kill the whole process instead.

how to communicate with uci protocol using matlab

I'm looking for a method to communicate with a chess engine with uci protocol using matlab.
The chess engine is rybka and its an exe file. When I run the rybka.exe, I can communicate via dos command prompt but I want do that via matlab.
I think I have to use streampipe and stdin and stdout but I don't know how use it.
I found this code in Python and it works fine but I'm looking for a matlab version:
import subprocess, time
engine = subprocess.Popen(
'a.exe',
universal_newlines=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
def put(command):
print('\nyou:\n\t'+command)
engine.stdin.write(command+'\n')
def get():
# using the 'isready' command (engine has to answer 'readyok')
# to indicate current last line of stdout
engine.stdin.write('isready\n')
print('\nengine:')
while True:
text = engine.stdout.readline().strip()
if text == 'readyok':
break
if text !='':
print('\t'+text)
If it's just a case of using the exe file and capturing the output you can use the system command to capture the output. For example I can run the system's dir command in the following way:
>> [~, output] = system('dir')
output =
ant ant.cmd antRun.bat antenv.cmd envset.cmd runant.pl
ant.bat antRun antRun.pl complete-ant-cmd.pl lcp.bat runant.py
Documentation: http://www.mathworks.com/help/matlab/ref/system.html
See also: Running C program's executable from Matlab and getting the output

How : MATLAB script that can get arguments from Unix command-line

Just for the sake of concreteness, consider the following super-simple Python script, in a file called add_em:
#!/usr/bin/env python
# script name: add_em
from sys import argv
x = int(argv[1])
y = int(argv[2])
x_plus_y = x + y
print '%d' % x_plus_y
I can now run this script, and pass arguments to it, from the Unix command line, like this:
% python add_em 3 8
11
If I make the script executable, I don't even need to mention python on the command line:
% chmod +x add_em
% add_em 41 -29
12
Can someone show me how to write (and run) a MATLAB script so that it performs exactly like the script above does? In particular, it must be able to read its arguments from the Unix command line (as opposed to, e.g., the MATLAB GUI's "command line"), and print their numerical sum to standard output.
NOTE: this script need not be "stand-alone"; IOW, one may assume that MATLAB is locally installed, and even that one can mention matlab in the command line (analogously to the first form above, where the python interpreter is explicitly invoked on the command line).
Thanks!
PS: Needless to say, this script is the antithesis of "robust", but my aim was to produce a readily conveyed example.
You can have a MATLAB function doing what you want inside add_em.m
function add_em(x, y)
x_plus_y = x + y;
disp(x_plus_y);
exit;
and then call it from Unix command line using the -r switch. Example:
matlab -nodesktop -nojvm -nosplash -r "add_em(3, 8)"
The - options suppress desktop, java and splash so the script/function will be executed without additional overhead.
You can additionally suppress the MATLAB welcome/copyright message by redirecting output to a logfile (for any computations) or tailing, for example, the output in order to get something printed on terminal
matlab -nosplash -nodesktop -nojvm -r "add_em(3, 8)" | tail -n 3
Update: Just found out this post/answers with relevant info: suppress start message of Matlab

Writing log statements to standard output with Matlab

We're starting Matlab from our Jenkins buildserver. As the build may take some time it would be nice to get some log-outputs while matlab is running. Is there a way to print text to standard output? disp, fprintf and java.lang.System.out.printline only write to the matlab console, not to standard output.
Using a logfile or a pipe won't help, as Jenkins only reads from standard-output during a build step.
How can we write log-statements to the standard output while matlab is running?
EDIT:
We're running Matlab 2010b on Windows
Depending what you are doing with Matlab you could probably launch it in command line without GUI. I used this on a server and it behaves pretty much like a shell script and writes to standards outputs.
See the startup options.
I used the following:
/path/to/matlab -nojvm -nodisplay -nosplash -nodesktop -r /path/to/mfile
EDIT: forgot to mention one very important little detail, place an exit command at the end of your mfile or Matlab will hang there waiting.
It seems that the combination of -wait and -log (not -logfile) clones the command window output to the parent console's stdout, but only if you call the MATLAB executable in [MATLABROOT]\bin, not [MATLABROOT]\bin\win64 (the subdirectory for current arch).
Tested on Windows with R2015b and R2016b:
C:\MATLAB\bin\matlab.exe -wait -log
NOT
C:\MATLAB\win64\bin\matlab.exe -wait -log
Remember to put an exit/quit in your script if you are running with -r.
The only trouble is that I can't seem to find any documentation for the -log option! Meh.
There don't seem to be any good ways to do this from within MATLAB. The easiest way I can think of doing this is by using a shell script. You could write a small shell script which would simply print any input to stdout, and then call that shell script from within matlab using the unix (or system) commands. Jenkins should be able to read the command-line output of the script and work with that.
I figured out a way to do this and am also doing it for Jenkins Matlab interface on windows.
Basic idea is that you will use diary command, but then tail -f the file, but you need a smart way to kill the tail command if you open multiple matlab instances because there will be name collisions. So the method I'm using is to name the file log.txt where the PID used is MATLAB's PID it is using when it opens.
There is an undocumented feature in MATLAB that allows you to get its PID. So now, both your batch file and MATLAB know the PID without having to read/write to a random text file that will get messy when executing multiple jobs. So the PID you use that as your unique identifier. The PID of "tail -f" is also used by MATLAB to kill tail -f to make the batch file die and is found by MATLAB using the commandline details associated with the process invocation since it uses again the unique PID log file name.
This uses some wmic commands and needs Windows Vista/7 or above. With XP you probably have to work harder to get the process ID's but should be still possible.
Here is what to do:
1) Get gnu awk for windows: http://gnuwin32.sourceforge.net/packages/gawk.htm
2) Get tail.exe from windows resource kit: http://www.microsoft.com/en-us/download/details.aspx?id=17657
3) Make sure tail and awk are in your path (the windows resourece kit I don't think automatically puts them in the path)
3) Create a batch file called matlabrun.bat as follows, (note: you need the #echo off, also the entire command is quite long, scroll right..)
#echo off
wmic process call create "c:\matlab\bin\win64\matlab.exe -r \"cd('c:\jenkins\workspace\test'); workdir=pwd; outpath=[pwd '\output'] ; try; run('C:\MATLAB\work\test_run'); end; quit; \" " | findstr ProcessId | awk "{print $3}" | awk -F";" "{ print $1 }"
4) Create another batch file called run.bat with:
for /f %%i in ('matlabrun.bat') do (
echo MATLAB Log... > log%%i.txt
tail -f log%%i.txt
set logfilename=log%%i.txt
goto next
)
:next
del /f %logfilename%
5) The run.bat file will execute matlabrun.bat and since -wait is not passed, matlab will immediately return to the command line and execute the tail -f command. That will block the batch file from completing until you kill it. matlabrun.bat returns the PID of matlab.
6) Another important note: since you are using "wmic process create" which will provide you with a PID that MATLAB is using, but will default to a working directory of c:\windows\system32. So that is why I pass the work directory to matlab. wmic process create is also a bit particular about what parameters you put into your command string for matlab to run. So it appears to have a problem with using commas in your command string. So I suggest not to use those, or figure out how to escape them (it might be that ^, works, but I just removed my commas anyway in my matlab run command).
6) The "test_run.m" file contains the following code to write to the correct log file and to kill correct tail -f instance.
matlabpid=feature('getpid');
filename=['log',num2str(matlabpid),'.txt'];
filenamefull=[workdir,'\',filename];
diary(filenamefull);
disp('Script starting...')
%%% put your code here %%%
disp('Script completed...');
diary off;
%%% FIND PID of tail.exe and kill it
%%% by using the name of the log file in the process command line
[a,b]=dos(['wmic process get Commandline,ProcessId']);
C=textscan(b,'%s','delimiter','\n');C=C{1};
for jj=1:size(C,1),
if strfind(C{jj},filename),
D=textscan(C{jj},'%s');D=D{1};
dos(['taskkill /f /pid ',D{4}]) %kills tail.exe which is the log watcher
break
end
end
7) You start it by doing run.bat. It will go and execute matlab, then start tailing the output while MATLAB runs in real-time. Then when done it will delete the log file.
8) My directory structure / files are in these locations (I'm using win7 64bit):
c:\jenkins\workspace\test\tail.exe
c:\jenkins\workspace\test\awk.exe
c:\jenkins\workspace\test\matlabrun.bat
c:\jenkins\workspace\test\run.bat
c:\matlab\work\test_run.m
c:\matlab\bin\win64\matlab.exe
If you are using 32bit matlab, point it to the win32 directory. To get the correct PID, you need to specify the actualy matlab.exe binary in the win32 or win64 directory.
You can do this by pointing the -logfile option to the Jenkins log file. Something like the following:
"C:\path\to\matlab.exe" "-r" "functionToRun" "-logfile" "%JENKINS_HOME%\jobs\%JOB_NAME%\builds\%BUILD_NUMBER%\log" /wait
You can use the diary mode. Not sure if it will fit your specific implementation.
http://www.mathworks.com/help/techdoc/ref/diary.html
I didn't find a real solution. Mathworks created some wrapper tool. But this will only output the results after matlab has exited. You won't get any ouput during execution.
http://www.mathworks.de/support/solutions/en/data/1-ACT3YN/index.html?product=ML&solution=1-ACT3YN
So I'll have live without real live-output...
Or try using '-logfile' option in matlab.
matlab.exe -nodisplay -nosplash -nodesktop -wait -logfile logfile.txt -r "try script.m ;catch err; disp(err.message); end ; exit"
I prefer using bash (Execute shell) in Jenkins, then you can tail the log-file while matlab is running.
matlab.exe <...> &
matpid=$!
tail -f logfile.txt &
tailpid=$!
wait $matpid
matexit=$?
kill $tailpid
sleep 1 # Just to make sure kill is done before Jenkins step ends and no zombie processes
exit $matexit