MATLAB Answers

Executing Matlab.m script from Excel VBA macro

조회 수: 121(최근 30일)
Yale 2012년 11월 6일
댓글: ashwin charles 2020년 12월 3일
The Matlab code that we wish to run reads in an “input.xls” file, performs calculations in Matlab .m modules, then outputs results to an “output.xls” file. There is an Excel VBA macro in a separate workbook that populates the “input.xls” file before calling the Matlab .m file with the following code:
Dim MatLab As Object
Set MatLab = CreateObject("Matlab.Application")
MatLab.Execute (PATH TO MATLAB.m)
The Matlab code should then populate the “output.xls” file (using xlswrite functions) with the final results once the run is complete. The Matlab code now fails to write to the “output.xls” file. There is evidence that the Excel macro is successfully calling the Matlab code since the Matlab application is opening in the background and running for the expected amount of time. However, the instance of Matlab is not visible and without the “output.xls” file, there is no way to diagnose the potential errors. Also, please note that run from the Matlab command line, the Matlab code populates the “output.xls” file perfectly so there should be no issues with the Matlab code.
Any help would be much appreciated!
  댓글 수: 2
Erik Engwall
Erik Engwall 2019년 6월 16일
Time management is zero sum. If the VBA already exists, then using it enables you to do something else.

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


Aaron Close
Aaron Close 2017년 2월 22일
you can use in VBA
fileToRun = "C:\path\path\file.m"
matlabCommand = "matlab -nodisplay -nosplash -nodesktop -r "" run('" & fileToRun & "');exit;"" "
Shell (matlabCommand)
  댓글 수: 1
Nallely Hernandez
Nallely Hernandez 2017년 4월 18일
Hi, could you help me please. I run my macro whit the code, but I don't have response
Function call_r() As Double
fileToRun = "C:\Users\Dell\OneDrive\Documentos2\MATLAB\suma.m"
matlabCommand = "matlab -nodisplay -nosplash -nodesktop -r "" run('" & fileToRun & "');exit;"" "
call_r = Shell(matlabCommand)
End Function
The code in Matlab:
function [res] = suma()
num1 = 7;
num2 = 5;
res = num1 + num2;

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

Vishal Rane
Vishal Rane 2012년 11월 8일
Have you explored the Spreadsheet Link EX toolbox ?
From its description I think it might help.

Karabo Magoro
Karabo Magoro 2012년 12월 13일
Hi Yale, I'm also having the same problem. If you find/found the solution please share.
Regards, Karabo

Long Cheng
Long Cheng 2016년 5월 25일
Hi, Yale,
I am having similar problem. Please share if you have any good solutions. Thank you.

Antonio Gugin
Antonio Gugin 2019년 7월 29일
Hey guys,
I am using this very helpful code (Thanks a lot), however, I was wondering if there was a way to use an already opened matlab window (if there is such) instead of always opening a new one each time this is ran. Thanks a lot!
fileToRun = "C:\path\path\file.m"
matlabCommand = "matlab -r "" run('" & fileToRun & "');"" "
Shell (matlabCommand)
  댓글 수: 1
ashwin charles
ashwin charles 2020년 12월 3일
Hey, did you find a away to not a new window every time?

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

Community Treasure Hunt

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

Start Hunting!

Translated by