Running a Matlab script from Excel

6 views (last 30 days)
Chris
Chris on 4 Apr 2014
Commented: Chris on 4 Apr 2014
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?

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 4 Apr 2014
Edited: Azzi Abdelmalek on 4 Apr 2014
I know that you can call a Matlab function from your Excell file. Transform your script to a function
  1 Comment
Chris
Chris on 4 Apr 2014
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.

Sign in to comment.

Categories

Find more on Data Export to MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!