필터 지우기
필터 지우기

How can i execute Matlab m.file in Excel VBA?

조회 수: 15 (최근 30일)
Lun Lee
Lun Lee 2015년 7월 29일
답변: Sean de Wolski 2015년 7월 31일
Hello!
I am working on a project, which the input data are from an excel workbook and then the result calculating by matlab are returned in another excel workbook. The m.file run correctly in Matlab and also return the values back in excel.
Now i am facing the problem to execute Matlab m.file from excel VBA without opening Matlab. I have looked up these related solutions on this forum and others, Spreadsheet Link EX i won't consider because my calculation in Matlab is 4-dimensions. I apply the solution from Johan (<http://www.mathworks.com/matlabcentral/answers/8684-running-a-specific-m-file-fig-from-excel)>.
However, until now there is still a problem. When i execute the excel macro to execute Matlab Application, the message 'Excel is waiting for another application to complete an OLE action.' comes out all the time. Afterwards i turn off the alert display in excel, then the error message 'Runtime Error 2147023170 (800706be): Automation Error The Remote Procedure Call Failed' appears.
The abstract of my m.file is:
enableservice('AutomationServer',true)
% Input
t1 = xlsread(filename,sheet,xlRange);
...
% Calculation
...
% Output
C = xlswrite(filename,sheet,xlRange);
The macro code in excel is like:
Sub runMatlab()
Dim hMatlab As Object
Dim sDir As String, cdsDir As String, s1 As String
Set hMatlab = CreateObject("Matlab.Application")
s1 = "'"
sDir = s1 & ActiveWorkbook.Path & s1
cdsDir ="cd(" & sDir & ")"
hMatlab.Execute(cdsDir)
Application.DisplayAlerts = False
hMatlab.Execute("Calculation")
Application.DisplayAlerts = True
ThisWorkbook.Close SaveChanges:=True
End Sub
Could someone give me a hint how to solve this problem? I will be really appreciate!

답변 (2개)

Udaya Mallampati
Udaya Mallampati 2015년 7월 31일
Hi Lun,
You can put a breakpoint in the VBA code and find out the line which causes this problem. If the call to "CreateObject" fails, then try registering MATLAB as COM server with the following procedure:
  • Shut down all MATLAB and Excel instances.
  • Open a command prompt, and using cd, change to the bin\win32 subfolder of the MATLAB installation folder.
  • Type the command:
.\matlab /regserver
  • When the MATLAB session starts, close it.
If the call to "matlabobj.Excecute" fails, then replace the calculation with a simple function call and see if it works. I executed the code at my end by replacing the calculation with a call to "magic()" function and found that it gives the expected result.
Hope this helps!

Sean de Wolski
Sean de Wolski 2015년 7월 31일
You could use Spreadsheet Link EX which takes care of dealing with COM for you so that you can seamlessly move data back and forth, call MATLAB functions and extract any graphics if necessary:

카테고리

Help CenterFile Exchange에서 Data Export to MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by