How to run an R function from MATLAB

조회 수: 141 (최근 30일)
Daniel
Daniel 2015년 7월 24일
댓글: Haris K. 2024년 4월 16일
Hi,
I have written some code in R and for various reasons, I can't rewrite it in MATLAB easily. I was wondering if there is a way to call that R function from a MATLAB script, run it, and import the results of that function back into MATLAB.
Thanks

답변 (3개)

Cindy Solomon
Cindy Solomon 2015년 7월 27일
편집: Cindy Solomon 2015년 7월 27일
Hi Daniel,
There is a File Exchange function here that appears to have this functionality (calling an R-command, copying variables to MATLAB, etc). From looking at the comments, a few users have had some difficulty with the COM connection, so it is possible to use the "system" command as well, which lets you execute commands from the OS in MATLAB. For more information on that function, see its doc page here.
I hope this helps!
  댓글 수: 1
Sinan Islam
Sinan Islam 2020년 11월 15일
Why MATLAB never consider adding R to the External Language Interfaces? Isnt R popular enough?

댓글을 달려면 로그인하십시오.


Hemn
Hemn 2021년 6월 2일
편집: Hemn 2021년 6월 2일
Hi Daniel
Of course you can run R file in matlab and for this you can use !Rscript function. imagine that you have an R file named myfile.r, then you can run your file as following:
!Rscript myfile.r
just be careful that your R file is in your matlab path.
  댓글 수: 3
Angie Rodriguez
Angie Rodriguez 2023년 2월 4일
Hi! ... I was looking for that function but i didn't find it. Is not implemented anymore? In what version does it works?
Walter Roberson
Walter Roberson 2023년 2월 4일
If you are using Windows, then you could consider using System.Diagnostics.Process to create an instance of R that you could communicate with by standard input and standard output.

댓글을 달려면 로그인하십시오.


Haris K.
Haris K. 2024년 4월 15일
편집: Haris K. 2024년 4월 15일
A couple of things if you are trying to run an R script using system(), that could be helpful to other fellow MATLABians out there.
  1. You might need to add Rscript to your system's PATH environment variables. If you are on Windows, to do that, in your Windows search box type ‘Edit the system environment variables’ > click on the button ‘Environment Variables…’ > click on the line reading ‘Path’, and then on button ‘Edit’ > on the window that opens, press ‘New’ and type in the path of the Rscript.exe: ‘C:\Program Files\R\R-4.3.3\bin\x64’.
  2. By default R will set your MATLAB’s pwd as the R’s working directory when calling the R script via MATLAB. If the R script you want to run is located elsewhere, simply type the full path. For example:
system('Rscript C:\Users\x\Documents\myscript.R')
  댓글 수: 1
Haris K.
Haris K. 2024년 4월 16일
Also, you will need to enclose the path inside double quotation marks if you are specifying a path that contains spaces (-thats a cmd thing):
system('Rscript "C:\Users\x\Documents\Another Folder\myscript.R"')

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by