I successfully integrated MATLAB & R in macOS with Homebrew and Xcode Build Server (Apple Silicon). I made a step-by-step for that.
However, I am failing when trying to do the same in Windows 10, and I am failing... successfully as well. I tried to make the same step-by-step.
(1) Open PowerShell as Administrator
- Press Windows + S.
- Type PowerShell.
- Right-click on Windows PowerShell → Run as administrator.
(2) Run the Command Again
- Now, with administrator permissions, run:
Set-ExecutionPolicy Bypass -Scope Process -Force;
[System.Net.WebClient]::new().DownloadString('.ps1') | Invoke-Expression
This will correctly install Chocolatey.
(3) Verify the Installation Once finished, check if it works by running:
choco --version
Expected output:
2.4.2 (or a similar version)
INSTALL THE REQUIRED TOOLS
(1) Now install the tools MATLAB needs:
choco install gnuwin32-coreutils.install -y
(2)
- Check the Location of the expr.exe File
- Open PowerShell as Administrator.
- Run the following command:
dir "C:\" -Recurse -Filter expr.exe -ErrorAction SilentlyContinue
What does it do? It searches for expr.exe across the C: drive. You should see a path like:
C:\Program Files (x86)\GnuWin32\bin\expr.exe
(3) Add the Path to the System PATH If the previous command returned a valid path, add it to the PATH:
setx PATH "$($env:PATH);C:\Program Files (x86)\GnuWin32\bin"
(4) Verify that the PATH Includes expr.exe Close and reopen PowerShell. Run:
expr --version
Check if MATLAB is in the PATH
where matlab
If it doesn't return MATLAB's path, you need to add it manually.
- Add MATLAB to the PATH
- Find MATLAB’s bin folder, for example:
C:\Program Files\MATLAB\R2024b\bin
Open PowerShell as Administrator and run:
setx PATH "$($env:PATH);C:\Program Files\MATLAB\R2024b\bin"
Close and reopen PowerShell. Final Step: Restart MATLAB Restart the computer if necessary. Open MATLAB as Administrator.
Then, in macOS I had the following code and worked:
# install.packages("R.matlab")
library(R.matlab)
# system("matlab -nodesktop -nosplash -r \"disp('MATLAB works!');exit;\"")
# Save MATLAB code to file
matlab_code <- "
% MATLAB Model
% Parameters
alpha1 = 0.315; alpha2 = 0.100; alpha3 = 0.585;
beta = 0.95; sigma = 0.40; deltak = 0.06;
deltag = 0.05; rho = 0.95;
% Initial Values
Y = 1; C = 0.75; L = 0.3; K = 3.5;
I = 0.25; G = 1; z = 1; e = 0;
tau = 0.25; tauA = 0.22; lamda = 0.0303;
% Model Equations
for t = 1:1000
Y = z*(K^alpha1)*(G^alpha2)*(L^alpha3);
C = (sigma/(1-sigma))*(1-L)*(1-tau)*((alpha3/(alpha1+alpha3))*Y/L);
K = (Y - C) + (1 - deltak) * K;
G = lamda * Y + (1 - deltag) * G;
I = Y - C - (lamda * Y);
z = exp(rho * log(z) + randn * 0.01);
end
% Display results
disp(['Final Output: ', num2str(Y)]);
"
writeLines(matlab_code, "model.m")
# Run MATLAB script and capture output
matlab_output <- system("matlab -nodesktop -nosplash -r \"run('model.m'); exit;\"", intern = TRUE)
# Print output for debugging
print(matlab_output)
# Find the line containing "Final Output:"
final_line <- grep("Final Output:", matlab_output, value = TRUE)
# Extract the numeric part using a regular expression
final_value <- as.numeric(sub("Final Output: ", "", final_line))
# Print extracted value
print(final_value)
In Windows, R opens MATLAB displays the result but does not capture it and bring it to the environment.
matlab_output <- system("matlab -nodesktop -nosplash -r \"run('model.m'); exit;\"", intern = TRUE)
I successfully integrated MATLAB & R in macOS with Homebrew and Xcode Build Server (Apple Silicon). I made a step-by-step for that.
However, I am failing when trying to do the same in Windows 10, and I am failing... successfully as well. I tried to make the same step-by-step.
(1) Open PowerShell as Administrator
- Press Windows + S.
- Type PowerShell.
- Right-click on Windows PowerShell → Run as administrator.
(2) Run the Command Again
- Now, with administrator permissions, run:
Set-ExecutionPolicy Bypass -Scope Process -Force;
[System.Net.WebClient]::new().DownloadString('https://community.chocolatey./install.ps1') | Invoke-Expression
This will correctly install Chocolatey.
(3) Verify the Installation Once finished, check if it works by running:
choco --version
Expected output:
2.4.2 (or a similar version)
INSTALL THE REQUIRED TOOLS
(1) Now install the tools MATLAB needs:
choco install gnuwin32-coreutils.install -y
(2)
- Check the Location of the expr.exe File
- Open PowerShell as Administrator.
- Run the following command:
dir "C:\" -Recurse -Filter expr.exe -ErrorAction SilentlyContinue
What does it do? It searches for expr.exe across the C: drive. You should see a path like:
C:\Program Files (x86)\GnuWin32\bin\expr.exe
(3) Add the Path to the System PATH If the previous command returned a valid path, add it to the PATH:
setx PATH "$($env:PATH);C:\Program Files (x86)\GnuWin32\bin"
(4) Verify that the PATH Includes expr.exe Close and reopen PowerShell. Run:
expr --version
Check if MATLAB is in the PATH
where matlab
If it doesn't return MATLAB's path, you need to add it manually.
- Add MATLAB to the PATH
- Find MATLAB’s bin folder, for example:
C:\Program Files\MATLAB\R2024b\bin
Open PowerShell as Administrator and run:
setx PATH "$($env:PATH);C:\Program Files\MATLAB\R2024b\bin"
Close and reopen PowerShell. Final Step: Restart MATLAB Restart the computer if necessary. Open MATLAB as Administrator.
Then, in macOS I had the following code and worked:
# install.packages("R.matlab")
library(R.matlab)
# system("matlab -nodesktop -nosplash -r \"disp('MATLAB works!');exit;\"")
# Save MATLAB code to file
matlab_code <- "
% MATLAB Model
% Parameters
alpha1 = 0.315; alpha2 = 0.100; alpha3 = 0.585;
beta = 0.95; sigma = 0.40; deltak = 0.06;
deltag = 0.05; rho = 0.95;
% Initial Values
Y = 1; C = 0.75; L = 0.3; K = 3.5;
I = 0.25; G = 1; z = 1; e = 0;
tau = 0.25; tauA = 0.22; lamda = 0.0303;
% Model Equations
for t = 1:1000
Y = z*(K^alpha1)*(G^alpha2)*(L^alpha3);
C = (sigma/(1-sigma))*(1-L)*(1-tau)*((alpha3/(alpha1+alpha3))*Y/L);
K = (Y - C) + (1 - deltak) * K;
G = lamda * Y + (1 - deltag) * G;
I = Y - C - (lamda * Y);
z = exp(rho * log(z) + randn * 0.01);
end
% Display results
disp(['Final Output: ', num2str(Y)]);
"
writeLines(matlab_code, "model.m")
# Run MATLAB script and capture output
matlab_output <- system("matlab -nodesktop -nosplash -r \"run('model.m'); exit;\"", intern = TRUE)
# Print output for debugging
print(matlab_output)
# Find the line containing "Final Output:"
final_line <- grep("Final Output:", matlab_output, value = TRUE)
# Extract the numeric part using a regular expression
final_value <- as.numeric(sub("Final Output: ", "", final_line))
# Print extracted value
print(final_value)
In Windows, R opens MATLAB displays the result but does not capture it and bring it to the environment.
matlab_output <- system("matlab -nodesktop -nosplash -r \"run('model.m'); exit;\"", intern = TRUE)
Share
Improve this question
edited Feb 15 at 16:13
Paula
asked Feb 15 at 1:03
PaulaPaula
7033 silver badges10 bronze badges
9
|
Show 4 more comments
1 Answer
Reset to default 1Note that your installation method is likely incidental to your problem, given that both R and MATLAB seem to be working in principle.
I'm not familiar with MATLAB, but the docs suggest that what you're likely looking for is the
-batch
parameter rather than-r
, because only-batch
"Logs text to stdout and stderr."- If I understand the docs correctly,
-batch
is meant for non-interactive, CLI-only calls that behave like a console (terminal) application (which is what you're looking for), whereas-r
is meant for opening the MATLAB GUI and executing a startup command there, at the start of an interactive session.
- If I understand the docs correctly,
Therefore, try the following:
# Run MATLAB script and capture output
# Note the use of -batch instead of -r, which should also obviate the need for
# -nodesktop and -nosplash
matlab_output <- system("matlab -batch \"run('model.m'); exit;\"", intern = TRUE)
?system2
and rework yoursystem
line to use that instead. – G. Grothendieck Commented Feb 15 at 13:10"run('model.m'); exit;
"" – jdweng Commented Feb 15 at 13:32