Running a Matlab script from Excel

조회 수: 6 (최근 30일)
Chris
Chris 2014년 4월 4일
댓글: Chris 2014년 4월 4일
Hi,
I've looked through and tried several of the older suggestions on this forum related to the topic without any success.
I am looking to execute a matlab script from an excel macro. I know that my VBA code is communicating with Matlab because it briefly opens Matlab but then closes without performing the calculation I would like it to compute.
The excel file that is running the macro is also the file where the Matlab script needs to pull the information from to compute my data.
I have the Matlab script currently using the uigetfile function to select the excel file you would like to use to run in the script because it may not always match the file running the macro but in most cases it will. I suspect that since I am choosing the same file that is already open in excel and is driving the script to open that that is causing an issue. Does anyone have any advice?

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 4일
편집: Azzi Abdelmalek 2014년 4월 4일
I know that you can call a Matlab function from your Excell file. Transform your script to a function
  댓글 수: 1
Chris
Chris 2014년 4월 4일
Yes, I have this in the VBA code already.
Sub Run_Matlab_Script()
'Set the MATLAB object (the COM server).
Dim hMatlab As Object
Dim sDir As String, cdsDir As String, s1 As String
Dim Result As String
Set hMatlab = CreateObject("matlab.application")
s1 = "'"
sDir = s1 & ActiveWorkbook.Path & s1
cdsDir = "cd(" & sDir & ")"
hMatlab.Execute (cdsDir)
'Load the m file in MATLAB.
hMatlab.Execute ("neutral_response")
"neutral response" is my script name. As I said it opens Matlab briefly but after I choose the excel file (which happens to be the same file running the VBA macro) Matlab shuts down.

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

카테고리

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