Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How run shell script from R or/and from Matlab?

Tags:

shell

r

matlab

Let's assume that we have command X which can be executed in shell and return some result in stdout and stderr. Is it possible to do this from R or/and Matlab? And if yes, how can the result of command be handled?

like image 774
Mihran Hovsepyan Avatar asked Feb 21 '11 14:02

Mihran Hovsepyan


2 Answers

you can use the system() command to execute shellscripts, system-commands, etc in R

it's documented at http://cran.r-project.org/doc/manuals/R-lang.html#System-and-foreign-language-interfaces

like image 148
Nikolaus Gradwohl Avatar answered Oct 08 '22 23:10

Nikolaus Gradwohl


According to this post, you can call anything from any OS, using the system function. An Examples is [status, result] = system(’dir’);, to call the dir command on a UNIX-like OS.

From MathWorks documentation:

system('command') calls upon the operating system to run the specified command, for example dir or ls or a UNIX shell script, and directs the output to the MATLAB software. The command executes in a system shell, which might not be the shell from which you launched MATLAB. If command runs successfully, ans is 0. If command fails or does not exist on your operating system, ans is a nonzero value and an explanatory message appears.

[status, result] = system('command') calls upon the operating system to run command, and directs the output to MATLAB. If command runs successfully, status is 0 and result contains the output from command. If command fails or does not exist on your operating system, status is a nonzero value and result contains an explanatory message.

See Michael Katz's blog here

like image 20
glarrain Avatar answered Oct 09 '22 01:10

glarrain