Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
calling R code from MATLAB

Subject: calling R code from MATLAB

From: Lourdes Pelayo

Date: 13 Feb, 2008 20:39:04

Message: 1 of 8

Hello

I want to know if somebody knows a tool to call a COMPLETE R
 code from Matlab.

I've seen some tools/packages to call R executing line by
line, but I need to call the complete file coded in R and
after that get the result and import it to Matlab.

I have a loop in Matlab and need to get some values
calculated and R, update my data back in Matlab and loop
calling R again.

Something like this:

for (a=1:N)
     %call R to manipulate values
     %update values in matlab
end

I'd really appreciate your help

Lourdes

Subject: calling R code from MATLAB

From: Arthur G

Date: 13 Feb, 2008 21:56:40

Message: 2 of 8

On 2008-02-13 15:39:04 -0500, "Lourdes Pelayo" <loperam@yahoo.com> said:
> Hello
>
> I want to know if somebody knows a tool to call a COMPLETE R
> code from Matlab.
>
> I've seen some tools/packages to call R executing line by
> line, but I need to call the complete file coded in R and
> after that get the result and import it to Matlab.
>
> I have a loop in Matlab and need to get some values
> calculated and R, update my data back in Matlab and loop
> calling R again.
>
> Something like this:
>
> for (a=1:N)
> %call R to manipulate values
> %update values in matlab
> end
>
> I'd really appreciate your help
>
> Lourdes

What about running R in batch mode and parsing the output?
Assuming R is on your path,
system('R CMD BATCH infile outfile');
will run the code in infile and place output in outfile.

I've also been meaning to check out SAGE
http://www.sagemath.org/
which lets you interact with R, MATLAB, and other math software
with one consistent interface.

--Arthur

Subject: calling R code from MATLAB

From: Eric

Date: 14 Feb, 2008 16:37:35

Message: 3 of 8

On Feb 13, 1:56 pm, Arthur G <gorramfreak+n...@gmail.com> wrote:
> On 2008-02-13 15:39:04 -0500, "Lourdes Pelayo" <lope...@yahoo.com> said:
>
>
>
> > Hello
>
> > I want to know if somebody knows a tool to call a COMPLETE R
> > code from Matlab.
>
> > I've seen some tools/packages to call R executing line by
> > line, but I need to call the complete file coded in R and
> > after that get the result and import it to Matlab.
>
> > I have a loop in Matlab and need to get some values
> > calculated and R, update my data back in Matlab and loop
> > calling R again.
>
> > Something like this:
>
> > for (a=1:N)
> > %call R to manipulate values
> > %update values in matlab
> > end
>
> > I'd really appreciate your help
>
> > Lourdes
>
> What about running R in batch mode and parsing the output?
> Assuming R is on your path,
> system('R CMD BATCH infile outfile');
> will run the code in infile and place output in outfile.
>
> I've also been meaning to check out SAGEhttp://www.sagemath.org/
> which lets you interact with R, MATLAB, and other math software
> with one consistent interface.
>
> --Arthur

Unfortunately I don't know how to call R scripts directly from Matlab
either. I would second Arthur's suggestion that you check out Python.
I didn't know Sage interfaced with R, I would have suggested using RPy
(rpy.sourceforge.net) to call your R code and interfacing with Matlab
using Sage.

Another possibility, which may be more direct, is to use the Java
interface to R and call it directly from within Matlab
(www.omegahat.org/RSJava/).

Eric

Subject: calling R code from MATLAB

From: Lourdes Pelayo

Date: 16 Feb, 2008 04:22:02

Message: 4 of 8


> What about running R in batch mode and parsing the output?
> Assuming R is on your path,
> system('R CMD BATCH infile outfile');
> will run the code in infile and place output in outfile.
>
> I've also been meaning to check out SAGE
> http://www.sagemath.org/
> which lets you interact with R, MATLAB, and other math
software
> with one consistent interface.
>
> --Arthur
>
I tried the: system('R CMD BATCH infile outfile');
suggestion, but I wasn't able to make it work.
Could you give me an example?

thanks

Subject: calling R code from MATLAB

From: Arthur G

Date: 19 Feb, 2008 16:03:51

Message: 5 of 8

On 2008-02-15 23:22:02 -0500, "Lourdes Pelayo" <loperam@yahoo.com> said:

>
>> What about running R in batch mode and parsing the output?
>> Assuming R is on your path,
>> system('R CMD BATCH infile outfile');
>> will run the code in infile and place output in outfile.
>>
>> I've also been meaning to check out SAGE
>> http://www.sagemath.org/
>> which lets you interact with R, MATLAB, and other math
> software
>> with one consistent interface.
>>
>> --Arthur
>>
> I tried the: system('R CMD BATCH infile outfile');
> suggestion, but I wasn't able to make it work.
> Could you give me an example?
>
> thanks

First things first... Let's make sure what you're trying to do is
reasonable. If I understand you, you want to combine calculations done
in MATLAB and R. Also, you already have a file containing the R
commands that need to be executed. MATLAB and R need to exchange data,
probably by writing results to files. If this is *not* what you want,
please clarify your needs.

Second, let's make sure you can run R from MATLAB. If R is in your
path, you should be able to run (in Matlab) system('R --version'),
which on my system produces something like this:

>> system('R --version')
R version 2.6.1 (2007-11-26)
...

Do you get something sort of error message instead? On my Mac, for
example, I get this error:
>> system('R --version')
/bin/bash: R: command not found

If so, then R is not in your path. If you don't know what I mean by
path or how to add R to it, then the simplest fix is to alter the
system command to include the location of R. For example, on Windows,
it might look something like this:

>> system('"F:\Program Files\R\R-2.6.2\bin\R" --version')
R version 2.6.2 (2008-02-08)
...

On my Mac, I would use the following instead:
>> system('/Library/Frameworks/R.framework/Resources/bin/R --version')
R version 2.6.1 (2007-11-26)

Once you can confirm that R is working from within MATLAB, I can try to
help you run your actual script.

--Arthur

Subject: calling R code from MATLAB

From: Kevin

Date: 4 Apr, 2008 03:06:02

Message: 6 of 8



Arthur G <gorramfreak+news@gmail.com> wrote in message
<47bafde7$0$308$b45e6eb0@senator-bedfellow.mit.edu>...
> On 2008-02-15 23:22:02 -0500, "Lourdes Pelayo"
<loperam@yahoo.com> said:
>
> >
> >> What about running R in batch mode and parsing the output?
> >> Assuming R is on your path,
> >> system('R CMD BATCH infile outfile');
> >> will run the code in infile and place output in outfile.
> >>
> >> I've also been meaning to check out SAGE
> >> http://www.sagemath.org/
> >> which lets you interact with R, MATLAB, and other math
> > software
> >> with one consistent interface.
> >>
> >> --Arthur
> >>
> > I tried the: system('R CMD BATCH infile outfile');
> > suggestion, but I wasn't able to make it work.
> > Could you give me an example?
> >
> > thanks
>
> First things first... Let's make sure what you're trying to do is
> reasonable. If I understand you, you want to combine calculations done
> in MATLAB and R. Also, you already have a file containing the R
> commands that need to be executed. MATLAB and R need to exchange
data,
> probably by writing results to files. If this is *not* what you want,
> please clarify your needs.
>
> Second, let's make sure you can run R from MATLAB. If R is in your
> path, you should be able to run (in Matlab) system('R --version'),
> which on my system produces something like this:
>
> >> system('R --version')
> R version 2.6.1 (2007-11-26)
> ...
>
> Do you get something sort of error message instead? On my Mac, for
> example, I get this error:
> >> system('R --version')
> /bin/bash: R: command not found
>
> If so, then R is not in your path. If you don't know what I mean by
> path or how to add R to it, then the simplest fix is to alter the
> system command to include the location of R. For example, on Windows,
> it might look something like this:
>
> >> system('"F:\Program Files\R\R-2.6.2\bin\R" --version')
> R version 2.6.2 (2008-02-08)
> ...
>
> On my Mac, I would use the following instead:
> >> system('/Library/Frameworks/R.framework/Resources/bin/R --
version')
> R version 2.6.1 (2007-11-26)
>
> Once you can confirm that R is working from within MATLAB, I can try to
> help you run your actual script.
>
> --Arthur
>


Arthur,

Could you provide more information or a link to references on calling R
commands in matlab? I am interested in taking a vector (obtained from
processing already done in Matlab) and applying a shapiro-wilkes test to it.

After typing:
>> system('R --version')
I see:
R version 2.6.2 (2008-02-08)


Much appreciated!
Kevin

Subject: calling R code from MATLAB

From: Kevin

Date: 24 Mar, 2009 03:25:03

Message: 7 of 8

In response to the above, Arthur passed on the following to me via email (and encouraged me to post it here for others). Note that calc.R is your R-source file that has the info that you want to process. In the example below we are running a shapiro-wilkes test on the data obtained in matlab

In an m-file you would have:
~~~~~~~~~~~~~~~~~~~~~
data = sum(rand(100), 1);
csvwrite('data.csv', data(:)); % Write as a column
system('R CMD BATCH calc.R outputForDebugging.txt');
testResults = csvread('testResults.csv');
testResultsStruct = struct('W_statistic', testResults(1), 'p_value', testResults(2))
~~~~~~~~~~~~~~~~~~~~~

In the calc.R r-file you would have:
~~~~~~~~~~~~~~~~~~~~~
data <- read.table("data.csv", header = FALSE, sep = ",")
results <- shapiro.test(data$V1)
results2 <- c( results$statistic[["W"]], results$p.value )
write.table(results2, file="testResults.csv", sep = ",", col.names = FALSE, row.names = FALSE, qmethod = "double")
~~~~~~~~~~~~~~~~~~~~~

Subject: calling R code from MATLAB

From: Cary Opel

Date: 17 Jun, 2012 17:17:07

Message: 8 of 8

After using R(D)COM and Matlab R-link for a while, I do not recommend it. The COM interface has trouble parsing many commands and it is difficult to debug the code. I recommend using a system command from Matlab as described in the R Wiki. This also avoids having to install RAndFriends.

http://rwiki.sciviews.org/doku.php?id=tips:callingr:matlab

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us